blob: 72661af4c381f810fcb04a6e546bb7f29a66f6e8 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/compiler/instruction-selector-impl.h"
6#include "src/compiler/node-matchers.h"
7
8namespace v8 {
9namespace internal {
10namespace compiler {
11
12enum ImmediateMode {
13 kArithmeticImm, // 12 bit unsigned immediate shifted left 0 or 12 bits
14 kShift32Imm, // 0 - 31
15 kShift64Imm, // 0 - 63
16 kLogical32Imm,
17 kLogical64Imm,
18 kLoadStoreImm8, // signed 8 bit or 12 bit unsigned scaled by access size
19 kLoadStoreImm16,
20 kLoadStoreImm32,
21 kLoadStoreImm64,
22 kNoImmediate
23};
24
25
26// Adds Arm64-specific methods for generating operands.
27class Arm64OperandGenerator FINAL : public OperandGenerator {
28 public:
29 explicit Arm64OperandGenerator(InstructionSelector* selector)
30 : OperandGenerator(selector) {}
31
32 InstructionOperand* UseOperand(Node* node, ImmediateMode mode) {
33 if (CanBeImmediate(node, mode)) {
34 return UseImmediate(node);
35 }
36 return UseRegister(node);
37 }
38
39 bool CanBeImmediate(Node* node, ImmediateMode mode) {
40 int64_t value;
41 if (node->opcode() == IrOpcode::kInt32Constant)
42 value = OpParameter<int32_t>(node);
43 else if (node->opcode() == IrOpcode::kInt64Constant)
44 value = OpParameter<int64_t>(node);
45 else
46 return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 return CanBeImmediate(value, mode);
48 }
49
50 bool CanBeImmediate(int64_t value, ImmediateMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 unsigned ignored;
52 switch (mode) {
53 case kLogical32Imm:
54 // TODO(dcarney): some unencodable values can be handled by
55 // switching instructions.
56 return Assembler::IsImmLogical(static_cast<uint64_t>(value), 32,
57 &ignored, &ignored, &ignored);
58 case kLogical64Imm:
59 return Assembler::IsImmLogical(static_cast<uint64_t>(value), 64,
60 &ignored, &ignored, &ignored);
61 case kArithmeticImm:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 return Assembler::IsImmAddSub(value);
63 case kShift32Imm:
64 return 0 <= value && value < 32;
65 case kShift64Imm:
66 return 0 <= value && value < 64;
67 case kLoadStoreImm8:
68 return IsLoadStoreImmediate(value, LSByte);
69 case kLoadStoreImm16:
70 return IsLoadStoreImmediate(value, LSHalfword);
71 case kLoadStoreImm32:
72 return IsLoadStoreImmediate(value, LSWord);
73 case kLoadStoreImm64:
74 return IsLoadStoreImmediate(value, LSDoubleWord);
75 case kNoImmediate:
76 return false;
77 }
78 return false;
79 }
80
81 private:
82 bool IsLoadStoreImmediate(int64_t value, LSDataSize size) {
83 return Assembler::IsImmLSScaled(value, size) ||
84 Assembler::IsImmLSUnscaled(value);
85 }
86};
87
88
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089static void VisitRRFloat64(InstructionSelector* selector, ArchOpcode opcode,
90 Node* node) {
91 Arm64OperandGenerator g(selector);
92 selector->Emit(opcode, g.DefineAsRegister(node),
93 g.UseRegister(node->InputAt(0)));
94}
95
96
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097static void VisitRRR(InstructionSelector* selector, ArchOpcode opcode,
98 Node* node) {
99 Arm64OperandGenerator g(selector);
100 selector->Emit(opcode, g.DefineAsRegister(node),
101 g.UseRegister(node->InputAt(0)),
102 g.UseRegister(node->InputAt(1)));
103}
104
105
106static void VisitRRRFloat64(InstructionSelector* selector, ArchOpcode opcode,
107 Node* node) {
108 Arm64OperandGenerator g(selector);
109 selector->Emit(opcode, g.DefineAsRegister(node),
110 g.UseRegister(node->InputAt(0)),
111 g.UseRegister(node->InputAt(1)));
112}
113
114
115static void VisitRRO(InstructionSelector* selector, ArchOpcode opcode,
116 Node* node, ImmediateMode operand_mode) {
117 Arm64OperandGenerator g(selector);
118 selector->Emit(opcode, g.DefineAsRegister(node),
119 g.UseRegister(node->InputAt(0)),
120 g.UseOperand(node->InputAt(1), operand_mode));
121}
122
123
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124template <typename Matcher>
125static bool TryMatchShift(InstructionSelector* selector, Node* node,
126 InstructionCode* opcode, IrOpcode::Value shift_opcode,
127 ImmediateMode imm_mode,
128 AddressingMode addressing_mode) {
129 if (node->opcode() != shift_opcode) return false;
130 Arm64OperandGenerator g(selector);
131 Matcher m(node);
132 if (g.CanBeImmediate(m.right().node(), imm_mode)) {
133 *opcode |= AddressingModeField::encode(addressing_mode);
134 return true;
135 }
136 return false;
137}
138
139
140static bool TryMatchAnyShift(InstructionSelector* selector, Node* node,
141 InstructionCode* opcode, bool try_ror) {
142 return TryMatchShift<Int32BinopMatcher>(selector, node, opcode,
143 IrOpcode::kWord32Shl, kShift32Imm,
144 kMode_Operand2_R_LSL_I) ||
145 TryMatchShift<Int32BinopMatcher>(selector, node, opcode,
146 IrOpcode::kWord32Shr, kShift32Imm,
147 kMode_Operand2_R_LSR_I) ||
148 TryMatchShift<Int32BinopMatcher>(selector, node, opcode,
149 IrOpcode::kWord32Sar, kShift32Imm,
150 kMode_Operand2_R_ASR_I) ||
151 (try_ror && TryMatchShift<Int32BinopMatcher>(
152 selector, node, opcode, IrOpcode::kWord32Ror,
153 kShift32Imm, kMode_Operand2_R_ROR_I)) ||
154 TryMatchShift<Int64BinopMatcher>(selector, node, opcode,
155 IrOpcode::kWord64Shl, kShift64Imm,
156 kMode_Operand2_R_LSL_I) ||
157 TryMatchShift<Int64BinopMatcher>(selector, node, opcode,
158 IrOpcode::kWord64Shr, kShift64Imm,
159 kMode_Operand2_R_LSR_I) ||
160 TryMatchShift<Int64BinopMatcher>(selector, node, opcode,
161 IrOpcode::kWord64Sar, kShift64Imm,
162 kMode_Operand2_R_ASR_I) ||
163 (try_ror && TryMatchShift<Int64BinopMatcher>(
164 selector, node, opcode, IrOpcode::kWord64Ror,
165 kShift64Imm, kMode_Operand2_R_ROR_I));
166}
167
168
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169// Shared routine for multiple binary operations.
170template <typename Matcher>
171static void VisitBinop(InstructionSelector* selector, Node* node,
172 InstructionCode opcode, ImmediateMode operand_mode,
173 FlagsContinuation* cont) {
174 Arm64OperandGenerator g(selector);
175 Matcher m(node);
176 InstructionOperand* inputs[4];
177 size_t input_count = 0;
178 InstructionOperand* outputs[2];
179 size_t output_count = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400180 bool try_ror_operand = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400182 if (m.IsInt32Add() || m.IsInt64Add() || m.IsInt32Sub() || m.IsInt64Sub()) {
183 try_ror_operand = false;
184 }
185
186 if (g.CanBeImmediate(m.right().node(), operand_mode)) {
187 inputs[input_count++] = g.UseRegister(m.left().node());
188 inputs[input_count++] = g.UseImmediate(m.right().node());
189 } else if (TryMatchAnyShift(selector, m.right().node(), &opcode,
190 try_ror_operand)) {
191 Matcher m_shift(m.right().node());
192 inputs[input_count++] = g.UseRegister(m.left().node());
193 inputs[input_count++] = g.UseRegister(m_shift.left().node());
194 inputs[input_count++] = g.UseImmediate(m_shift.right().node());
195 } else if (m.HasProperty(Operator::kCommutative) &&
196 TryMatchAnyShift(selector, m.left().node(), &opcode,
197 try_ror_operand)) {
198 Matcher m_shift(m.left().node());
199 inputs[input_count++] = g.UseRegister(m.right().node());
200 inputs[input_count++] = g.UseRegister(m_shift.left().node());
201 inputs[input_count++] = g.UseImmediate(m_shift.right().node());
202 } else {
203 inputs[input_count++] = g.UseRegister(m.left().node());
204 inputs[input_count++] = g.UseRegister(m.right().node());
205 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206
207 if (cont->IsBranch()) {
208 inputs[input_count++] = g.Label(cont->true_block());
209 inputs[input_count++] = g.Label(cont->false_block());
210 }
211
212 outputs[output_count++] = g.DefineAsRegister(node);
213 if (cont->IsSet()) {
214 outputs[output_count++] = g.DefineAsRegister(cont->result());
215 }
216
217 DCHECK_NE(0, input_count);
218 DCHECK_NE(0, output_count);
219 DCHECK_GE(arraysize(inputs), input_count);
220 DCHECK_GE(arraysize(outputs), output_count);
221
222 Instruction* instr = selector->Emit(cont->Encode(opcode), output_count,
223 outputs, input_count, inputs);
224 if (cont->IsBranch()) instr->MarkAsControl();
225}
226
227
228// Shared routine for multiple binary operations.
229template <typename Matcher>
230static void VisitBinop(InstructionSelector* selector, Node* node,
231 ArchOpcode opcode, ImmediateMode operand_mode) {
232 FlagsContinuation cont;
233 VisitBinop<Matcher>(selector, node, opcode, operand_mode, &cont);
234}
235
236
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400237template <typename Matcher>
238static void VisitAddSub(InstructionSelector* selector, Node* node,
239 ArchOpcode opcode, ArchOpcode negate_opcode) {
240 Arm64OperandGenerator g(selector);
241 Matcher m(node);
242 if (m.right().HasValue() && (m.right().Value() < 0) &&
243 g.CanBeImmediate(-m.right().Value(), kArithmeticImm)) {
244 selector->Emit(negate_opcode, g.DefineAsRegister(node),
245 g.UseRegister(m.left().node()),
246 g.TempImmediate(-m.right().Value()));
247 } else {
248 VisitBinop<Matcher>(selector, node, opcode, kArithmeticImm);
249 }
250}
251
252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253void InstructionSelector::VisitLoad(Node* node) {
254 MachineType rep = RepresentationOf(OpParameter<LoadRepresentation>(node));
255 MachineType typ = TypeOf(OpParameter<LoadRepresentation>(node));
256 Arm64OperandGenerator g(this);
257 Node* base = node->InputAt(0);
258 Node* index = node->InputAt(1);
259 ArchOpcode opcode;
260 ImmediateMode immediate_mode = kNoImmediate;
261 switch (rep) {
262 case kRepFloat32:
263 opcode = kArm64LdrS;
264 immediate_mode = kLoadStoreImm32;
265 break;
266 case kRepFloat64:
267 opcode = kArm64LdrD;
268 immediate_mode = kLoadStoreImm64;
269 break;
270 case kRepBit: // Fall through.
271 case kRepWord8:
272 opcode = typ == kTypeInt32 ? kArm64Ldrsb : kArm64Ldrb;
273 immediate_mode = kLoadStoreImm8;
274 break;
275 case kRepWord16:
276 opcode = typ == kTypeInt32 ? kArm64Ldrsh : kArm64Ldrh;
277 immediate_mode = kLoadStoreImm16;
278 break;
279 case kRepWord32:
280 opcode = kArm64LdrW;
281 immediate_mode = kLoadStoreImm32;
282 break;
283 case kRepTagged: // Fall through.
284 case kRepWord64:
285 opcode = kArm64Ldr;
286 immediate_mode = kLoadStoreImm64;
287 break;
288 default:
289 UNREACHABLE();
290 return;
291 }
292 if (g.CanBeImmediate(index, immediate_mode)) {
293 Emit(opcode | AddressingModeField::encode(kMode_MRI),
294 g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index));
295 } else {
296 Emit(opcode | AddressingModeField::encode(kMode_MRR),
297 g.DefineAsRegister(node), g.UseRegister(base), g.UseRegister(index));
298 }
299}
300
301
302void InstructionSelector::VisitStore(Node* node) {
303 Arm64OperandGenerator g(this);
304 Node* base = node->InputAt(0);
305 Node* index = node->InputAt(1);
306 Node* value = node->InputAt(2);
307
308 StoreRepresentation store_rep = OpParameter<StoreRepresentation>(node);
309 MachineType rep = RepresentationOf(store_rep.machine_type());
310 if (store_rep.write_barrier_kind() == kFullWriteBarrier) {
311 DCHECK(rep == kRepTagged);
312 // TODO(dcarney): refactor RecordWrite function to take temp registers
313 // and pass them here instead of using fixed regs
314 // TODO(dcarney): handle immediate indices.
315 InstructionOperand* temps[] = {g.TempRegister(x11), g.TempRegister(x12)};
316 Emit(kArm64StoreWriteBarrier, NULL, g.UseFixed(base, x10),
317 g.UseFixed(index, x11), g.UseFixed(value, x12), arraysize(temps),
318 temps);
319 return;
320 }
321 DCHECK_EQ(kNoWriteBarrier, store_rep.write_barrier_kind());
322 ArchOpcode opcode;
323 ImmediateMode immediate_mode = kNoImmediate;
324 switch (rep) {
325 case kRepFloat32:
326 opcode = kArm64StrS;
327 immediate_mode = kLoadStoreImm32;
328 break;
329 case kRepFloat64:
330 opcode = kArm64StrD;
331 immediate_mode = kLoadStoreImm64;
332 break;
333 case kRepBit: // Fall through.
334 case kRepWord8:
335 opcode = kArm64Strb;
336 immediate_mode = kLoadStoreImm8;
337 break;
338 case kRepWord16:
339 opcode = kArm64Strh;
340 immediate_mode = kLoadStoreImm16;
341 break;
342 case kRepWord32:
343 opcode = kArm64StrW;
344 immediate_mode = kLoadStoreImm32;
345 break;
346 case kRepTagged: // Fall through.
347 case kRepWord64:
348 opcode = kArm64Str;
349 immediate_mode = kLoadStoreImm64;
350 break;
351 default:
352 UNREACHABLE();
353 return;
354 }
355 if (g.CanBeImmediate(index, immediate_mode)) {
356 Emit(opcode | AddressingModeField::encode(kMode_MRI), NULL,
357 g.UseRegister(base), g.UseImmediate(index), g.UseRegister(value));
358 } else {
359 Emit(opcode | AddressingModeField::encode(kMode_MRR), NULL,
360 g.UseRegister(base), g.UseRegister(index), g.UseRegister(value));
361 }
362}
363
364
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400365void InstructionSelector::VisitCheckedLoad(Node* node) {
366 MachineType rep = RepresentationOf(OpParameter<MachineType>(node));
367 MachineType typ = TypeOf(OpParameter<MachineType>(node));
368 Arm64OperandGenerator g(this);
369 Node* const buffer = node->InputAt(0);
370 Node* const offset = node->InputAt(1);
371 Node* const length = node->InputAt(2);
372 ArchOpcode opcode;
373 switch (rep) {
374 case kRepWord8:
375 opcode = typ == kTypeInt32 ? kCheckedLoadInt8 : kCheckedLoadUint8;
376 break;
377 case kRepWord16:
378 opcode = typ == kTypeInt32 ? kCheckedLoadInt16 : kCheckedLoadUint16;
379 break;
380 case kRepWord32:
381 opcode = kCheckedLoadWord32;
382 break;
383 case kRepFloat32:
384 opcode = kCheckedLoadFloat32;
385 break;
386 case kRepFloat64:
387 opcode = kCheckedLoadFloat64;
388 break;
389 default:
390 UNREACHABLE();
391 return;
392 }
393 Emit(opcode, g.DefineAsRegister(node), g.UseRegister(buffer),
394 g.UseRegister(offset), g.UseOperand(length, kArithmeticImm));
395}
396
397
398void InstructionSelector::VisitCheckedStore(Node* node) {
399 MachineType rep = RepresentationOf(OpParameter<MachineType>(node));
400 Arm64OperandGenerator g(this);
401 Node* const buffer = node->InputAt(0);
402 Node* const offset = node->InputAt(1);
403 Node* const length = node->InputAt(2);
404 Node* const value = node->InputAt(3);
405 ArchOpcode opcode;
406 switch (rep) {
407 case kRepWord8:
408 opcode = kCheckedStoreWord8;
409 break;
410 case kRepWord16:
411 opcode = kCheckedStoreWord16;
412 break;
413 case kRepWord32:
414 opcode = kCheckedStoreWord32;
415 break;
416 case kRepFloat32:
417 opcode = kCheckedStoreFloat32;
418 break;
419 case kRepFloat64:
420 opcode = kCheckedStoreFloat64;
421 break;
422 default:
423 UNREACHABLE();
424 return;
425 }
426 Emit(opcode, nullptr, g.UseRegister(buffer), g.UseRegister(offset),
427 g.UseOperand(length, kArithmeticImm), g.UseRegister(value));
428}
429
430
431template <typename Matcher>
432static void VisitLogical(InstructionSelector* selector, Node* node, Matcher* m,
433 ArchOpcode opcode, bool left_can_cover,
434 bool right_can_cover, ImmediateMode imm_mode) {
435 Arm64OperandGenerator g(selector);
436
437 // Map instruction to equivalent operation with inverted right input.
438 ArchOpcode inv_opcode = opcode;
439 switch (opcode) {
440 case kArm64And32:
441 inv_opcode = kArm64Bic32;
442 break;
443 case kArm64And:
444 inv_opcode = kArm64Bic;
445 break;
446 case kArm64Or32:
447 inv_opcode = kArm64Orn32;
448 break;
449 case kArm64Or:
450 inv_opcode = kArm64Orn;
451 break;
452 case kArm64Eor32:
453 inv_opcode = kArm64Eon32;
454 break;
455 case kArm64Eor:
456 inv_opcode = kArm64Eon;
457 break;
458 default:
459 UNREACHABLE();
460 }
461
462 // Select Logical(y, ~x) for Logical(Xor(x, -1), y).
463 if ((m->left().IsWord32Xor() || m->left().IsWord64Xor()) && left_can_cover) {
464 Matcher mleft(m->left().node());
465 if (mleft.right().Is(-1)) {
466 // TODO(all): support shifted operand on right.
467 selector->Emit(inv_opcode, g.DefineAsRegister(node),
468 g.UseRegister(m->right().node()),
469 g.UseRegister(mleft.left().node()));
470 return;
471 }
472 }
473
474 // Select Logical(x, ~y) for Logical(x, Xor(y, -1)).
475 if ((m->right().IsWord32Xor() || m->right().IsWord64Xor()) &&
476 right_can_cover) {
477 Matcher mright(m->right().node());
478 if (mright.right().Is(-1)) {
479 // TODO(all): support shifted operand on right.
480 selector->Emit(inv_opcode, g.DefineAsRegister(node),
481 g.UseRegister(m->left().node()),
482 g.UseRegister(mright.left().node()));
483 return;
484 }
485 }
486
487 if (m->IsWord32Xor() && m->right().Is(-1)) {
488 selector->Emit(kArm64Not32, g.DefineAsRegister(node),
489 g.UseRegister(m->left().node()));
490 } else if (m->IsWord64Xor() && m->right().Is(-1)) {
491 selector->Emit(kArm64Not, g.DefineAsRegister(node),
492 g.UseRegister(m->left().node()));
493 } else {
494 VisitBinop<Matcher>(selector, node, opcode, imm_mode);
495 }
496}
497
498
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499void InstructionSelector::VisitWord32And(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400500 Arm64OperandGenerator g(this);
501 Int32BinopMatcher m(node);
502 if (m.left().IsWord32Shr() && CanCover(node, m.left().node()) &&
503 m.right().HasValue()) {
504 uint32_t mask = m.right().Value();
505 uint32_t mask_width = base::bits::CountPopulation32(mask);
506 uint32_t mask_msb = base::bits::CountLeadingZeros32(mask);
507 if ((mask_width != 0) && (mask_msb + mask_width == 32)) {
508 // The mask must be contiguous, and occupy the least-significant bits.
509 DCHECK_EQ(0, base::bits::CountTrailingZeros32(mask));
510
511 // Select Ubfx for And(Shr(x, imm), mask) where the mask is in the least
512 // significant bits.
513 Int32BinopMatcher mleft(m.left().node());
514 if (mleft.right().IsInRange(0, 31)) {
515 // Ubfx cannot extract bits past the register size, however since
516 // shifting the original value would have introduced some zeros we can
517 // still use ubfx with a smaller mask and the remaining bits will be
518 // zeros.
519 uint32_t lsb = mleft.right().Value();
520 if (lsb + mask_width > 32) mask_width = 32 - lsb;
521
522 Emit(kArm64Ubfx32, g.DefineAsRegister(node),
523 g.UseRegister(mleft.left().node()),
524 g.UseImmediate(mleft.right().node()), g.TempImmediate(mask_width));
525 return;
526 }
527 // Other cases fall through to the normal And operation.
528 }
529 }
530 VisitLogical<Int32BinopMatcher>(
531 this, node, &m, kArm64And32, CanCover(node, m.left().node()),
532 CanCover(node, m.right().node()), kLogical32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533}
534
535
536void InstructionSelector::VisitWord64And(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400537 Arm64OperandGenerator g(this);
538 Int64BinopMatcher m(node);
539 if (m.left().IsWord64Shr() && CanCover(node, m.left().node()) &&
540 m.right().HasValue()) {
541 uint64_t mask = m.right().Value();
542 uint64_t mask_width = base::bits::CountPopulation64(mask);
543 uint64_t mask_msb = base::bits::CountLeadingZeros64(mask);
544 if ((mask_width != 0) && (mask_msb + mask_width == 64)) {
545 // The mask must be contiguous, and occupy the least-significant bits.
546 DCHECK_EQ(0, base::bits::CountTrailingZeros64(mask));
547
548 // Select Ubfx for And(Shr(x, imm), mask) where the mask is in the least
549 // significant bits.
550 Int64BinopMatcher mleft(m.left().node());
551 if (mleft.right().IsInRange(0, 63)) {
552 // Ubfx cannot extract bits past the register size, however since
553 // shifting the original value would have introduced some zeros we can
554 // still use ubfx with a smaller mask and the remaining bits will be
555 // zeros.
556 uint64_t lsb = mleft.right().Value();
557 if (lsb + mask_width > 64) mask_width = 64 - lsb;
558
559 Emit(kArm64Ubfx, g.DefineAsRegister(node),
560 g.UseRegister(mleft.left().node()),
561 g.UseImmediate(mleft.right().node()), g.TempImmediate(mask_width));
562 return;
563 }
564 // Other cases fall through to the normal And operation.
565 }
566 }
567 VisitLogical<Int64BinopMatcher>(
568 this, node, &m, kArm64And, CanCover(node, m.left().node()),
569 CanCover(node, m.right().node()), kLogical64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570}
571
572
573void InstructionSelector::VisitWord32Or(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400574 Int32BinopMatcher m(node);
575 VisitLogical<Int32BinopMatcher>(
576 this, node, &m, kArm64Or32, CanCover(node, m.left().node()),
577 CanCover(node, m.right().node()), kLogical32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578}
579
580
581void InstructionSelector::VisitWord64Or(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400582 Int64BinopMatcher m(node);
583 VisitLogical<Int64BinopMatcher>(
584 this, node, &m, kArm64Or, CanCover(node, m.left().node()),
585 CanCover(node, m.right().node()), kLogical64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586}
587
588
589void InstructionSelector::VisitWord32Xor(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 Int32BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400591 VisitLogical<Int32BinopMatcher>(
592 this, node, &m, kArm64Eor32, CanCover(node, m.left().node()),
593 CanCover(node, m.right().node()), kLogical32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594}
595
596
597void InstructionSelector::VisitWord64Xor(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 Int64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400599 VisitLogical<Int64BinopMatcher>(
600 this, node, &m, kArm64Eor, CanCover(node, m.left().node()),
601 CanCover(node, m.right().node()), kLogical64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602}
603
604
605void InstructionSelector::VisitWord32Shl(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400606 VisitRRO(this, kArm64Lsl32, node, kShift32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607}
608
609
610void InstructionSelector::VisitWord64Shl(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400611 Arm64OperandGenerator g(this);
612 Int64BinopMatcher m(node);
613 if ((m.left().IsChangeInt32ToInt64() || m.left().IsChangeUint32ToUint64()) &&
614 m.right().IsInRange(32, 63)) {
615 // There's no need to sign/zero-extend to 64-bit if we shift out the upper
616 // 32 bits anyway.
617 Emit(kArm64Lsl, g.DefineAsRegister(node),
618 g.UseRegister(m.left().node()->InputAt(0)),
619 g.UseImmediate(m.right().node()));
620 return;
621 }
622 VisitRRO(this, kArm64Lsl, node, kShift64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623}
624
625
626void InstructionSelector::VisitWord32Shr(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400627 Arm64OperandGenerator g(this);
628 Int32BinopMatcher m(node);
629 if (m.left().IsWord32And() && m.right().IsInRange(0, 31)) {
630 int32_t lsb = m.right().Value();
631 Int32BinopMatcher mleft(m.left().node());
632 if (mleft.right().HasValue()) {
633 uint32_t mask = (mleft.right().Value() >> lsb) << lsb;
634 uint32_t mask_width = base::bits::CountPopulation32(mask);
635 uint32_t mask_msb = base::bits::CountLeadingZeros32(mask);
636 // Select Ubfx for Shr(And(x, mask), imm) where the result of the mask is
637 // shifted into the least-significant bits.
638 if ((mask_msb + mask_width + lsb) == 32) {
639 DCHECK_EQ(lsb, base::bits::CountTrailingZeros32(mask));
640 Emit(kArm64Ubfx32, g.DefineAsRegister(node),
641 g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
642 g.TempImmediate(mask_width));
643 return;
644 }
645 }
646 }
647 VisitRRO(this, kArm64Lsr32, node, kShift32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648}
649
650
651void InstructionSelector::VisitWord64Shr(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400652 Arm64OperandGenerator g(this);
653 Int64BinopMatcher m(node);
654 if (m.left().IsWord64And() && m.right().IsInRange(0, 63)) {
655 int64_t lsb = m.right().Value();
656 Int64BinopMatcher mleft(m.left().node());
657 if (mleft.right().HasValue()) {
658 // Select Ubfx for Shr(And(x, mask), imm) where the result of the mask is
659 // shifted into the least-significant bits.
660 uint64_t mask = (mleft.right().Value() >> lsb) << lsb;
661 uint64_t mask_width = base::bits::CountPopulation64(mask);
662 uint64_t mask_msb = base::bits::CountLeadingZeros64(mask);
663 if ((mask_msb + mask_width + lsb) == 64) {
664 DCHECK_EQ(lsb, base::bits::CountTrailingZeros64(mask));
665 Emit(kArm64Ubfx, g.DefineAsRegister(node),
666 g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
667 g.TempImmediate(mask_width));
668 return;
669 }
670 }
671 }
672 VisitRRO(this, kArm64Lsr, node, kShift64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673}
674
675
676void InstructionSelector::VisitWord32Sar(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400677 Arm64OperandGenerator g(this);
678 Int32BinopMatcher m(node);
679 // Select Sxth/Sxtb for (x << K) >> K where K is 16 or 24.
680 if (CanCover(node, m.left().node()) && m.left().IsWord32Shl()) {
681 Int32BinopMatcher mleft(m.left().node());
682 if (mleft.right().Is(16) && m.right().Is(16)) {
683 Emit(kArm64Sxth32, g.DefineAsRegister(node),
684 g.UseRegister(mleft.left().node()));
685 return;
686 } else if (mleft.right().Is(24) && m.right().Is(24)) {
687 Emit(kArm64Sxtb32, g.DefineAsRegister(node),
688 g.UseRegister(mleft.left().node()));
689 return;
690 }
691 }
692 VisitRRO(this, kArm64Asr32, node, kShift32Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693}
694
695
696void InstructionSelector::VisitWord64Sar(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400697 VisitRRO(this, kArm64Asr, node, kShift64Imm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698}
699
700
701void InstructionSelector::VisitWord32Ror(Node* node) {
702 VisitRRO(this, kArm64Ror32, node, kShift32Imm);
703}
704
705
706void InstructionSelector::VisitWord64Ror(Node* node) {
707 VisitRRO(this, kArm64Ror, node, kShift64Imm);
708}
709
710
711void InstructionSelector::VisitInt32Add(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400712 Arm64OperandGenerator g(this);
713 Int32BinopMatcher m(node);
714 // Select Madd(x, y, z) for Add(Mul(x, y), z).
715 if (m.left().IsInt32Mul() && CanCover(node, m.left().node())) {
716 Int32BinopMatcher mleft(m.left().node());
717 Emit(kArm64Madd32, g.DefineAsRegister(node),
718 g.UseRegister(mleft.left().node()),
719 g.UseRegister(mleft.right().node()), g.UseRegister(m.right().node()));
720 return;
721 }
722 // Select Madd(x, y, z) for Add(x, Mul(x, y)).
723 if (m.right().IsInt32Mul() && CanCover(node, m.right().node())) {
724 Int32BinopMatcher mright(m.right().node());
725 Emit(kArm64Madd32, g.DefineAsRegister(node),
726 g.UseRegister(mright.left().node()),
727 g.UseRegister(mright.right().node()), g.UseRegister(m.left().node()));
728 return;
729 }
730 VisitAddSub<Int32BinopMatcher>(this, node, kArm64Add32, kArm64Sub32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731}
732
733
734void InstructionSelector::VisitInt64Add(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400735 Arm64OperandGenerator g(this);
736 Int64BinopMatcher m(node);
737 // Select Madd(x, y, z) for Add(Mul(x, y), z).
738 if (m.left().IsInt64Mul() && CanCover(node, m.left().node())) {
739 Int64BinopMatcher mleft(m.left().node());
740 Emit(kArm64Madd, g.DefineAsRegister(node),
741 g.UseRegister(mleft.left().node()),
742 g.UseRegister(mleft.right().node()), g.UseRegister(m.right().node()));
743 return;
744 }
745 // Select Madd(x, y, z) for Add(x, Mul(x, y)).
746 if (m.right().IsInt64Mul() && CanCover(node, m.right().node())) {
747 Int64BinopMatcher mright(m.right().node());
748 Emit(kArm64Madd, g.DefineAsRegister(node),
749 g.UseRegister(mright.left().node()),
750 g.UseRegister(mright.right().node()), g.UseRegister(m.left().node()));
751 return;
752 }
753 VisitAddSub<Int64BinopMatcher>(this, node, kArm64Add, kArm64Sub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754}
755
756
757void InstructionSelector::VisitInt32Sub(Node* node) {
758 Arm64OperandGenerator g(this);
759 Int32BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400760
761 // Select Msub(a, x, y) for Sub(a, Mul(x, y)).
762 if (m.right().IsInt32Mul() && CanCover(node, m.right().node())) {
763 Int32BinopMatcher mright(m.right().node());
764 Emit(kArm64Msub32, g.DefineAsRegister(node),
765 g.UseRegister(mright.left().node()),
766 g.UseRegister(mright.right().node()), g.UseRegister(m.left().node()));
767 return;
768 }
769
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000770 if (m.left().Is(0)) {
771 Emit(kArm64Neg32, g.DefineAsRegister(node),
772 g.UseRegister(m.right().node()));
773 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400774 VisitAddSub<Int32BinopMatcher>(this, node, kArm64Sub32, kArm64Add32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 }
776}
777
778
779void InstructionSelector::VisitInt64Sub(Node* node) {
780 Arm64OperandGenerator g(this);
781 Int64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400782
783 // Select Msub(a, x, y) for Sub(a, Mul(x, y)).
784 if (m.right().IsInt64Mul() && CanCover(node, m.right().node())) {
785 Int64BinopMatcher mright(m.right().node());
786 Emit(kArm64Msub, g.DefineAsRegister(node),
787 g.UseRegister(mright.left().node()),
788 g.UseRegister(mright.right().node()), g.UseRegister(m.left().node()));
789 return;
790 }
791
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792 if (m.left().Is(0)) {
793 Emit(kArm64Neg, g.DefineAsRegister(node), g.UseRegister(m.right().node()));
794 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400795 VisitAddSub<Int64BinopMatcher>(this, node, kArm64Sub, kArm64Add);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796 }
797}
798
799
800void InstructionSelector::VisitInt32Mul(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400801 Arm64OperandGenerator g(this);
802 Int32BinopMatcher m(node);
803
804 if (m.left().IsInt32Sub() && CanCover(node, m.left().node())) {
805 Int32BinopMatcher mleft(m.left().node());
806
807 // Select Mneg(x, y) for Mul(Sub(0, x), y).
808 if (mleft.left().Is(0)) {
809 Emit(kArm64Mneg32, g.DefineAsRegister(node),
810 g.UseRegister(mleft.right().node()),
811 g.UseRegister(m.right().node()));
812 return;
813 }
814 }
815
816 if (m.right().IsInt32Sub() && CanCover(node, m.right().node())) {
817 Int32BinopMatcher mright(m.right().node());
818
819 // Select Mneg(x, y) for Mul(x, Sub(0, y)).
820 if (mright.left().Is(0)) {
821 Emit(kArm64Mneg32, g.DefineAsRegister(node),
822 g.UseRegister(m.left().node()),
823 g.UseRegister(mright.right().node()));
824 return;
825 }
826 }
827
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 VisitRRR(this, kArm64Mul32, node);
829}
830
831
832void InstructionSelector::VisitInt64Mul(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400833 Arm64OperandGenerator g(this);
834 Int64BinopMatcher m(node);
835
836 if (m.left().IsInt64Sub() && CanCover(node, m.left().node())) {
837 Int64BinopMatcher mleft(m.left().node());
838
839 // Select Mneg(x, y) for Mul(Sub(0, x), y).
840 if (mleft.left().Is(0)) {
841 Emit(kArm64Mneg, g.DefineAsRegister(node),
842 g.UseRegister(mleft.right().node()),
843 g.UseRegister(m.right().node()));
844 return;
845 }
846 }
847
848 if (m.right().IsInt64Sub() && CanCover(node, m.right().node())) {
849 Int64BinopMatcher mright(m.right().node());
850
851 // Select Mneg(x, y) for Mul(x, Sub(0, y)).
852 if (mright.left().Is(0)) {
853 Emit(kArm64Mneg, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
854 g.UseRegister(mright.right().node()));
855 return;
856 }
857 }
858
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 VisitRRR(this, kArm64Mul, node);
860}
861
862
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400863void InstructionSelector::VisitInt32MulHigh(Node* node) {
864 // TODO(arm64): Can we do better here?
865 Arm64OperandGenerator g(this);
866 InstructionOperand* const smull_operand = g.TempRegister();
867 Emit(kArm64Smull, smull_operand, g.UseRegister(node->InputAt(0)),
868 g.UseRegister(node->InputAt(1)));
869 Emit(kArm64Asr, g.DefineAsRegister(node), smull_operand, g.TempImmediate(32));
870}
871
872
873void InstructionSelector::VisitUint32MulHigh(Node* node) {
874 // TODO(arm64): Can we do better here?
875 Arm64OperandGenerator g(this);
876 InstructionOperand* const smull_operand = g.TempRegister();
877 Emit(kArm64Umull, smull_operand, g.UseRegister(node->InputAt(0)),
878 g.UseRegister(node->InputAt(1)));
879 Emit(kArm64Lsr, g.DefineAsRegister(node), smull_operand, g.TempImmediate(32));
880}
881
882
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883void InstructionSelector::VisitInt32Div(Node* node) {
884 VisitRRR(this, kArm64Idiv32, node);
885}
886
887
888void InstructionSelector::VisitInt64Div(Node* node) {
889 VisitRRR(this, kArm64Idiv, node);
890}
891
892
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400893void InstructionSelector::VisitUint32Div(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894 VisitRRR(this, kArm64Udiv32, node);
895}
896
897
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400898void InstructionSelector::VisitUint64Div(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 VisitRRR(this, kArm64Udiv, node);
900}
901
902
903void InstructionSelector::VisitInt32Mod(Node* node) {
904 VisitRRR(this, kArm64Imod32, node);
905}
906
907
908void InstructionSelector::VisitInt64Mod(Node* node) {
909 VisitRRR(this, kArm64Imod, node);
910}
911
912
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400913void InstructionSelector::VisitUint32Mod(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 VisitRRR(this, kArm64Umod32, node);
915}
916
917
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400918void InstructionSelector::VisitUint64Mod(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000919 VisitRRR(this, kArm64Umod, node);
920}
921
922
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400923void InstructionSelector::VisitChangeFloat32ToFloat64(Node* node) {
924 Arm64OperandGenerator g(this);
925 Emit(kArm64Float32ToFloat64, g.DefineAsRegister(node),
926 g.UseRegister(node->InputAt(0)));
927}
928
929
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930void InstructionSelector::VisitChangeInt32ToFloat64(Node* node) {
931 Arm64OperandGenerator g(this);
932 Emit(kArm64Int32ToFloat64, g.DefineAsRegister(node),
933 g.UseRegister(node->InputAt(0)));
934}
935
936
937void InstructionSelector::VisitChangeUint32ToFloat64(Node* node) {
938 Arm64OperandGenerator g(this);
939 Emit(kArm64Uint32ToFloat64, g.DefineAsRegister(node),
940 g.UseRegister(node->InputAt(0)));
941}
942
943
944void InstructionSelector::VisitChangeFloat64ToInt32(Node* node) {
945 Arm64OperandGenerator g(this);
946 Emit(kArm64Float64ToInt32, g.DefineAsRegister(node),
947 g.UseRegister(node->InputAt(0)));
948}
949
950
951void InstructionSelector::VisitChangeFloat64ToUint32(Node* node) {
952 Arm64OperandGenerator g(this);
953 Emit(kArm64Float64ToUint32, g.DefineAsRegister(node),
954 g.UseRegister(node->InputAt(0)));
955}
956
957
958void InstructionSelector::VisitChangeInt32ToInt64(Node* node) {
959 Arm64OperandGenerator g(this);
960 Emit(kArm64Sxtw, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
961}
962
963
964void InstructionSelector::VisitChangeUint32ToUint64(Node* node) {
965 Arm64OperandGenerator g(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400966 Node* value = node->InputAt(0);
967 switch (value->opcode()) {
968 case IrOpcode::kWord32And:
969 case IrOpcode::kWord32Or:
970 case IrOpcode::kWord32Xor:
971 case IrOpcode::kWord32Shl:
972 case IrOpcode::kWord32Shr:
973 case IrOpcode::kWord32Sar:
974 case IrOpcode::kWord32Ror:
975 case IrOpcode::kWord32Equal:
976 case IrOpcode::kInt32Add:
977 case IrOpcode::kInt32AddWithOverflow:
978 case IrOpcode::kInt32Sub:
979 case IrOpcode::kInt32SubWithOverflow:
980 case IrOpcode::kInt32Mul:
981 case IrOpcode::kInt32MulHigh:
982 case IrOpcode::kInt32Div:
983 case IrOpcode::kInt32Mod:
984 case IrOpcode::kInt32LessThan:
985 case IrOpcode::kInt32LessThanOrEqual:
986 case IrOpcode::kUint32Div:
987 case IrOpcode::kUint32LessThan:
988 case IrOpcode::kUint32LessThanOrEqual:
989 case IrOpcode::kUint32Mod:
990 case IrOpcode::kUint32MulHigh: {
991 // 32-bit operations will write their result in a W register (implicitly
992 // clearing the top 32-bit of the corresponding X register) so the
993 // zero-extension is a no-op.
994 Emit(kArchNop, g.DefineSameAsFirst(node), g.Use(value));
995 return;
996 }
997 default:
998 break;
999 }
1000 Emit(kArm64Mov32, g.DefineAsRegister(node), g.UseRegister(value));
1001}
1002
1003
1004void InstructionSelector::VisitTruncateFloat64ToFloat32(Node* node) {
1005 Arm64OperandGenerator g(this);
1006 Emit(kArm64Float64ToFloat32, g.DefineAsRegister(node),
1007 g.UseRegister(node->InputAt(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008}
1009
1010
1011void InstructionSelector::VisitTruncateInt64ToInt32(Node* node) {
1012 Arm64OperandGenerator g(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001013 Node* value = node->InputAt(0);
1014 if (CanCover(node, value)) {
1015 Int64BinopMatcher m(value);
1016 if ((m.IsWord64Sar() && m.right().HasValue() &&
1017 (m.right().Value() == 32)) ||
1018 (m.IsWord64Shr() && m.right().IsInRange(32, 63))) {
1019 Emit(kArm64Lsr, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
1020 g.UseImmediate(m.right().node()));
1021 return;
1022 }
1023 }
1024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 Emit(kArm64Mov32, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
1026}
1027
1028
1029void InstructionSelector::VisitFloat64Add(Node* node) {
1030 VisitRRRFloat64(this, kArm64Float64Add, node);
1031}
1032
1033
1034void InstructionSelector::VisitFloat64Sub(Node* node) {
1035 VisitRRRFloat64(this, kArm64Float64Sub, node);
1036}
1037
1038
1039void InstructionSelector::VisitFloat64Mul(Node* node) {
1040 VisitRRRFloat64(this, kArm64Float64Mul, node);
1041}
1042
1043
1044void InstructionSelector::VisitFloat64Div(Node* node) {
1045 VisitRRRFloat64(this, kArm64Float64Div, node);
1046}
1047
1048
1049void InstructionSelector::VisitFloat64Mod(Node* node) {
1050 Arm64OperandGenerator g(this);
1051 Emit(kArm64Float64Mod, g.DefineAsFixed(node, d0),
1052 g.UseFixed(node->InputAt(0), d0),
1053 g.UseFixed(node->InputAt(1), d1))->MarkAsCall();
1054}
1055
1056
1057void InstructionSelector::VisitFloat64Sqrt(Node* node) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001058 VisitRRFloat64(this, kArm64Float64Sqrt, node);
1059}
1060
1061
1062void InstructionSelector::VisitFloat64Floor(Node* node) {
1063 VisitRRFloat64(this, kArm64Float64Floor, node);
1064}
1065
1066
1067void InstructionSelector::VisitFloat64Ceil(Node* node) {
1068 VisitRRFloat64(this, kArm64Float64Ceil, node);
1069}
1070
1071
1072void InstructionSelector::VisitFloat64RoundTruncate(Node* node) {
1073 VisitRRFloat64(this, kArm64Float64RoundTruncate, node);
1074}
1075
1076
1077void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) {
1078 VisitRRFloat64(this, kArm64Float64RoundTiesAway, node);
1079}
1080
1081
1082void InstructionSelector::VisitCall(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 Arm64OperandGenerator g(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001084 const CallDescriptor* descriptor = OpParameter<const CallDescriptor*>(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001085
1086 FrameStateDescriptor* frame_state_descriptor = NULL;
1087 if (descriptor->NeedsFrameState()) {
1088 frame_state_descriptor =
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001089 GetFrameStateDescriptor(node->InputAt(descriptor->InputCount()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001090 }
1091
1092 CallBuffer buffer(zone(), descriptor, frame_state_descriptor);
1093
1094 // Compute InstructionOperands for inputs and outputs.
1095 // TODO(turbofan): on ARM64 it's probably better to use the code object in a
1096 // register if there are multiple uses of it. Improve constant pool and the
1097 // heuristics in the register allocator for where to emit constants.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001098 InitializeCallBuffer(node, &buffer, true, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099
1100 // Push the arguments to the stack.
1101 bool pushed_count_uneven = buffer.pushed_nodes.size() & 1;
1102 int aligned_push_count = buffer.pushed_nodes.size();
1103 // TODO(dcarney): claim and poke probably take small immediates,
1104 // loop here or whatever.
1105 // Bump the stack pointer(s).
1106 if (aligned_push_count > 0) {
1107 // TODO(dcarney): it would be better to bump the csp here only
1108 // and emit paired stores with increment for non c frames.
1109 Emit(kArm64Claim | MiscField::encode(aligned_push_count), NULL);
1110 }
1111 // Move arguments to the stack.
1112 {
1113 int slot = buffer.pushed_nodes.size() - 1;
1114 // Emit the uneven pushes.
1115 if (pushed_count_uneven) {
1116 Node* input = buffer.pushed_nodes[slot];
1117 Emit(kArm64Poke | MiscField::encode(slot), NULL, g.UseRegister(input));
1118 slot--;
1119 }
1120 // Now all pushes can be done in pairs.
1121 for (; slot >= 0; slot -= 2) {
1122 Emit(kArm64PokePair | MiscField::encode(slot), NULL,
1123 g.UseRegister(buffer.pushed_nodes[slot]),
1124 g.UseRegister(buffer.pushed_nodes[slot - 1]));
1125 }
1126 }
1127
1128 // Select the appropriate opcode based on the call type.
1129 InstructionCode opcode;
1130 switch (descriptor->kind()) {
1131 case CallDescriptor::kCallCodeObject: {
1132 opcode = kArchCallCodeObject;
1133 break;
1134 }
1135 case CallDescriptor::kCallJSFunction:
1136 opcode = kArchCallJSFunction;
1137 break;
1138 default:
1139 UNREACHABLE();
1140 return;
1141 }
1142 opcode |= MiscField::encode(descriptor->flags());
1143
1144 // Emit the call instruction.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001145 InstructionOperand** first_output =
1146 buffer.outputs.size() > 0 ? &buffer.outputs.front() : NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 Instruction* call_instr =
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001148 Emit(opcode, buffer.outputs.size(), first_output,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 buffer.instruction_args.size(), &buffer.instruction_args.front());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001150 call_instr->MarkAsCall();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001151}
1152
1153
1154// Shared routine for multiple compare operations.
1155static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
1156 InstructionOperand* left, InstructionOperand* right,
1157 FlagsContinuation* cont) {
1158 Arm64OperandGenerator g(selector);
1159 opcode = cont->Encode(opcode);
1160 if (cont->IsBranch()) {
1161 selector->Emit(opcode, NULL, left, right, g.Label(cont->true_block()),
1162 g.Label(cont->false_block()))->MarkAsControl();
1163 } else {
1164 DCHECK(cont->IsSet());
1165 selector->Emit(opcode, g.DefineAsRegister(cont->result()), left, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001166 }
1167}
1168
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001169
1170// Shared routine for multiple word compare operations.
1171static void VisitWordCompare(InstructionSelector* selector, Node* node,
1172 InstructionCode opcode, FlagsContinuation* cont,
1173 bool commutative, ImmediateMode immediate_mode) {
1174 Arm64OperandGenerator g(selector);
1175 Node* left = node->InputAt(0);
1176 Node* right = node->InputAt(1);
1177
1178 // Match immediates on left or right side of comparison.
1179 if (g.CanBeImmediate(right, immediate_mode)) {
1180 VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right),
1181 cont);
1182 } else if (g.CanBeImmediate(left, immediate_mode)) {
1183 if (!commutative) cont->Commute();
1184 VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left),
1185 cont);
1186 } else {
1187 VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right),
1188 cont);
1189 }
1190}
1191
1192
1193static void VisitWord32Compare(InstructionSelector* selector, Node* node,
1194 FlagsContinuation* cont) {
1195 VisitWordCompare(selector, node, kArm64Cmp32, cont, false, kArithmeticImm);
1196}
1197
1198
1199static void VisitWordTest(InstructionSelector* selector, Node* node,
1200 InstructionCode opcode, FlagsContinuation* cont) {
1201 Arm64OperandGenerator g(selector);
1202 VisitCompare(selector, opcode, g.UseRegister(node), g.UseRegister(node),
1203 cont);
1204}
1205
1206
1207static void VisitWord32Test(InstructionSelector* selector, Node* node,
1208 FlagsContinuation* cont) {
1209 VisitWordTest(selector, node, kArm64Tst32, cont);
1210}
1211
1212
1213static void VisitWord64Test(InstructionSelector* selector, Node* node,
1214 FlagsContinuation* cont) {
1215 VisitWordTest(selector, node, kArm64Tst, cont);
1216}
1217
1218
1219// Shared routine for multiple float compare operations.
1220static void VisitFloat64Compare(InstructionSelector* selector, Node* node,
1221 FlagsContinuation* cont) {
1222 Arm64OperandGenerator g(selector);
1223 Node* left = node->InputAt(0);
1224 Node* right = node->InputAt(1);
1225 VisitCompare(selector, kArm64Float64Cmp, g.UseRegister(left),
1226 g.UseRegister(right), cont);
1227}
1228
1229
1230void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
1231 BasicBlock* fbranch) {
1232 OperandGenerator g(this);
1233 Node* user = branch;
1234 Node* value = branch->InputAt(0);
1235
1236 FlagsContinuation cont(kNotEqual, tbranch, fbranch);
1237
1238 // Try to combine with comparisons against 0 by simply inverting the branch.
1239 while (CanCover(user, value)) {
1240 if (value->opcode() == IrOpcode::kWord32Equal) {
1241 Int32BinopMatcher m(value);
1242 if (m.right().Is(0)) {
1243 user = value;
1244 value = m.left().node();
1245 cont.Negate();
1246 } else {
1247 break;
1248 }
1249 } else if (value->opcode() == IrOpcode::kWord64Equal) {
1250 Int64BinopMatcher m(value);
1251 if (m.right().Is(0)) {
1252 user = value;
1253 value = m.left().node();
1254 cont.Negate();
1255 } else {
1256 break;
1257 }
1258 } else {
1259 break;
1260 }
1261 }
1262
1263 // Try to combine the branch with a comparison.
1264 if (CanCover(user, value)) {
1265 switch (value->opcode()) {
1266 case IrOpcode::kWord32Equal:
1267 cont.OverwriteAndNegateIfEqual(kEqual);
1268 return VisitWord32Compare(this, value, &cont);
1269 case IrOpcode::kInt32LessThan:
1270 cont.OverwriteAndNegateIfEqual(kSignedLessThan);
1271 return VisitWord32Compare(this, value, &cont);
1272 case IrOpcode::kInt32LessThanOrEqual:
1273 cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
1274 return VisitWord32Compare(this, value, &cont);
1275 case IrOpcode::kUint32LessThan:
1276 cont.OverwriteAndNegateIfEqual(kUnsignedLessThan);
1277 return VisitWord32Compare(this, value, &cont);
1278 case IrOpcode::kUint32LessThanOrEqual:
1279 cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
1280 return VisitWord32Compare(this, value, &cont);
1281 case IrOpcode::kWord64Equal:
1282 cont.OverwriteAndNegateIfEqual(kEqual);
1283 return VisitWordCompare(this, value, kArm64Cmp, &cont, false,
1284 kArithmeticImm);
1285 case IrOpcode::kInt64LessThan:
1286 cont.OverwriteAndNegateIfEqual(kSignedLessThan);
1287 return VisitWordCompare(this, value, kArm64Cmp, &cont, false,
1288 kArithmeticImm);
1289 case IrOpcode::kInt64LessThanOrEqual:
1290 cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
1291 return VisitWordCompare(this, value, kArm64Cmp, &cont, false,
1292 kArithmeticImm);
1293 case IrOpcode::kUint64LessThan:
1294 cont.OverwriteAndNegateIfEqual(kUnsignedLessThan);
1295 return VisitWordCompare(this, value, kArm64Cmp, &cont, false,
1296 kArithmeticImm);
1297 case IrOpcode::kFloat64Equal:
1298 cont.OverwriteAndNegateIfEqual(kUnorderedEqual);
1299 return VisitFloat64Compare(this, value, &cont);
1300 case IrOpcode::kFloat64LessThan:
1301 cont.OverwriteAndNegateIfEqual(kUnorderedLessThan);
1302 return VisitFloat64Compare(this, value, &cont);
1303 case IrOpcode::kFloat64LessThanOrEqual:
1304 cont.OverwriteAndNegateIfEqual(kUnorderedLessThanOrEqual);
1305 return VisitFloat64Compare(this, value, &cont);
1306 case IrOpcode::kProjection:
1307 // Check if this is the overflow output projection of an
1308 // <Operation>WithOverflow node.
1309 if (OpParameter<size_t>(value) == 1u) {
1310 // We cannot combine the <Operation>WithOverflow with this branch
1311 // unless the 0th projection (the use of the actual value of the
1312 // <Operation> is either NULL, which means there's no use of the
1313 // actual value, or was already defined, which means it is scheduled
1314 // *AFTER* this branch).
1315 Node* node = value->InputAt(0);
1316 Node* result = node->FindProjection(0);
1317 if (result == NULL || IsDefined(result)) {
1318 switch (node->opcode()) {
1319 case IrOpcode::kInt32AddWithOverflow:
1320 cont.OverwriteAndNegateIfEqual(kOverflow);
1321 return VisitBinop<Int32BinopMatcher>(this, node, kArm64Add32,
1322 kArithmeticImm, &cont);
1323 case IrOpcode::kInt32SubWithOverflow:
1324 cont.OverwriteAndNegateIfEqual(kOverflow);
1325 return VisitBinop<Int32BinopMatcher>(this, node, kArm64Sub32,
1326 kArithmeticImm, &cont);
1327 default:
1328 break;
1329 }
1330 }
1331 }
1332 break;
1333 case IrOpcode::kInt32Add:
1334 return VisitWordCompare(this, value, kArm64Cmn32, &cont, true,
1335 kArithmeticImm);
1336 case IrOpcode::kInt32Sub:
1337 return VisitWordCompare(this, value, kArm64Cmp32, &cont, false,
1338 kArithmeticImm);
1339 case IrOpcode::kWord32And: {
1340 Int32BinopMatcher m(value);
1341 if (m.right().HasValue() &&
1342 (base::bits::CountPopulation32(m.right().Value()) == 1)) {
1343 // If the mask has only one bit set, we can use tbz/tbnz.
1344 DCHECK((cont.condition() == kEqual) ||
1345 (cont.condition() == kNotEqual));
1346 Emit(cont.Encode(kArm64TestAndBranch32), NULL,
1347 g.UseRegister(m.left().node()),
1348 g.TempImmediate(
1349 base::bits::CountTrailingZeros32(m.right().Value())),
1350 g.Label(cont.true_block()),
1351 g.Label(cont.false_block()))->MarkAsControl();
1352 return;
1353 }
1354 return VisitWordCompare(this, value, kArm64Tst32, &cont, true,
1355 kLogical32Imm);
1356 }
1357 case IrOpcode::kWord64And: {
1358 Int64BinopMatcher m(value);
1359 if (m.right().HasValue() &&
1360 (base::bits::CountPopulation64(m.right().Value()) == 1)) {
1361 // If the mask has only one bit set, we can use tbz/tbnz.
1362 DCHECK((cont.condition() == kEqual) ||
1363 (cont.condition() == kNotEqual));
1364 Emit(cont.Encode(kArm64TestAndBranch), NULL,
1365 g.UseRegister(m.left().node()),
1366 g.TempImmediate(
1367 base::bits::CountTrailingZeros64(m.right().Value())),
1368 g.Label(cont.true_block()),
1369 g.Label(cont.false_block()))->MarkAsControl();
1370 return;
1371 }
1372 return VisitWordCompare(this, value, kArm64Tst, &cont, true,
1373 kLogical64Imm);
1374 }
1375 default:
1376 break;
1377 }
1378 }
1379
1380 // Branch could not be combined with a compare, compare against 0 and branch.
1381 Emit(cont.Encode(kArm64CompareAndBranch32), NULL, g.UseRegister(value),
1382 g.Label(cont.true_block()),
1383 g.Label(cont.false_block()))->MarkAsControl();
1384}
1385
1386
1387void InstructionSelector::VisitWord32Equal(Node* const node) {
1388 Node* const user = node;
1389 FlagsContinuation cont(kEqual, node);
1390 Int32BinopMatcher m(user);
1391 if (m.right().Is(0)) {
1392 Node* const value = m.left().node();
1393 if (CanCover(user, value)) {
1394 switch (value->opcode()) {
1395 case IrOpcode::kInt32Add:
1396 return VisitWordCompare(this, value, kArm64Cmn32, &cont, true,
1397 kArithmeticImm);
1398 case IrOpcode::kInt32Sub:
1399 return VisitWordCompare(this, value, kArm64Cmp32, &cont, false,
1400 kArithmeticImm);
1401 case IrOpcode::kWord32And:
1402 return VisitWordCompare(this, value, kArm64Tst32, &cont, true,
1403 kLogical32Imm);
1404 default:
1405 break;
1406 }
1407 return VisitWord32Test(this, value, &cont);
1408 }
1409 }
1410 VisitWord32Compare(this, node, &cont);
1411}
1412
1413
1414void InstructionSelector::VisitInt32LessThan(Node* node) {
1415 FlagsContinuation cont(kSignedLessThan, node);
1416 VisitWord32Compare(this, node, &cont);
1417}
1418
1419
1420void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) {
1421 FlagsContinuation cont(kSignedLessThanOrEqual, node);
1422 VisitWord32Compare(this, node, &cont);
1423}
1424
1425
1426void InstructionSelector::VisitUint32LessThan(Node* node) {
1427 FlagsContinuation cont(kUnsignedLessThan, node);
1428 VisitWord32Compare(this, node, &cont);
1429}
1430
1431
1432void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) {
1433 FlagsContinuation cont(kUnsignedLessThanOrEqual, node);
1434 VisitWord32Compare(this, node, &cont);
1435}
1436
1437
1438void InstructionSelector::VisitWord64Equal(Node* const node) {
1439 Node* const user = node;
1440 FlagsContinuation cont(kEqual, node);
1441 Int64BinopMatcher m(user);
1442 if (m.right().Is(0)) {
1443 Node* const value = m.left().node();
1444 if (CanCover(user, value)) {
1445 switch (value->opcode()) {
1446 case IrOpcode::kWord64And:
1447 return VisitWordCompare(this, value, kArm64Tst, &cont, true,
1448 kLogical64Imm);
1449 default:
1450 break;
1451 }
1452 return VisitWord64Test(this, value, &cont);
1453 }
1454 }
1455 VisitWordCompare(this, node, kArm64Cmp, &cont, false, kArithmeticImm);
1456}
1457
1458
1459void InstructionSelector::VisitInt32AddWithOverflow(Node* node) {
1460 if (Node* ovf = node->FindProjection(1)) {
1461 FlagsContinuation cont(kOverflow, ovf);
1462 return VisitBinop<Int32BinopMatcher>(this, node, kArm64Add32,
1463 kArithmeticImm, &cont);
1464 }
1465 FlagsContinuation cont;
1466 VisitBinop<Int32BinopMatcher>(this, node, kArm64Add32, kArithmeticImm, &cont);
1467}
1468
1469
1470void InstructionSelector::VisitInt32SubWithOverflow(Node* node) {
1471 if (Node* ovf = node->FindProjection(1)) {
1472 FlagsContinuation cont(kOverflow, ovf);
1473 return VisitBinop<Int32BinopMatcher>(this, node, kArm64Sub32,
1474 kArithmeticImm, &cont);
1475 }
1476 FlagsContinuation cont;
1477 VisitBinop<Int32BinopMatcher>(this, node, kArm64Sub32, kArithmeticImm, &cont);
1478}
1479
1480
1481void InstructionSelector::VisitInt64LessThan(Node* node) {
1482 FlagsContinuation cont(kSignedLessThan, node);
1483 VisitWordCompare(this, node, kArm64Cmp, &cont, false, kArithmeticImm);
1484}
1485
1486
1487void InstructionSelector::VisitInt64LessThanOrEqual(Node* node) {
1488 FlagsContinuation cont(kSignedLessThanOrEqual, node);
1489 VisitWordCompare(this, node, kArm64Cmp, &cont, false, kArithmeticImm);
1490}
1491
1492
1493void InstructionSelector::VisitUint64LessThan(Node* node) {
1494 FlagsContinuation cont(kUnsignedLessThan, node);
1495 VisitWordCompare(this, node, kArm64Cmp, &cont, false, kArithmeticImm);
1496}
1497
1498
1499void InstructionSelector::VisitFloat64Equal(Node* node) {
1500 FlagsContinuation cont(kUnorderedEqual, node);
1501 VisitFloat64Compare(this, node, &cont);
1502}
1503
1504
1505void InstructionSelector::VisitFloat64LessThan(Node* node) {
1506 FlagsContinuation cont(kUnorderedLessThan, node);
1507 VisitFloat64Compare(this, node, &cont);
1508}
1509
1510
1511void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) {
1512 FlagsContinuation cont(kUnorderedLessThanOrEqual, node);
1513 VisitFloat64Compare(this, node, &cont);
1514}
1515
1516
1517// static
1518MachineOperatorBuilder::Flags
1519InstructionSelector::SupportedMachineOperatorFlags() {
1520 return MachineOperatorBuilder::kFloat64Floor |
1521 MachineOperatorBuilder::kFloat64Ceil |
1522 MachineOperatorBuilder::kFloat64RoundTruncate |
1523 MachineOperatorBuilder::kFloat64RoundTiesAway |
1524 MachineOperatorBuilder::kWord32ShiftIsSafe |
1525 MachineOperatorBuilder::kInt32DivIsSafe |
1526 MachineOperatorBuilder::kUint32DivIsSafe;
1527}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001528} // namespace compiler
1529} // namespace internal
1530} // namespace v8