1 // 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.
5 #include "src/base/bits.h"
6 #include "src/compiler/instruction-selector-impl.h"
7 #include "src/compiler/node-matchers.h"
8 #include "src/compiler/node-properties.h"
14 #define TRACE_UNIMPL() \
15 PrintF("UNIMPLEMENTED instr_sel: %s at line %d\n", __FUNCTION__, __LINE__)
17 #define TRACE() PrintF("instr_sel: %s at line %d\n", __FUNCTION__, __LINE__)
20 // Adds Mips-specific methods for generating InstructionOperands.
21 class MipsOperandGenerator FINAL : public OperandGenerator {
23 explicit MipsOperandGenerator(InstructionSelector* selector)
24 : OperandGenerator(selector) {}
26 InstructionOperand UseOperand(Node* node, InstructionCode opcode) {
27 if (CanBeImmediate(node, opcode)) {
28 return UseImmediate(node);
30 return UseRegister(node);
33 bool CanBeImmediate(Node* node, InstructionCode opcode) {
35 if (!m.HasValue()) return false;
36 int32_t value = m.Value();
37 switch (ArchOpcodeField::decode(opcode)) {
41 return is_uint5(value);
43 return is_uint16(value);
46 case kCheckedLoadFloat32:
47 case kCheckedLoadFloat64:
48 case kCheckedStoreFloat32:
49 case kCheckedStoreFloat64:
50 return is_int16(value + kIntSize);
52 return is_int16(value);
57 bool ImmediateFitsAddrMode1Instruction(int32_t imm) const {
64 static void VisitRRR(InstructionSelector* selector, ArchOpcode opcode,
66 MipsOperandGenerator g(selector);
67 selector->Emit(opcode, g.DefineAsRegister(node),
68 g.UseRegister(node->InputAt(0)),
69 g.UseRegister(node->InputAt(1)));
73 static void VisitRR(InstructionSelector* selector, ArchOpcode opcode,
75 MipsOperandGenerator g(selector);
76 selector->Emit(opcode, g.DefineAsRegister(node),
77 g.UseRegister(node->InputAt(0)));
81 static void VisitRRO(InstructionSelector* selector, ArchOpcode opcode,
83 MipsOperandGenerator g(selector);
84 selector->Emit(opcode, g.DefineAsRegister(node),
85 g.UseRegister(node->InputAt(0)),
86 g.UseOperand(node->InputAt(1), opcode));
90 static void VisitBinop(InstructionSelector* selector, Node* node,
91 InstructionCode opcode, FlagsContinuation* cont) {
92 MipsOperandGenerator g(selector);
93 Int32BinopMatcher m(node);
94 InstructionOperand inputs[4];
95 size_t input_count = 0;
96 InstructionOperand outputs[2];
97 size_t output_count = 0;
99 inputs[input_count++] = g.UseRegister(m.left().node());
100 inputs[input_count++] = g.UseOperand(m.right().node(), opcode);
102 if (cont->IsBranch()) {
103 inputs[input_count++] = g.Label(cont->true_block());
104 inputs[input_count++] = g.Label(cont->false_block());
107 outputs[output_count++] = g.DefineAsRegister(node);
109 outputs[output_count++] = g.DefineAsRegister(cont->result());
112 DCHECK_NE(0u, input_count);
113 DCHECK_NE(0u, output_count);
114 DCHECK_GE(arraysize(inputs), input_count);
115 DCHECK_GE(arraysize(outputs), output_count);
117 Instruction* instr = selector->Emit(cont->Encode(opcode), output_count,
118 outputs, input_count, inputs);
119 if (cont->IsBranch()) instr->MarkAsControl();
123 static void VisitBinop(InstructionSelector* selector, Node* node,
124 InstructionCode opcode) {
125 FlagsContinuation cont;
126 VisitBinop(selector, node, opcode, &cont);
130 void InstructionSelector::VisitLoad(Node* node) {
131 MachineType rep = RepresentationOf(OpParameter<LoadRepresentation>(node));
132 MachineType typ = TypeOf(OpParameter<LoadRepresentation>(node));
133 MipsOperandGenerator g(this);
134 Node* base = node->InputAt(0);
135 Node* index = node->InputAt(1);
145 case kRepBit: // Fall through.
147 opcode = typ == kTypeUint32 ? kMipsLbu : kMipsLb;
150 opcode = typ == kTypeUint32 ? kMipsLhu : kMipsLh;
152 case kRepTagged: // Fall through.
161 if (g.CanBeImmediate(index, opcode)) {
162 Emit(opcode | AddressingModeField::encode(kMode_MRI),
163 g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index));
165 InstructionOperand addr_reg = g.TempRegister();
166 Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg,
167 g.UseRegister(index), g.UseRegister(base));
168 // Emit desired load opcode, using temp addr_reg.
169 Emit(opcode | AddressingModeField::encode(kMode_MRI),
170 g.DefineAsRegister(node), addr_reg, g.TempImmediate(0));
175 void InstructionSelector::VisitStore(Node* node) {
176 MipsOperandGenerator g(this);
177 Node* base = node->InputAt(0);
178 Node* index = node->InputAt(1);
179 Node* value = node->InputAt(2);
181 StoreRepresentation store_rep = OpParameter<StoreRepresentation>(node);
182 MachineType rep = RepresentationOf(store_rep.machine_type());
183 if (store_rep.write_barrier_kind() == kFullWriteBarrier) {
184 DCHECK(rep == kRepTagged);
185 // TODO(dcarney): refactor RecordWrite function to take temp registers
186 // and pass them here instead of using fixed regs
187 // TODO(dcarney): handle immediate indices.
188 InstructionOperand temps[] = {g.TempRegister(t1), g.TempRegister(t2)};
189 Emit(kMipsStoreWriteBarrier, g.NoOutput(), g.UseFixed(base, t0),
190 g.UseFixed(index, t1), g.UseFixed(value, t2), arraysize(temps), temps);
193 DCHECK_EQ(kNoWriteBarrier, store_rep.write_barrier_kind());
203 case kRepBit: // Fall through.
210 case kRepTagged: // Fall through.
219 if (g.CanBeImmediate(index, opcode)) {
220 Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
221 g.UseRegister(base), g.UseImmediate(index), g.UseRegister(value));
223 InstructionOperand addr_reg = g.TempRegister();
224 Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg,
225 g.UseRegister(index), g.UseRegister(base));
226 // Emit desired store opcode, using temp addr_reg.
227 Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
228 addr_reg, g.TempImmediate(0), g.UseRegister(value));
233 void InstructionSelector::VisitWord32And(Node* node) {
234 VisitBinop(this, node, kMipsAnd);
238 void InstructionSelector::VisitWord32Or(Node* node) {
239 VisitBinop(this, node, kMipsOr);
243 void InstructionSelector::VisitWord32Xor(Node* node) {
244 VisitBinop(this, node, kMipsXor);
248 void InstructionSelector::VisitWord32Shl(Node* node) {
249 VisitRRO(this, kMipsShl, node);
253 void InstructionSelector::VisitWord32Shr(Node* node) {
254 VisitRRO(this, kMipsShr, node);
258 void InstructionSelector::VisitWord32Sar(Node* node) {
259 VisitRRO(this, kMipsSar, node);
263 void InstructionSelector::VisitWord32Ror(Node* node) {
264 VisitRRO(this, kMipsRor, node);
268 void InstructionSelector::VisitInt32Add(Node* node) {
269 MipsOperandGenerator g(this);
271 // TODO(plind): Consider multiply & add optimization from arm port.
272 VisitBinop(this, node, kMipsAdd);
276 void InstructionSelector::VisitInt32Sub(Node* node) {
277 VisitBinop(this, node, kMipsSub);
281 void InstructionSelector::VisitInt32Mul(Node* node) {
282 MipsOperandGenerator g(this);
283 Int32BinopMatcher m(node);
284 if (m.right().HasValue() && m.right().Value() > 0) {
285 int32_t value = m.right().Value();
286 if (base::bits::IsPowerOfTwo32(value)) {
287 Emit(kMipsShl | AddressingModeField::encode(kMode_None),
288 g.DefineAsRegister(node), g.UseRegister(m.left().node()),
289 g.TempImmediate(WhichPowerOf2(value)));
292 if (base::bits::IsPowerOfTwo32(value - 1)) {
293 InstructionOperand temp = g.TempRegister();
294 Emit(kMipsShl | AddressingModeField::encode(kMode_None), temp,
295 g.UseRegister(m.left().node()),
296 g.TempImmediate(WhichPowerOf2(value - 1)));
297 Emit(kMipsAdd | AddressingModeField::encode(kMode_None),
298 g.DefineAsRegister(node), g.UseRegister(m.left().node()), temp);
301 if (base::bits::IsPowerOfTwo32(value + 1)) {
302 InstructionOperand temp = g.TempRegister();
303 Emit(kMipsShl | AddressingModeField::encode(kMode_None), temp,
304 g.UseRegister(m.left().node()),
305 g.TempImmediate(WhichPowerOf2(value + 1)));
306 Emit(kMipsSub | AddressingModeField::encode(kMode_None),
307 g.DefineAsRegister(node), temp, g.UseRegister(m.left().node()));
311 Emit(kMipsMul, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
312 g.UseRegister(m.right().node()));
316 void InstructionSelector::VisitInt32MulHigh(Node* node) {
317 MipsOperandGenerator g(this);
318 Emit(kMipsMulHigh, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
319 g.UseRegister(node->InputAt(1)));
323 void InstructionSelector::VisitUint32MulHigh(Node* node) {
324 MipsOperandGenerator g(this);
325 Emit(kMipsMulHighU, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
326 g.UseRegister(node->InputAt(1)));
330 void InstructionSelector::VisitInt32Div(Node* node) {
331 MipsOperandGenerator g(this);
332 Int32BinopMatcher m(node);
333 Emit(kMipsDiv, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
334 g.UseRegister(m.right().node()));
338 void InstructionSelector::VisitUint32Div(Node* node) {
339 MipsOperandGenerator g(this);
340 Int32BinopMatcher m(node);
341 Emit(kMipsDivU, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
342 g.UseRegister(m.right().node()));
346 void InstructionSelector::VisitInt32Mod(Node* node) {
347 MipsOperandGenerator g(this);
348 Int32BinopMatcher m(node);
349 Emit(kMipsMod, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
350 g.UseRegister(m.right().node()));
354 void InstructionSelector::VisitUint32Mod(Node* node) {
355 MipsOperandGenerator g(this);
356 Int32BinopMatcher m(node);
357 Emit(kMipsModU, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
358 g.UseRegister(m.right().node()));
362 void InstructionSelector::VisitChangeFloat32ToFloat64(Node* node) {
363 MipsOperandGenerator g(this);
364 Emit(kMipsCvtDS, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
368 void InstructionSelector::VisitChangeInt32ToFloat64(Node* node) {
369 MipsOperandGenerator g(this);
370 Emit(kMipsCvtDW, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
374 void InstructionSelector::VisitChangeUint32ToFloat64(Node* node) {
375 MipsOperandGenerator g(this);
376 Emit(kMipsCvtDUw, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
380 void InstructionSelector::VisitChangeFloat64ToInt32(Node* node) {
381 MipsOperandGenerator g(this);
382 Emit(kMipsTruncWD, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
386 void InstructionSelector::VisitChangeFloat64ToUint32(Node* node) {
387 MipsOperandGenerator g(this);
388 Emit(kMipsTruncUwD, g.DefineAsRegister(node),
389 g.UseRegister(node->InputAt(0)));
393 void InstructionSelector::VisitTruncateFloat64ToFloat32(Node* node) {
394 MipsOperandGenerator g(this);
395 Emit(kMipsCvtSD, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
399 void InstructionSelector::VisitFloat64Add(Node* node) {
400 VisitRRR(this, kMipsAddD, node);
404 void InstructionSelector::VisitFloat64Sub(Node* node) {
405 VisitRRR(this, kMipsSubD, node);
409 void InstructionSelector::VisitFloat64Mul(Node* node) {
410 VisitRRR(this, kMipsMulD, node);
414 void InstructionSelector::VisitFloat64Div(Node* node) {
415 VisitRRR(this, kMipsDivD, node);
419 void InstructionSelector::VisitFloat64Mod(Node* node) {
420 MipsOperandGenerator g(this);
421 Emit(kMipsModD, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f12),
422 g.UseFixed(node->InputAt(1), f14))->MarkAsCall();
426 void InstructionSelector::VisitFloat64Sqrt(Node* node) {
427 MipsOperandGenerator g(this);
428 Emit(kMipsSqrtD, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
432 void InstructionSelector::VisitFloat64Floor(Node* node) {
433 VisitRR(this, kMipsFloat64Floor, node);
437 void InstructionSelector::VisitFloat64Ceil(Node* node) {
438 VisitRR(this, kMipsFloat64Ceil, node);
442 void InstructionSelector::VisitFloat64RoundTruncate(Node* node) {
443 VisitRR(this, kMipsFloat64RoundTruncate, node);
447 void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) {
452 void InstructionSelector::VisitCall(Node* node) {
453 MipsOperandGenerator g(this);
454 const CallDescriptor* descriptor = OpParameter<const CallDescriptor*>(node);
456 FrameStateDescriptor* frame_state_descriptor = NULL;
457 if (descriptor->NeedsFrameState()) {
458 frame_state_descriptor =
459 GetFrameStateDescriptor(node->InputAt(descriptor->InputCount()));
462 CallBuffer buffer(zone(), descriptor, frame_state_descriptor);
464 // Compute InstructionOperands for inputs and outputs.
465 InitializeCallBuffer(node, &buffer, true, false);
466 // Possibly align stack here for functions.
467 int push_count = buffer.pushed_nodes.size();
468 if (push_count > 0) {
469 Emit(kMipsStackClaim | MiscField::encode(push_count), g.NoOutput());
471 int slot = buffer.pushed_nodes.size() - 1;
472 for (auto i = buffer.pushed_nodes.rbegin(); i != buffer.pushed_nodes.rend();
474 Emit(kMipsStoreToStackSlot | MiscField::encode(slot), g.NoOutput(),
479 // Select the appropriate opcode based on the call type.
480 InstructionCode opcode;
481 switch (descriptor->kind()) {
482 case CallDescriptor::kCallCodeObject: {
483 opcode = kArchCallCodeObject;
486 case CallDescriptor::kCallJSFunction:
487 opcode = kArchCallJSFunction;
493 opcode |= MiscField::encode(descriptor->flags());
495 // Emit the call instruction.
496 InstructionOperand* first_output =
497 buffer.outputs.size() > 0 ? &buffer.outputs.front() : NULL;
498 Instruction* call_instr =
499 Emit(opcode, buffer.outputs.size(), first_output,
500 buffer.instruction_args.size(), &buffer.instruction_args.front());
501 call_instr->MarkAsCall();
505 void InstructionSelector::VisitCheckedLoad(Node* node) {
506 MachineType rep = RepresentationOf(OpParameter<MachineType>(node));
507 MachineType typ = TypeOf(OpParameter<MachineType>(node));
508 MipsOperandGenerator g(this);
509 Node* const buffer = node->InputAt(0);
510 Node* const offset = node->InputAt(1);
511 Node* const length = node->InputAt(2);
515 opcode = typ == kTypeInt32 ? kCheckedLoadInt8 : kCheckedLoadUint8;
518 opcode = typ == kTypeInt32 ? kCheckedLoadInt16 : kCheckedLoadUint16;
521 opcode = kCheckedLoadWord32;
524 opcode = kCheckedLoadFloat32;
527 opcode = kCheckedLoadFloat64;
533 InstructionOperand offset_operand = g.CanBeImmediate(offset, opcode)
534 ? g.UseImmediate(offset)
535 : g.UseRegister(offset);
537 InstructionOperand length_operand = (!g.CanBeImmediate(offset, opcode))
538 ? g.CanBeImmediate(length, opcode)
539 ? g.UseImmediate(length)
540 : g.UseRegister(length)
541 : g.UseRegister(length);
543 Emit(opcode | AddressingModeField::encode(kMode_MRI),
544 g.DefineAsRegister(node), offset_operand, length_operand,
545 g.UseRegister(buffer));
549 void InstructionSelector::VisitCheckedStore(Node* node) {
550 MachineType rep = RepresentationOf(OpParameter<MachineType>(node));
551 MipsOperandGenerator g(this);
552 Node* const buffer = node->InputAt(0);
553 Node* const offset = node->InputAt(1);
554 Node* const length = node->InputAt(2);
555 Node* const value = node->InputAt(3);
559 opcode = kCheckedStoreWord8;
562 opcode = kCheckedStoreWord16;
565 opcode = kCheckedStoreWord32;
568 opcode = kCheckedStoreFloat32;
571 opcode = kCheckedStoreFloat64;
577 InstructionOperand offset_operand = g.CanBeImmediate(offset, opcode)
578 ? g.UseImmediate(offset)
579 : g.UseRegister(offset);
581 InstructionOperand length_operand = (!g.CanBeImmediate(offset, opcode))
582 ? g.CanBeImmediate(length, opcode)
583 ? g.UseImmediate(length)
584 : g.UseRegister(length)
585 : g.UseRegister(length);
587 Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
588 offset_operand, length_operand, g.UseRegister(value),
589 g.UseRegister(buffer));
595 // Shared routine for multiple compare operations.
596 static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
597 InstructionOperand left, InstructionOperand right,
598 FlagsContinuation* cont) {
599 MipsOperandGenerator g(selector);
600 opcode = cont->Encode(opcode);
601 if (cont->IsBranch()) {
602 selector->Emit(opcode, g.NoOutput(), left, right,
603 g.Label(cont->true_block()),
604 g.Label(cont->false_block()))->MarkAsControl();
606 DCHECK(cont->IsSet());
607 // TODO(plind): Revisit and test this path.
608 selector->Emit(opcode, g.DefineAsRegister(cont->result()), left, right);
613 // Shared routine for multiple float compare operations.
614 void VisitFloat64Compare(InstructionSelector* selector, Node* node,
615 FlagsContinuation* cont) {
616 MipsOperandGenerator g(selector);
617 Node* left = node->InputAt(0);
618 Node* right = node->InputAt(1);
619 VisitCompare(selector, kMipsCmpD, g.UseRegister(left), g.UseRegister(right),
624 // Shared routine for multiple word compare operations.
625 void VisitWordCompare(InstructionSelector* selector, Node* node,
626 InstructionCode opcode, FlagsContinuation* cont,
628 MipsOperandGenerator g(selector);
629 Node* left = node->InputAt(0);
630 Node* right = node->InputAt(1);
632 // Match immediates on left or right side of comparison.
633 if (g.CanBeImmediate(right, opcode)) {
634 VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right),
636 } else if (g.CanBeImmediate(left, opcode)) {
637 if (!commutative) cont->Commute();
638 VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left),
641 VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right),
647 void VisitWordCompare(InstructionSelector* selector, Node* node,
648 FlagsContinuation* cont) {
649 VisitWordCompare(selector, node, kMipsCmp, cont, false);
655 // Shared routine for word comparisons against zero.
656 void VisitWordCompareZero(InstructionSelector* selector, Node* user,
657 Node* value, FlagsContinuation* cont) {
658 while (selector->CanCover(user, value)) {
659 switch (value->opcode()) {
660 case IrOpcode::kWord32Equal: {
661 // Combine with comparisons against 0 by simply inverting the
663 Int32BinopMatcher m(value);
664 if (m.right().Is(0)) {
666 value = m.left().node();
670 cont->OverwriteAndNegateIfEqual(kEqual);
671 return VisitWordCompare(selector, value, cont);
673 case IrOpcode::kInt32LessThan:
674 cont->OverwriteAndNegateIfEqual(kSignedLessThan);
675 return VisitWordCompare(selector, value, cont);
676 case IrOpcode::kInt32LessThanOrEqual:
677 cont->OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
678 return VisitWordCompare(selector, value, cont);
679 case IrOpcode::kUint32LessThan:
680 cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
681 return VisitWordCompare(selector, value, cont);
682 case IrOpcode::kUint32LessThanOrEqual:
683 cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
684 return VisitWordCompare(selector, value, cont);
685 case IrOpcode::kFloat64Equal:
686 cont->OverwriteAndNegateIfEqual(kEqual);
687 return VisitFloat64Compare(selector, value, cont);
688 case IrOpcode::kFloat64LessThan:
689 cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
690 return VisitFloat64Compare(selector, value, cont);
691 case IrOpcode::kFloat64LessThanOrEqual:
692 cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
693 return VisitFloat64Compare(selector, value, cont);
694 case IrOpcode::kProjection:
695 // Check if this is the overflow output projection of an
696 // <Operation>WithOverflow node.
697 if (ProjectionIndexOf(value->op()) == 1u) {
698 // We cannot combine the <Operation>WithOverflow with this branch
699 // unless the 0th projection (the use of the actual value of the
700 // <Operation> is either NULL, which means there's no use of the
701 // actual value, or was already defined, which means it is scheduled
702 // *AFTER* this branch).
703 Node* const node = value->InputAt(0);
704 Node* const result = NodeProperties::FindProjection(node, 0);
705 if (!result || selector->IsDefined(result)) {
706 switch (node->opcode()) {
707 case IrOpcode::kInt32AddWithOverflow:
708 cont->OverwriteAndNegateIfEqual(kOverflow);
709 return VisitBinop(selector, node, kMipsAddOvf, cont);
710 case IrOpcode::kInt32SubWithOverflow:
711 cont->OverwriteAndNegateIfEqual(kOverflow);
712 return VisitBinop(selector, node, kMipsSubOvf, cont);
719 case IrOpcode::kWord32And:
720 return VisitWordCompare(selector, value, kMipsTst, cont, true);
727 // Continuation could not be combined with a compare, emit compare against 0.
728 MipsOperandGenerator g(selector);
729 InstructionCode const opcode = cont->Encode(kMipsCmp);
730 InstructionOperand const value_operand = g.UseRegister(value);
731 if (cont->IsBranch()) {
732 selector->Emit(opcode, g.NoOutput(), value_operand, g.TempImmediate(0),
733 g.Label(cont->true_block()),
734 g.Label(cont->false_block()))->MarkAsControl();
736 selector->Emit(opcode, g.DefineAsRegister(cont->result()), value_operand,
742 void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch,
743 BasicBlock* fbranch) {
744 FlagsContinuation cont(kNotEqual, tbranch, fbranch);
745 VisitWordCompareZero(this, branch, branch->InputAt(0), &cont);
749 void InstructionSelector::VisitSwitch(Node* node, BasicBlock* default_branch,
750 BasicBlock** case_branches,
751 int32_t* case_values, size_t case_count,
752 int32_t min_value, int32_t max_value) {
753 MipsOperandGenerator g(this);
754 InstructionOperand value_operand = g.UseRegister(node->InputAt(0));
755 InstructionOperand default_operand = g.Label(default_branch);
757 // Note that {value_range} can be 0 if {min_value} is -2^31 and {max_value}
758 // is 2^31-1, so don't assume that it's non-zero below.
760 1u + bit_cast<uint32_t>(max_value) - bit_cast<uint32_t>(min_value);
762 // Determine whether to issue an ArchTableSwitch or an ArchLookupSwitch
764 size_t table_space_cost = 9 + value_range;
765 size_t table_time_cost = 9;
766 size_t lookup_space_cost = 2 + 2 * case_count;
767 size_t lookup_time_cost = case_count;
768 if (case_count > 0 &&
769 table_space_cost + 3 * table_time_cost <=
770 lookup_space_cost + 3 * lookup_time_cost &&
771 min_value > std::numeric_limits<int32_t>::min()) {
772 InstructionOperand index_operand = value_operand;
774 index_operand = g.TempRegister();
775 Emit(kMipsSub, index_operand, value_operand, g.TempImmediate(min_value));
777 size_t input_count = 2 + value_range;
778 auto* inputs = zone()->NewArray<InstructionOperand>(input_count);
779 inputs[0] = index_operand;
780 std::fill(&inputs[1], &inputs[input_count], default_operand);
781 for (size_t index = 0; index < case_count; ++index) {
782 size_t value = case_values[index] - min_value;
783 BasicBlock* branch = case_branches[index];
784 DCHECK_LE(0u, value);
785 DCHECK_LT(value + 2, input_count);
786 inputs[value + 2] = g.Label(branch);
788 Emit(kArchTableSwitch, 0, nullptr, input_count, inputs, 0, nullptr)
793 // Generate a sequence of conditional jumps.
794 size_t input_count = 2 + case_count * 2;
795 auto* inputs = zone()->NewArray<InstructionOperand>(input_count);
796 inputs[0] = value_operand;
797 inputs[1] = default_operand;
798 for (size_t index = 0; index < case_count; ++index) {
799 int32_t value = case_values[index];
800 BasicBlock* branch = case_branches[index];
801 inputs[index * 2 + 2 + 0] = g.TempImmediate(value);
802 inputs[index * 2 + 2 + 1] = g.Label(branch);
804 Emit(kArchLookupSwitch, 0, nullptr, input_count, inputs, 0, nullptr)
809 void InstructionSelector::VisitWord32Equal(Node* const node) {
810 FlagsContinuation cont(kEqual, node);
811 Int32BinopMatcher m(node);
812 if (m.right().Is(0)) {
813 return VisitWordCompareZero(this, m.node(), m.left().node(), &cont);
815 VisitWordCompare(this, node, &cont);
819 void InstructionSelector::VisitInt32LessThan(Node* node) {
820 FlagsContinuation cont(kSignedLessThan, node);
821 VisitWordCompare(this, node, &cont);
825 void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) {
826 FlagsContinuation cont(kSignedLessThanOrEqual, node);
827 VisitWordCompare(this, node, &cont);
831 void InstructionSelector::VisitUint32LessThan(Node* node) {
832 FlagsContinuation cont(kUnsignedLessThan, node);
833 VisitWordCompare(this, node, &cont);
837 void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) {
838 FlagsContinuation cont(kUnsignedLessThanOrEqual, node);
839 VisitWordCompare(this, node, &cont);
843 void InstructionSelector::VisitInt32AddWithOverflow(Node* node) {
844 if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
845 FlagsContinuation cont(kOverflow, ovf);
846 return VisitBinop(this, node, kMipsAddOvf, &cont);
848 FlagsContinuation cont;
849 VisitBinop(this, node, kMipsAddOvf, &cont);
853 void InstructionSelector::VisitInt32SubWithOverflow(Node* node) {
854 if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
855 FlagsContinuation cont(kOverflow, ovf);
856 return VisitBinop(this, node, kMipsSubOvf, &cont);
858 FlagsContinuation cont;
859 VisitBinop(this, node, kMipsSubOvf, &cont);
863 void InstructionSelector::VisitFloat64Equal(Node* node) {
864 FlagsContinuation cont(kEqual, node);
865 VisitFloat64Compare(this, node, &cont);
869 void InstructionSelector::VisitFloat64LessThan(Node* node) {
870 FlagsContinuation cont(kUnsignedLessThan, node);
871 VisitFloat64Compare(this, node, &cont);
875 void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) {
876 FlagsContinuation cont(kUnsignedLessThanOrEqual, node);
877 VisitFloat64Compare(this, node, &cont);
882 MachineOperatorBuilder::Flags
883 InstructionSelector::SupportedMachineOperatorFlags() {
884 if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
885 return MachineOperatorBuilder::kFloat64Floor |
886 MachineOperatorBuilder::kFloat64Ceil |
887 MachineOperatorBuilder::kFloat64RoundTruncate;
889 return MachineOperatorBuilder::kNoFlags;
892 } // namespace compiler
893 } // namespace internal