simd-scalar-lowering.cc 59.2 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 8 9 10 11
#include "src/compiler/diamond.h"
#include "src/compiler/linkage.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/node.h"
12
#include "src/compiler/wasm-compiler.h"
13 14 15 16 17

namespace v8 {
namespace internal {
namespace compiler {

18 19 20
namespace {
static const int kNumLanes32 = 4;
static const int kNumLanes16 = 8;
21
static const int kNumLanes8 = 16;
22 23
static const int32_t kMask16 = 0xFFFF;
static const int32_t kMask8 = 0xFF;
24
static const int32_t kShift16 = 16;
25
static const int32_t kShift8 = 24;
26 27
}  // anonymous

28
SimdScalarLowering::SimdScalarLowering(
29 30 31 32
    MachineGraph* mcgraph, Signature<MachineRepresentation>* signature)
    : mcgraph_(mcgraph),
      state_(mcgraph->graph(), 3),
      stack_(mcgraph_->zone()),
33 34
      replacements_(nullptr),
      signature_(signature),
35 36
      placeholder_(graph()->NewNode(common()->Parameter(-2, "placeholder"),
                                    graph()->start())),
37
      parameter_count_after_lowering_(-1) {
38 39 40
  DCHECK_NOT_NULL(graph());
  DCHECK_NOT_NULL(graph()->end());
  replacements_ = zone()->NewArray<Replacement>(graph()->NodeCount());
41 42
  memset(static_cast<void*>(replacements_), 0,
         sizeof(Replacement) * graph()->NodeCount());
43 44 45 46 47
}

void SimdScalarLowering::LowerGraph() {
  stack_.push_back({graph()->end(), 0});
  state_.Set(graph()->end(), State::kOnStack);
48
  replacements_[graph()->end()->id()].type = SimdType::kInt32x4;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  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});
67 68 69
        } else if (input->opcode() == IrOpcode::kEffectPhi ||
                   input->opcode() == IrOpcode::kLoop) {
          stack_.push_front({input, 0});
70 71 72 73 74 75 76 77 78 79
        } else {
          stack_.push_back({input, 0});
        }
        state_.Set(input, State::kOnStack);
      }
    }
  }
}

#define FOREACH_INT32X4_OPCODE(V) \
80 81 82 83 84
  V(I32x4Splat)                   \
  V(I32x4ExtractLane)             \
  V(I32x4ReplaceLane)             \
  V(I32x4SConvertF32x4)           \
  V(I32x4UConvertF32x4)           \
85 86
  V(I32x4SConvertI16x8Low)        \
  V(I32x4SConvertI16x8High)       \
87
  V(I32x4Neg)                     \
88 89
  V(I32x4Shl)                     \
  V(I32x4ShrS)                    \
90
  V(I32x4Add)                     \
91
  V(I32x4AddHoriz)                \
92 93 94 95
  V(I32x4Sub)                     \
  V(I32x4Mul)                     \
  V(I32x4MinS)                    \
  V(I32x4MaxS)                    \
96
  V(I32x4ShrU)                    \
97 98
  V(I32x4MinU)                    \
  V(I32x4MaxU)                    \
99 100 101 102 103 104
  V(I32x4Eq)                      \
  V(I32x4Ne)                      \
  V(I32x4LtS)                     \
  V(I32x4LeS)                     \
  V(I32x4GtS)                     \
  V(I32x4GeS)                     \
105 106
  V(I32x4UConvertI16x8Low)        \
  V(I32x4UConvertI16x8High)       \
107 108 109 110
  V(I32x4LtU)                     \
  V(I32x4LeU)                     \
  V(I32x4GtU)                     \
  V(I32x4GeU)                     \
111 112 113
  V(S128And)                      \
  V(S128Or)                       \
  V(S128Xor)                      \
114 115 116 117 118 119 120
  V(S128Not)                      \
  V(S1x4AnyTrue)                  \
  V(S1x4AllTrue)                  \
  V(S1x8AnyTrue)                  \
  V(S1x8AllTrue)                  \
  V(S1x16AnyTrue)                 \
  V(S1x16AllTrue)
121 122

#define FOREACH_FLOAT32X4_OPCODE(V) \
123 124 125 126 127 128 129
  V(F32x4Splat)                     \
  V(F32x4ExtractLane)               \
  V(F32x4ReplaceLane)               \
  V(F32x4SConvertI32x4)             \
  V(F32x4UConvertI32x4)             \
  V(F32x4Abs)                       \
  V(F32x4Neg)                       \
130 131
  V(F32x4RecipApprox)               \
  V(F32x4RecipSqrtApprox)           \
132
  V(F32x4Add)                       \
133
  V(F32x4AddHoriz)                  \
134 135 136 137
  V(F32x4Sub)                       \
  V(F32x4Mul)                       \
  V(F32x4Min)                       \
  V(F32x4Max)
138

139
#define FOREACH_FLOAT32X4_TO_INT32X4OPCODE(V) \
140 141 142 143 144 145
  V(F32x4Eq)                                  \
  V(F32x4Ne)                                  \
  V(F32x4Lt)                                  \
  V(F32x4Le)                                  \
  V(F32x4Gt)                                  \
  V(F32x4Ge)
146

147 148 149 150
#define FOREACH_INT16X8_OPCODE(V) \
  V(I16x8Splat)                   \
  V(I16x8ExtractLane)             \
  V(I16x8ReplaceLane)             \
151 152
  V(I16x8SConvertI8x16Low)        \
  V(I16x8SConvertI8x16High)       \
153 154 155
  V(I16x8Neg)                     \
  V(I16x8Shl)                     \
  V(I16x8ShrS)                    \
156
  V(I16x8SConvertI32x4)           \
157 158
  V(I16x8Add)                     \
  V(I16x8AddSaturateS)            \
159
  V(I16x8AddHoriz)                \
160 161 162 163 164
  V(I16x8Sub)                     \
  V(I16x8SubSaturateS)            \
  V(I16x8Mul)                     \
  V(I16x8MinS)                    \
  V(I16x8MaxS)                    \
165 166
  V(I16x8UConvertI8x16Low)        \
  V(I16x8UConvertI8x16High)       \
167
  V(I16x8ShrU)                    \
168
  V(I16x8UConvertI32x4)           \
169 170 171
  V(I16x8AddSaturateU)            \
  V(I16x8SubSaturateU)            \
  V(I16x8MinU)                    \
172 173 174 175 176 177 178
  V(I16x8MaxU)                    \
  V(I16x8Eq)                      \
  V(I16x8Ne)                      \
  V(I16x8LtS)                     \
  V(I16x8LeS)                     \
  V(I16x8LtU)                     \
  V(I16x8LeU)
179

180 181 182 183
#define FOREACH_INT8X16_OPCODE(V) \
  V(I8x16Splat)                   \
  V(I8x16ExtractLane)             \
  V(I8x16ReplaceLane)             \
184
  V(I8x16SConvertI16x8)           \
185 186 187 188 189 190 191 192 193 194 195
  V(I8x16Neg)                     \
  V(I8x16Shl)                     \
  V(I8x16ShrS)                    \
  V(I8x16Add)                     \
  V(I8x16AddSaturateS)            \
  V(I8x16Sub)                     \
  V(I8x16SubSaturateS)            \
  V(I8x16Mul)                     \
  V(I8x16MinS)                    \
  V(I8x16MaxS)                    \
  V(I8x16ShrU)                    \
196
  V(I8x16UConvertI16x8)           \
197 198 199
  V(I8x16AddSaturateU)            \
  V(I8x16SubSaturateU)            \
  V(I8x16MinU)                    \
200 201 202 203 204 205
  V(I8x16MaxU)                    \
  V(I8x16Eq)                      \
  V(I8x16Ne)                      \
  V(I8x16LtS)                     \
  V(I8x16LeS)                     \
  V(I8x16LtU)                     \
206 207
  V(I8x16LeU)                     \
  V(S8x16Shuffle)
208

209 210 211 212 213 214 215 216 217 218 219 220 221
MachineType SimdScalarLowering::MachineTypeFrom(SimdType simdType) {
  switch (simdType) {
    case SimdType::kFloat32x4:
      return MachineType::Float32();
    case SimdType::kInt32x4:
      return MachineType::Int32();
    case SimdType::kInt16x8:
      return MachineType::Int16();
    case SimdType::kInt8x16:
      return MachineType::Int8();
  }
  return MachineType::None();
}
222

223 224 225 226 227 228 229
void SimdScalarLowering::SetLoweredType(Node* node, Node* output) {
  switch (node->opcode()) {
#define CASE_STMT(name) case IrOpcode::k##name:
    FOREACH_INT32X4_OPCODE(CASE_STMT)
    case IrOpcode::kReturn:
    case IrOpcode::kParameter:
    case IrOpcode::kCall: {
230
      replacements_[node->id()].type = SimdType::kInt32x4;
231 232 233
      break;
    }
      FOREACH_FLOAT32X4_OPCODE(CASE_STMT) {
234
        replacements_[node->id()].type = SimdType::kFloat32x4;
235 236
        break;
      }
237
      FOREACH_FLOAT32X4_TO_INT32X4OPCODE(CASE_STMT) {
238
        replacements_[node->id()].type = SimdType::kInt32x4;
239 240
        break;
      }
241 242 243 244
      FOREACH_INT16X8_OPCODE(CASE_STMT) {
        replacements_[node->id()].type = SimdType::kInt16x8;
        break;
      }
245 246 247 248
      FOREACH_INT8X16_OPCODE(CASE_STMT) {
        replacements_[node->id()].type = SimdType::kInt8x16;
        break;
      }
249
    default: {
250
      switch (output->opcode()) {
251
        case IrOpcode::kF32x4SConvertI32x4:
252 253 254
        case IrOpcode::kF32x4UConvertI32x4:
        case IrOpcode::kI16x8SConvertI32x4:
        case IrOpcode::kI16x8UConvertI32x4: {
255
          replacements_[node->id()].type = SimdType::kInt32x4;
256
          break;
257 258
        }
        case IrOpcode::kI8x16SConvertI16x8:
259 260 261 262 263
        case IrOpcode::kI8x16UConvertI16x8:
        case IrOpcode::kI32x4SConvertI16x8Low:
        case IrOpcode::kI32x4SConvertI16x8High:
        case IrOpcode::kI32x4UConvertI16x8Low:
        case IrOpcode::kI32x4UConvertI16x8High: {
264 265
          replacements_[node->id()].type = SimdType::kInt16x8;
          break;
266 267 268 269 270 271 272
        }
        case IrOpcode::kI16x8SConvertI8x16Low:
        case IrOpcode::kI16x8SConvertI8x16High:
        case IrOpcode::kI16x8UConvertI8x16Low:
        case IrOpcode::kI16x8UConvertI8x16High: {
          replacements_[node->id()].type = SimdType::kInt8x16;
          break;
273
        }
274
          FOREACH_FLOAT32X4_TO_INT32X4OPCODE(CASE_STMT)
275 276
        case IrOpcode::kI32x4SConvertF32x4:
        case IrOpcode::kI32x4UConvertF32x4: {
277
          replacements_[node->id()].type = SimdType::kFloat32x4;
278 279
          break;
        }
280 281
        case IrOpcode::kS128Select: {
          replacements_[node->id()].type = SimdType::kInt32x4;
282
          break;
283
        }
284 285 286
        default: {
          replacements_[node->id()].type = replacements_[output->id()].type;
        }
287 288
      }
    }
289
#undef CASE_STMT
290 291 292
  }
}

293
static int GetParameterIndexAfterLoweringSimd128(
294 295 296 297
    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;
298
  for (int i = 0; i < old_index; ++i) {
299 300 301 302 303 304 305 306 307
    if (signature->GetParam(i) == MachineRepresentation::kSimd128) {
      result += 3;
    }
  }
  return result;
}

int SimdScalarLowering::GetParameterCountAfterLowering() {
  if (parameter_count_after_lowering_ == -1) {
308 309 310
    // GetParameterIndexAfterLoweringSimd128(parameter_count) returns the
    // parameter count after lowering.
    parameter_count_after_lowering_ = GetParameterIndexAfterLoweringSimd128(
311 312 313 314 315
        signature(), static_cast<int>(signature()->parameter_count()));
  }
  return parameter_count_after_lowering_;
}

316
static int GetReturnCountAfterLoweringSimd128(
317 318
    Signature<MachineRepresentation>* signature) {
  int result = static_cast<int>(signature->return_count());
319
  for (int i = 0; i < static_cast<int>(signature->return_count()); ++i) {
320 321 322 323 324 325 326
    if (signature->GetReturn(i) == MachineRepresentation::kSimd128) {
      result += 3;
    }
  }
  return result;
}

327 328
int SimdScalarLowering::NumLanes(SimdType type) {
  int num_lanes = 0;
329
  if (type == SimdType::kFloat32x4 || type == SimdType::kInt32x4) {
330
    num_lanes = kNumLanes32;
331
  } else if (type == SimdType::kInt16x8) {
332
    num_lanes = kNumLanes16;
333
  } else if (type == SimdType::kInt8x16) {
334
    num_lanes = kNumLanes8;
335 336 337 338 339 340
  } else {
    UNREACHABLE();
  }
  return num_lanes;
}

341 342
constexpr int SimdScalarLowering::kLaneOffsets[];

343 344 345 346
void SimdScalarLowering::GetIndexNodes(Node* index, Node** new_indices,
                                       SimdType type) {
  int num_lanes = NumLanes(type);
  int lane_width = kSimd128Size / num_lanes;
347 348
  int laneIndex = kLaneOffsets[0] / lane_width;
  new_indices[laneIndex] = index;
349
  for (int i = 1; i < num_lanes; ++i) {
350 351 352 353 354
    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)));
355 356 357
  }
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
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();
  }
374 375 376
  if (rep == MachineRepresentation::kSimd128) {
    Node* base = node->InputAt(0);
    Node* index = node->InputAt(1);
377 378 379 380
    int num_lanes = NumLanes(type);
    Node** indices = zone()->NewArray<Node*>(num_lanes);
    GetIndexNodes(index, indices, type);
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
381
    rep_nodes[0] = node;
382
    rep_nodes[0]->ReplaceInput(1, indices[0]);
383 384
    NodeProperties::ChangeOp(rep_nodes[0], load_op);
    if (node->InputCount() > 2) {
385
      DCHECK_LT(3, node->InputCount());
386 387
      Node* effect_input = node->InputAt(2);
      Node* control_input = node->InputAt(3);
388 389 390 391 392
      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];
      }
393 394
      rep_nodes[0]->ReplaceInput(2, rep_nodes[1]);
    } else {
395
      for (int i = 1; i < num_lanes; ++i) {
396 397 398
        rep_nodes[i] = graph()->NewNode(load_op, base, indices[i]);
      }
    }
399
    ReplaceNode(node, rep_nodes, num_lanes);
400 401 402 403 404
  } else {
    DefaultLowering(node);
  }
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
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();
  }
437 438 439
  if (rep == MachineRepresentation::kSimd128) {
    Node* base = node->InputAt(0);
    Node* index = node->InputAt(1);
440 441 442
    int num_lanes = NumLanes(rep_type);
    Node** indices = zone()->NewArray<Node*>(num_lanes);
    GetIndexNodes(index, indices, rep_type);
443 444
    Node* value = node->InputAt(2);
    DCHECK(HasReplacement(1, value));
445
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
446 447 448
    rep_nodes[0] = node;
    Node** rep_inputs = GetReplacementsWithType(value, rep_type);
    rep_nodes[0]->ReplaceInput(2, rep_inputs[0]);
449
    rep_nodes[0]->ReplaceInput(1, indices[0]);
450 451
    NodeProperties::ChangeOp(node, store_op);
    if (node->InputCount() > 3) {
452
      DCHECK_LT(4, node->InputCount());
453 454
      Node* effect_input = node->InputAt(3);
      Node* control_input = node->InputAt(4);
455 456 457 458 459 460
      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];
      }
461 462
      rep_nodes[0]->ReplaceInput(3, rep_nodes[1]);
    } else {
463
      for (int i = 1; i < num_lanes; ++i) {
464 465 466 467
        rep_nodes[i] =
            graph()->NewNode(store_op, base, indices[i], rep_inputs[i]);
      }
    }
468
    ReplaceNode(node, rep_nodes, num_lanes);
469 470 471 472 473
  } else {
    DefaultLowering(node);
  }
}

474
void SimdScalarLowering::LowerBinaryOp(Node* node, SimdType input_rep_type,
475 476
                                       const Operator* op,
                                       bool not_horizontal) {
477
  DCHECK_EQ(2, node->InputCount());
478 479 480 481
  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);
482 483 484 485 486 487 488 489 490 491
  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]);
    }
492 493 494 495 496 497 498
  }
  ReplaceNode(node, rep_node, num_lanes);
}

void SimdScalarLowering::LowerCompareOp(Node* node, SimdType input_rep_type,
                                        const Operator* op,
                                        bool invert_inputs) {
499
  DCHECK_EQ(2, node->InputCount());
500 501
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
502 503 504
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
505
    Node* cmp_result = nullptr;
506
    if (invert_inputs) {
507
      cmp_result = graph()->NewNode(op, rep_right[i], rep_left[i]);
508
    } else {
509
      cmp_result = graph()->NewNode(op, rep_left[i], rep_right[i]);
510
    }
511 512
    Diamond d_cmp(graph(), common(),
                  graph()->NewNode(machine()->Word32Equal(), cmp_result,
513
                                   mcgraph_->Int32Constant(0)));
514 515 516 517 518
    MachineRepresentation rep =
        (input_rep_type == SimdType::kFloat32x4)
            ? MachineRepresentation::kWord32
            : MachineTypeFrom(input_rep_type).representation();
    rep_node[i] =
519
        d_cmp.Phi(rep, mcgraph_->Int32Constant(0), mcgraph_->Int32Constant(-1));
520
  }
521 522 523 524 525 526
  ReplaceNode(node, rep_node, num_lanes);
}

Node* SimdScalarLowering::FixUpperBits(Node* input, int32_t shift) {
  return graph()->NewNode(machine()->Word32Sar(),
                          graph()->NewNode(machine()->Word32Shl(), input,
527 528
                                           mcgraph_->Int32Constant(shift)),
                          mcgraph_->Int32Constant(shift));
529 530 531 532
}

void SimdScalarLowering::LowerBinaryOpForSmallInt(Node* node,
                                                  SimdType input_rep_type,
533 534
                                                  const Operator* op,
                                                  bool not_horizontal) {
535
  DCHECK_EQ(2, node->InputCount());
536 537
  DCHECK(input_rep_type == SimdType::kInt16x8 ||
         input_rep_type == SimdType::kInt8x16);
538 539 540 541
  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);
542 543
  int32_t shift_val =
      (input_rep_type == SimdType::kInt16x8) ? kShift16 : kShift8;
544 545 546 547 548 549 550 551 552 553 554 555 556 557
  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);
    }
558 559 560 561 562 563
  }
  ReplaceNode(node, rep_node, num_lanes);
}

Node* SimdScalarLowering::Mask(Node* input, int32_t mask) {
  return graph()->NewNode(machine()->Word32And(), input,
564
                          mcgraph_->Int32Constant(mask));
565 566 567 568 569 570
}

void SimdScalarLowering::LowerSaturateBinaryOp(Node* node,
                                               SimdType input_rep_type,
                                               const Operator* op,
                                               bool is_signed) {
571
  DCHECK_EQ(2, node->InputCount());
572 573
  DCHECK(input_rep_type == SimdType::kInt16x8 ||
         input_rep_type == SimdType::kInt8x16);
574 575 576 577
  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;
578 579 580 581 582 583 584 585 586 587 588 589 590 591
  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;
592
  } else {
593 594 595 596 597 598 599 600 601 602
    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;
603 604 605 606 607
  }
  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;
608 609
    Node* left = is_signed ? rep_left[i] : Mask(rep_left[i], mask);
    Node* right = is_signed ? rep_right[i] : Mask(rep_right[i], mask);
610 611 612
    op_result = graph()->NewNode(op, left, right);
    Diamond d_min(graph(), common(),
                  graph()->NewNode(machine()->Int32LessThan(), op_result,
613 614
                                   mcgraph_->Int32Constant(min)));
    rep_node[i] = d_min.Phi(phi_rep, mcgraph_->Int32Constant(min), op_result);
615 616
    Diamond d_max(graph(), common(),
                  graph()->NewNode(machine()->Int32LessThan(),
617 618
                                   mcgraph_->Int32Constant(max), rep_node[i]));
    rep_node[i] = d_max.Phi(phi_rep, mcgraph_->Int32Constant(max), rep_node[i]);
619 620
    rep_node[i] =
        is_signed ? rep_node[i] : FixUpperBits(rep_node[i], shift_val);
621 622
  }
  ReplaceNode(node, rep_node, num_lanes);
623 624
}

625 626
void SimdScalarLowering::LowerUnaryOp(Node* node, SimdType input_rep_type,
                                      const Operator* op) {
627
  DCHECK_EQ(1, node->InputCount());
628
  Node** rep = GetReplacementsWithType(node->InputAt(0), input_rep_type);
629 630 631
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
632 633
    rep_node[i] = graph()->NewNode(op, rep[i]);
  }
634
  ReplaceNode(node, rep_node, num_lanes);
635 636
}

637
void SimdScalarLowering::LowerIntMinMax(Node* node, const Operator* op,
638
                                        bool is_max, SimdType type) {
639
  DCHECK_EQ(2, node->InputCount());
640 641 642 643 644 645 646 647 648
  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;
649 650
  } else if (type == SimdType::kInt8x16) {
    rep = MachineRepresentation::kWord8;
651 652 653 654
  } else {
    UNREACHABLE();
  }
  for (int i = 0; i < num_lanes; ++i) {
655 656 657
    Diamond d(graph(), common(),
              graph()->NewNode(op, rep_left[i], rep_right[i]));
    if (is_max) {
658
      rep_node[i] = d.Phi(rep, rep_right[i], rep_left[i]);
659
    } else {
660
      rep_node[i] = d.Phi(rep, rep_left[i], rep_right[i]);
661 662
    }
  }
663
  ReplaceNode(node, rep_node, num_lanes);
664 665 666 667 668 669
}

Node* SimdScalarLowering::BuildF64Trunc(Node* input) {
  if (machine()->Float64RoundTruncate().IsSupported()) {
    return graph()->NewNode(machine()->Float64RoundTruncate().op(), input);
  } else {
670
    ExternalReference ref = ExternalReference::wasm_f64_trunc();
671 672 673 674 675
    Node* stack_slot =
        graph()->NewNode(machine()->StackSlot(MachineRepresentation::kFloat64));
    const Operator* store_op = machine()->Store(
        StoreRepresentation(MachineRepresentation::kFloat64, kNoWriteBarrier));
    Node* effect =
676
        graph()->NewNode(store_op, stack_slot, mcgraph_->Int32Constant(0),
677 678 679 680 681 682 683 684 685
                         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());
686
    auto call_descriptor =
687
        Linkage::GetSimplifiedCDescriptor(zone(), sig_builder.Build());
688
    Node* call = graph()->NewNode(common()->Call(call_descriptor), 4, args);
689
    return graph()->NewNode(machine()->Load(LoadRepresentation::Float64()),
690
                            stack_slot, mcgraph_->Int32Constant(0), call,
691 692 693 694 695
                            graph()->start());
  }
}

void SimdScalarLowering::LowerConvertFromFloat(Node* node, bool is_signed) {
696
  DCHECK_EQ(1, node->InputCount());
697 698
  Node** rep = GetReplacementsWithType(node->InputAt(0), SimdType::kFloat32x4);
  Node* rep_node[kNumLanes32];
699 700 701 702
  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(
703
      static_cast<double>(is_signed ? kMaxInt : 0xFFFFFFFFu)));
704
  for (int i = 0; i < kNumLanes32; ++i) {
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    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);
    }
  }
725
  ReplaceNode(node, rep_node, kNumLanes32);
726 727
}

728 729 730
void SimdScalarLowering::LowerConvertFromInt(Node* node,
                                             SimdType input_rep_type,
                                             SimdType output_rep_type,
731
                                             bool is_signed, int start_index) {
732 733 734
  DCHECK_EQ(1, node->InputCount());
  Node** rep = GetReplacementsWithType(node->InputAt(0), input_rep_type);

735
  int32_t mask = 0;
736 737
  if (input_rep_type == SimdType::kInt16x8) {
    DCHECK_EQ(output_rep_type, SimdType::kInt32x4);
738
    mask = kMask16;
739 740 741
  } else {
    DCHECK_EQ(output_rep_type, SimdType::kInt16x8);
    DCHECK_EQ(input_rep_type, SimdType::kInt8x16);
742
    mask = kMask8;
743 744 745 746 747
  }

  int num_lanes = NumLanes(output_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
748 749
    rep_node[i] =
        is_signed ? rep[i + start_index] : Mask(rep[i + start_index], mask);
750 751 752 753 754
  }

  ReplaceNode(node, rep_node, num_lanes);
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768
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);
  const Operator* less_op =
      is_signed ? machine()->Int32LessThan() : machine()->Uint32LessThan();
  Node* min = nullptr;
  Node* max = nullptr;
  int32_t shift_val = 0;
  MachineRepresentation phi_rep;
  if (output_rep_type == SimdType::kInt16x8) {
    DCHECK(input_rep_type == SimdType::kInt32x4);
    if (is_signed) {
769 770
      min = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::min());
      max = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::max());
771
    } else {
772
      max = mcgraph_->Uint32Constant(std::numeric_limits<uint16_t>::max());
773 774 775 776 777 778 779
      shift_val = kShift16;
    }
    phi_rep = MachineRepresentation::kWord16;
  } else {
    DCHECK(output_rep_type == SimdType::kInt8x16 &&
           input_rep_type == SimdType::kInt16x8);
    if (is_signed) {
780 781
      min = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::min());
      max = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::max());
782
    } else {
783
      max = mcgraph_->Uint32Constant(std::numeric_limits<uint8_t>::max());
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
      shift_val = kShift8;
    }
    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];
    if (is_signed) {
      Diamond d_min(graph(), common(), graph()->NewNode(less_op, input, min));
      input = d_min.Phi(phi_rep, min, input);
    }
    Diamond d_max(graph(), common(), graph()->NewNode(less_op, max, input));
    rep_node[i] = d_max.Phi(phi_rep, max, input);
    rep_node[i] =
        is_signed ? rep_node[i] : FixUpperBits(rep_node[i], shift_val);
  }
  ReplaceNode(node, rep_node, num_lanes);
}

808
void SimdScalarLowering::LowerShiftOp(Node* node, SimdType type) {
809
  DCHECK_EQ(1, node->InputCount());
810
  int32_t shift_amount = OpParameter<int32_t>(node->op());
811 812 813 814 815 816 817
  Node* shift_node = graph()->NewNode(common()->Int32Constant(shift_amount));
  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()) {
818 819 820 821 822
      case IrOpcode::kI8x16ShrU:
        rep_node[i] = Mask(rep_node[i], kMask8);
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shr(), rep_node[i], shift_node);
        break;
823
      case IrOpcode::kI16x8ShrU:
824 825
        rep_node[i] = Mask(rep_node[i], kMask16);
        V8_FALLTHROUGH;
826 827 828 829 830 831 832 833 834 835 836 837 838
      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;
839 840 841 842 843
      case IrOpcode::kI8x16Shl:
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shl(), rep_node[i], shift_node);
        rep_node[i] = FixUpperBits(rep_node[i], kShift8);
        break;
844 845
      case IrOpcode::kI32x4ShrS:
      case IrOpcode::kI16x8ShrS:
846
      case IrOpcode::kI8x16ShrS:
847 848 849 850 851 852
        rep_node[i] =
            graph()->NewNode(machine()->Word32Sar(), rep_node[i], shift_node);
        break;
      default:
        UNREACHABLE();
    }
853
  }
854
  ReplaceNode(node, rep_node, num_lanes);
855 856
}

857 858
void SimdScalarLowering::LowerNotEqual(Node* node, SimdType input_rep_type,
                                       const Operator* op) {
859
  DCHECK_EQ(2, node->InputCount());
860 861
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
862 863 864
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
865 866
    Diamond d(graph(), common(),
              graph()->NewNode(op, rep_left[i], rep_right[i]));
867 868 869 870 871
    MachineRepresentation rep =
        (input_rep_type == SimdType::kFloat32x4)
            ? MachineRepresentation::kWord32
            : MachineTypeFrom(input_rep_type).representation();
    rep_node[i] =
872
        d.Phi(rep, mcgraph_->Int32Constant(0), mcgraph_->Int32Constant(-1));
873
  }
874
  ReplaceNode(node, rep_node, num_lanes);
875 876
}

877 878
void SimdScalarLowering::LowerNode(Node* node) {
  SimdType rep_type = ReplacementType(node);
879
  int num_lanes = NumLanes(rep_type);
880 881 882 883
  switch (node->opcode()) {
    case IrOpcode::kStart: {
      int parameter_count = GetParameterCountAfterLowering();
      // Only exchange the node if the parameter count actually changed.
884
      if (parameter_count != static_cast<int>(signature()->parameter_count())) {
885 886 887 888 889 890 891 892
        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: {
893
      DCHECK_EQ(1, node->InputCount());
894 895 896 897
      // Only exchange the node if the parameter count actually changed. We do
      // not even have to do the default lowering because the the start node,
      // the only input of a parameter node, only changes if the parameter count
      // changes.
898 899
      if (GetParameterCountAfterLowering() !=
          static_cast<int>(signature()->parameter_count())) {
900
        int old_index = ParameterIndexOf(node->op());
901 902
        int new_index =
            GetParameterIndexAfterLoweringSimd128(signature(), old_index);
903 904 905
        if (old_index == new_index) {
          NodeProperties::ChangeOp(node, common()->Parameter(new_index));

906 907
          Node* new_node[kNumLanes32];
          for (int i = 0; i < kNumLanes32; ++i) {
908 909 910 911 912
            new_node[i] = nullptr;
          }
          new_node[0] = node;
          if (signature()->GetParam(old_index) ==
              MachineRepresentation::kSimd128) {
913
            for (int i = 1; i < kNumLanes32; ++i) {
914 915 916 917
              new_node[i] = graph()->NewNode(common()->Parameter(new_index + i),
                                             graph()->start());
            }
          }
918
          ReplaceNode(node, new_node, kNumLanes32);
919 920 921 922
        }
      }
      break;
    }
923 924 925 926
    case IrOpcode::kLoad:
    case IrOpcode::kUnalignedLoad:
    case IrOpcode::kProtectedLoad: {
      LowerLoadOp(node, rep_type);
927 928
      break;
    }
929 930 931 932
    case IrOpcode::kStore:
    case IrOpcode::kUnalignedStore:
    case IrOpcode::kProtectedStore: {
      LowerStoreOp(node);
933 934
      break;
    }
935 936
    case IrOpcode::kReturn: {
      DefaultLowering(node);
937
      int new_return_count = GetReturnCountAfterLoweringSimd128(signature());
938
      if (static_cast<int>(signature()->return_count()) != new_return_count) {
939 940 941 942 943
        NodeProperties::ChangeOp(node, common()->Return(new_return_count));
      }
      break;
    }
    case IrOpcode::kCall: {
944
      // TODO(turbofan): Make wasm code const-correct wrt. CallDescriptor.
945
      auto call_descriptor =
946 947
          const_cast<CallDescriptor*>(CallDescriptorOf(node->op()));
      if (DefaultLowering(node) ||
948 949
          (call_descriptor->ReturnCount() == 1 &&
           call_descriptor->GetReturnType(0) == MachineType::Simd128())) {
950
        // We have to adjust the call descriptor.
951 952
        const Operator* op = common()->Call(
            GetI32WasmCallDescriptorForSimd(zone(), call_descriptor));
953 954
        NodeProperties::ChangeOp(node, op);
      }
955 956
      if (call_descriptor->ReturnCount() == 1 &&
          call_descriptor->GetReturnType(0) == MachineType::Simd128()) {
957
        // We access the additional return values through projections.
958 959
        Node* rep_node[kNumLanes32];
        for (int i = 0; i < kNumLanes32; ++i) {
960 961 962
          rep_node[i] =
              graph()->NewNode(common()->Projection(i), node, graph()->start());
        }
963
        ReplaceNode(node, rep_node, kNumLanes32);
964 965 966 967 968 969 970 971 972
      }
      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);
973
        for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
974 975
          Node** rep_input =
              GetReplacementsWithType(node->InputAt(i), rep_type);
976
          for (int j = 0; j < num_lanes; j++) {
977 978 979 980 981 982 983 984
            rep_node[j]->ReplaceInput(i, rep_input[j]);
          }
        }
      } else {
        DefaultLowering(node);
      }
      break;
    }
985 986 987 988 989
#define I32X4_BINOP_CASE(opcode, instruction)                \
  case IrOpcode::opcode: {                                   \
    LowerBinaryOp(node, rep_type, machine()->instruction()); \
    break;                                                   \
  }
990 991 992 993 994 995
      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)
996
#undef I32X4_BINOP_CASE
997 998 999 1000
    case IrOpcode::kI32x4AddHoriz: {
      LowerBinaryOp(node, rep_type, machine()->Int32Add(), false);
      break;
    }
1001 1002 1003 1004
    case IrOpcode::kI16x8AddHoriz: {
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Add(), false);
      break;
    }
1005 1006
    case IrOpcode::kI16x8Add:
    case IrOpcode::kI8x16Add: {
1007 1008 1009
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Add());
      break;
    }
1010 1011
    case IrOpcode::kI16x8Sub:
    case IrOpcode::kI8x16Sub: {
1012 1013 1014
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Sub());
      break;
    }
1015 1016
    case IrOpcode::kI16x8Mul:
    case IrOpcode::kI8x16Mul: {
1017 1018 1019
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Mul());
      break;
    }
1020 1021
    case IrOpcode::kI16x8AddSaturateS:
    case IrOpcode::kI8x16AddSaturateS: {
1022 1023 1024
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Add(), true);
      break;
    }
1025 1026
    case IrOpcode::kI16x8SubSaturateS:
    case IrOpcode::kI8x16SubSaturateS: {
1027 1028 1029
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Sub(), true);
      break;
    }
1030 1031
    case IrOpcode::kI16x8AddSaturateU:
    case IrOpcode::kI8x16AddSaturateU: {
1032
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Add(), false);
1033 1034
      break;
    }
1035 1036
    case IrOpcode::kI16x8SubSaturateU:
    case IrOpcode::kI8x16SubSaturateU: {
1037
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Sub(), false);
1038 1039
      break;
    }
1040
    case IrOpcode::kI32x4MaxS:
1041 1042
    case IrOpcode::kI16x8MaxS:
    case IrOpcode::kI8x16MaxS: {
1043
      LowerIntMinMax(node, machine()->Int32LessThan(), true, rep_type);
1044 1045
      break;
    }
1046
    case IrOpcode::kI32x4MinS:
1047 1048
    case IrOpcode::kI16x8MinS:
    case IrOpcode::kI8x16MinS: {
1049 1050 1051 1052
      LowerIntMinMax(node, machine()->Int32LessThan(), false, rep_type);
      break;
    }
    case IrOpcode::kI32x4MaxU:
1053 1054
    case IrOpcode::kI16x8MaxU:
    case IrOpcode::kI8x16MaxU: {
1055 1056 1057 1058
      LowerIntMinMax(node, machine()->Uint32LessThan(), true, rep_type);
      break;
    }
    case IrOpcode::kI32x4MinU:
1059 1060
    case IrOpcode::kI16x8MinU:
    case IrOpcode::kI8x16MinU: {
1061
      LowerIntMinMax(node, machine()->Uint32LessThan(), false, rep_type);
1062 1063
      break;
    }
1064
    case IrOpcode::kI32x4Neg:
1065 1066
    case IrOpcode::kI16x8Neg:
    case IrOpcode::kI8x16Neg: {
1067
      DCHECK_EQ(1, node->InputCount());
1068
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
1069 1070
      int num_lanes = NumLanes(rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
1071
      Node* zero = graph()->NewNode(common()->Int32Constant(0));
1072
      for (int i = 0; i < num_lanes; ++i) {
1073
        rep_node[i] = graph()->NewNode(machine()->Int32Sub(), zero, rep[i]);
1074 1075
        if (node->opcode() == IrOpcode::kI16x8Neg) {
          rep_node[i] = FixUpperBits(rep_node[i], kShift16);
1076 1077
        } else if (node->opcode() == IrOpcode::kI8x16Neg) {
          rep_node[i] = FixUpperBits(rep_node[i], kShift8);
1078
        }
1079
      }
1080
      ReplaceNode(node, rep_node, num_lanes);
1081 1082
      break;
    }
1083 1084 1085 1086 1087 1088 1089 1090 1091
    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;
    }
1092
    case IrOpcode::kS128Not: {
1093
      DCHECK_EQ(1, node->InputCount());
1094
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
1095
      Node* rep_node[kNumLanes32];
1096
      Node* mask = graph()->NewNode(common()->Int32Constant(0xFFFFFFFF));
1097
      for (int i = 0; i < kNumLanes32; ++i) {
1098 1099
        rep_node[i] = graph()->NewNode(machine()->Word32Xor(), rep[i], mask);
      }
1100
      ReplaceNode(node, rep_node, kNumLanes32);
1101 1102
      break;
    }
1103
    case IrOpcode::kI32x4SConvertF32x4: {
1104 1105 1106
      LowerConvertFromFloat(node, true);
      break;
    }
1107
    case IrOpcode::kI32x4UConvertF32x4: {
1108 1109 1110
      LowerConvertFromFloat(node, false);
      break;
    }
1111 1112 1113 1114 1115
    case IrOpcode::kI32x4SConvertI16x8Low: {
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, true,
                          0);
      break;
    }
1116
    case IrOpcode::kI32x4SConvertI16x8High: {
1117 1118 1119 1120 1121 1122 1123
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, true,
                          4);
      break;
    }
    case IrOpcode::kI32x4UConvertI16x8Low: {
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, false,
                          0);
1124 1125 1126
      break;
    }
    case IrOpcode::kI32x4UConvertI16x8High: {
1127 1128 1129 1130 1131 1132 1133
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, false,
                          4);
      break;
    }
    case IrOpcode::kI16x8SConvertI8x16Low: {
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, true,
                          0);
1134 1135 1136
      break;
    }
    case IrOpcode::kI16x8SConvertI8x16High: {
1137 1138 1139 1140 1141 1142 1143
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, true,
                          8);
      break;
    }
    case IrOpcode::kI16x8UConvertI8x16Low: {
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, false,
                          0);
1144 1145 1146
      break;
    }
    case IrOpcode::kI16x8UConvertI8x16High: {
1147 1148
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, false,
                          8);
1149 1150
      break;
    }
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    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;
    }
1167 1168
    case IrOpcode::kI32x4Shl:
    case IrOpcode::kI16x8Shl:
1169
    case IrOpcode::kI8x16Shl:
1170 1171
    case IrOpcode::kI32x4ShrS:
    case IrOpcode::kI16x8ShrS:
1172
    case IrOpcode::kI8x16ShrS:
1173
    case IrOpcode::kI32x4ShrU:
1174 1175
    case IrOpcode::kI16x8ShrU:
    case IrOpcode::kI8x16ShrU: {
1176
      LowerShiftOp(node, rep_type);
1177 1178
      break;
    }
1179 1180 1181 1182
    case IrOpcode::kF32x4AddHoriz: {
      LowerBinaryOp(node, rep_type, machine()->Float32Add(), false);
      break;
    }
1183
#define F32X4_BINOP_CASE(name)                                 \
1184
  case IrOpcode::kF32x4##name: {                               \
1185 1186 1187 1188 1189 1190 1191 1192 1193
    LowerBinaryOp(node, rep_type, machine()->Float32##name()); \
    break;                                                     \
  }
      F32X4_BINOP_CASE(Add)
      F32X4_BINOP_CASE(Sub)
      F32X4_BINOP_CASE(Mul)
      F32X4_BINOP_CASE(Min)
      F32X4_BINOP_CASE(Max)
#undef F32X4_BINOP_CASE
1194
#define F32X4_UNOP_CASE(name)                                 \
1195
  case IrOpcode::kF32x4##name: {                              \
1196 1197 1198 1199 1200 1201
    LowerUnaryOp(node, rep_type, machine()->Float32##name()); \
    break;                                                    \
  }
      F32X4_UNOP_CASE(Abs)
      F32X4_UNOP_CASE(Neg)
#undef F32x4_UNOP_CASE
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
    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;
    }
1218
    case IrOpcode::kF32x4SConvertI32x4: {
1219
      LowerUnaryOp(node, SimdType::kInt32x4, machine()->RoundInt32ToFloat32());
1220 1221
      break;
    }
1222
    case IrOpcode::kF32x4UConvertI32x4: {
1223
      LowerUnaryOp(node, SimdType::kInt32x4, machine()->RoundUint32ToFloat32());
1224 1225
      break;
    }
1226
    case IrOpcode::kI32x4Splat:
1227
    case IrOpcode::kF32x4Splat:
1228 1229
    case IrOpcode::kI16x8Splat:
    case IrOpcode::kI8x16Splat: {
1230 1231
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
1232 1233
        if (HasReplacement(0, node->InputAt(0))) {
          rep_node[i] = GetReplacements(node->InputAt(0))[0];
1234
        } else {
1235
          rep_node[i] = node->InputAt(0);
1236
        }
1237
      }
1238
      ReplaceNode(node, rep_node, num_lanes);
1239 1240
      break;
    }
1241
    case IrOpcode::kI32x4ExtractLane:
1242
    case IrOpcode::kF32x4ExtractLane:
1243 1244
    case IrOpcode::kI16x8ExtractLane:
    case IrOpcode::kI8x16ExtractLane: {
1245
      int32_t lane = OpParameter<int32_t>(node->op());
1246 1247 1248 1249 1250 1251
      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);
1252 1253
      break;
    }
1254
    case IrOpcode::kI32x4ReplaceLane:
1255
    case IrOpcode::kF32x4ReplaceLane:
1256 1257
    case IrOpcode::kI16x8ReplaceLane:
    case IrOpcode::kI8x16ReplaceLane: {
1258 1259
      DCHECK_EQ(2, node->InputCount());
      Node* repNode = node->InputAt(1);
1260
      int32_t lane = OpParameter<int32_t>(node->op());
1261 1262 1263 1264 1265
      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];
      }
1266 1267 1268 1269 1270
      if (HasReplacement(0, repNode)) {
        rep_node[lane] = GetReplacements(repNode)[0];
      } else {
        rep_node[lane] = repNode;
      }
1271
      ReplaceNode(node, rep_node, num_lanes);
1272 1273
      break;
    }
1274 1275 1276 1277
#define COMPARISON_CASE(type, simd_op, lowering_op, invert)                    \
  case IrOpcode::simd_op: {                                                    \
    LowerCompareOp(node, SimdType::k##type, machine()->lowering_op(), invert); \
    break;                                                                     \
1278
  }
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
      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)
1302 1303 1304 1305 1306 1307 1308 1309 1310
      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)
1311
#undef COMPARISON_CASE
1312
    case IrOpcode::kF32x4Ne: {
1313
      LowerNotEqual(node, SimdType::kFloat32x4, machine()->Float32Equal());
1314 1315
      break;
    }
1316
    case IrOpcode::kI32x4Ne: {
1317 1318 1319 1320 1321
      LowerNotEqual(node, SimdType::kInt32x4, machine()->Word32Equal());
      break;
    }
    case IrOpcode::kI16x8Ne: {
      LowerNotEqual(node, SimdType::kInt16x8, machine()->Word32Equal());
1322 1323
      break;
    }
1324 1325 1326 1327
    case IrOpcode::kI8x16Ne: {
      LowerNotEqual(node, SimdType::kInt8x16, machine()->Word32Equal());
      break;
    }
1328
    case IrOpcode::kS128Select: {
1329
      DCHECK_EQ(3, node->InputCount());
1330 1331 1332
      DCHECK(ReplacementType(node->InputAt(0)) == SimdType::kInt32x4 ||
             ReplacementType(node->InputAt(0)) == SimdType::kInt16x8 ||
             ReplacementType(node->InputAt(0)) == SimdType::kInt8x16);
1333 1334 1335
      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);
1336 1337
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
1338 1339 1340 1341 1342 1343
        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);
1344
      }
1345
      ReplaceNode(node, rep_node, num_lanes);
1346 1347
      break;
    }
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
    case IrOpcode::kS8x16Shuffle: {
      DCHECK_EQ(2, node->InputCount());
      const uint8_t* shuffle = OpParameter<uint8_t*>(node->op());
      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;
    }
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    case IrOpcode::kS1x4AnyTrue:
    case IrOpcode::kS1x4AllTrue:
    case IrOpcode::kS1x8AnyTrue:
    case IrOpcode::kS1x8AllTrue:
    case IrOpcode::kS1x16AnyTrue:
    case IrOpcode::kS1x16AllTrue: {
      DCHECK_EQ(1, node->InputCount());
      SimdType input_rep_type = ReplacementType(node->InputAt(0));
      int input_num_lanes = NumLanes(input_rep_type);
      Node** rep = GetReplacements(node->InputAt(0));
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
1372 1373
      Node* true_node = mcgraph_->Int32Constant(-1);
      Node* false_node = mcgraph_->Int32Constant(0);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
      Node* tmp_result = false_node;
      if (node->opcode() == IrOpcode::kS1x4AllTrue ||
          node->opcode() == IrOpcode::kS1x8AllTrue ||
          node->opcode() == IrOpcode::kS1x16AllTrue) {
        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));
        if (node->opcode() == IrOpcode::kS1x4AllTrue ||
            node->opcode() == IrOpcode::kS1x8AllTrue ||
            node->opcode() == IrOpcode::kS1x16AllTrue) {
          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;
    }
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    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]);
    }
    if (HasReplacement(1, input)) {
      something_changed = true;
1415
      for (int j = 1; j < ReplacementCount(input); ++j) {
1416 1417 1418 1419 1420 1421 1422
        node->InsertInput(zone(), i + j, GetReplacements(input)[j]);
      }
    }
  }
  return something_changed;
}

1423 1424 1425 1426
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];
1427
  }
1428
  replacements_[old->id()].num_replacements = count;
1429 1430 1431
}

bool SimdScalarLowering::HasReplacement(size_t index, Node* node) {
1432 1433
  return replacements_[node->id()].node != nullptr &&
         replacements_[node->id()].node[index] != nullptr;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
}

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;
}

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
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;
    }
  }
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
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);
      }
    }
  }
}

1525 1526 1527 1528 1529
Node** SimdScalarLowering::GetReplacementsWithType(Node* node, SimdType type) {
  Node** replacements = GetReplacements(node);
  if (ReplacementType(node) == type) {
    return GetReplacements(node);
  }
1530 1531 1532 1533 1534 1535
  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) {
1536 1537 1538
      SmallerIntToInt32<int16_t>(replacements, result);
    } else if (ReplacementType(node) == SimdType::kInt8x16) {
      SmallerIntToInt32<int8_t>(replacements, result);
1539 1540
    } else {
      UNREACHABLE();
1541
    }
1542 1543 1544 1545 1546 1547 1548 1549 1550
  } else if (type == SimdType::kFloat32x4) {
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToFloat32(replacements, result);
    } else if (ReplacementType(node) == SimdType::kInt16x8) {
      UNIMPLEMENTED();
    } else {
      UNREACHABLE();
    }
  } else if (type == SimdType::kInt16x8) {
1551 1552 1553
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToSmallerInt<int16_t>(replacements, result);
    } else if (ReplacementType(node) == SimdType::kFloat32x4) {
1554 1555 1556
      UNIMPLEMENTED();
    } else {
      UNREACHABLE();
1557
    }
1558 1559 1560 1561 1562 1563
  } else if (type == SimdType::kInt8x16) {
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToSmallerInt<int8_t>(replacements, result);
    } else {
      UNIMPLEMENTED();
    }
1564 1565
  } else {
    UNREACHABLE();
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
  }
  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);
1579 1580 1581
    int num_lanes = NumLanes(type);
    Node*** inputs_rep = zone()->NewArray<Node**>(num_lanes);
    for (int i = 0; i < num_lanes; ++i) {
1582 1583 1584
      inputs_rep[i] = zone()->NewArray<Node*>(value_count + 1);
      inputs_rep[i][value_count] = NodeProperties::GetControlInput(phi, 0);
    }
1585
    for (int i = 0; i < value_count; ++i) {
1586
      for (int j = 0; j < num_lanes; ++j) {
1587 1588 1589
        inputs_rep[j][i] = placeholder_;
      }
    }
1590 1591
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
    for (int i = 0; i < num_lanes; ++i) {
1592 1593 1594
      rep_nodes[i] = graph()->NewNode(
          common()->Phi(MachineTypeFrom(type).representation(), value_count),
          value_count + 1, inputs_rep[i], false);
1595
    }
1596
    ReplaceNode(phi, rep_nodes, num_lanes);
1597 1598 1599 1600 1601
  }
}
}  // namespace compiler
}  // namespace internal
}  // namespace v8