// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/base/adapters.h" #include "src/base/bits.h" #include "src/compiler/instruction-selector-impl.h" #include "src/compiler/node-matchers.h" #include "src/compiler/node-properties.h" namespace v8 { namespace internal { namespace compiler { #define TRACE_UNIMPL() \ PrintF("UNIMPLEMENTED instr_sel: %s at line %d\n", __FUNCTION__, __LINE__) #define TRACE() PrintF("instr_sel: %s at line %d\n", __FUNCTION__, __LINE__) // Adds Mips-specific methods for generating InstructionOperands. class MipsOperandGenerator final : public OperandGenerator { public: explicit MipsOperandGenerator(InstructionSelector* selector) : OperandGenerator(selector) {} InstructionOperand UseOperand(Node* node, InstructionCode opcode) { if (CanBeImmediate(node, opcode)) { return UseImmediate(node); } return UseRegister(node); } // Use the zero register if the node has the immediate value zero, otherwise // assign a register. InstructionOperand UseRegisterOrImmediateZero(Node* node) { if ((IsIntegerConstant(node) && (GetIntegerConstantValue(node) == 0)) || (IsFloatConstant(node) && (bit_cast(GetFloatConstantValue(node)) == V8_INT64_C(0)))) { return UseImmediate(node); } return UseRegister(node); } bool IsIntegerConstant(Node* node) { return (node->opcode() == IrOpcode::kInt32Constant); } int64_t GetIntegerConstantValue(Node* node) { DCHECK(node->opcode() == IrOpcode::kInt32Constant); return OpParameter(node); } bool IsFloatConstant(Node* node) { return (node->opcode() == IrOpcode::kFloat32Constant) || (node->opcode() == IrOpcode::kFloat64Constant); } double GetFloatConstantValue(Node* node) { if (node->opcode() == IrOpcode::kFloat32Constant) { return OpParameter(node); } DCHECK_EQ(IrOpcode::kFloat64Constant, node->opcode()); return OpParameter(node); } bool CanBeImmediate(Node* node, InstructionCode opcode) { Int32Matcher m(node); if (!m.HasValue()) return false; int32_t value = m.Value(); switch (ArchOpcodeField::decode(opcode)) { case kMipsShl: case kMipsSar: case kMipsShr: return is_uint5(value); case kMipsAdd: case kMipsAnd: case kMipsOr: case kMipsTst: case kMipsSub: case kMipsXor: return is_uint16(value); case kMipsLb: case kMipsLbu: case kMipsSb: case kMipsLh: case kMipsLhu: case kMipsSh: case kMipsLw: case kMipsSw: case kMipsLwc1: case kMipsSwc1: case kMipsLdc1: case kMipsSdc1: case kCheckedLoadInt8: case kCheckedLoadUint8: case kCheckedLoadInt16: case kCheckedLoadUint16: case kCheckedLoadWord32: case kCheckedStoreWord8: case kCheckedStoreWord16: case kCheckedStoreWord32: case kCheckedLoadFloat32: case kCheckedLoadFloat64: case kCheckedStoreFloat32: case kCheckedStoreFloat64: // true even for 32b values, offsets > 16b // are handled in assembler-mips.cc return is_int32(value); default: return is_int16(value); } } private: bool ImmediateFitsAddrMode1Instruction(int32_t imm) const { TRACE_UNIMPL(); return false; } }; static void VisitRRR(InstructionSelector* selector, ArchOpcode opcode, Node* node) { MipsOperandGenerator g(selector); selector->Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } static void VisitRR(InstructionSelector* selector, ArchOpcode opcode, Node* node) { MipsOperandGenerator g(selector); selector->Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } static void VisitRRO(InstructionSelector* selector, ArchOpcode opcode, Node* node) { MipsOperandGenerator g(selector); selector->Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseOperand(node->InputAt(1), opcode)); } bool TryMatchImmediate(InstructionSelector* selector, InstructionCode* opcode_return, Node* node, size_t* input_count_return, InstructionOperand* inputs) { MipsOperandGenerator g(selector); if (g.CanBeImmediate(node, *opcode_return)) { *opcode_return |= AddressingModeField::encode(kMode_MRI); inputs[0] = g.UseImmediate(node); *input_count_return = 1; return true; } return false; } static void VisitBinop(InstructionSelector* selector, Node* node, InstructionCode opcode, bool has_reverse_opcode, InstructionCode reverse_opcode, FlagsContinuation* cont) { MipsOperandGenerator g(selector); Int32BinopMatcher m(node); InstructionOperand inputs[4]; size_t input_count = 0; InstructionOperand outputs[2]; size_t output_count = 0; if (TryMatchImmediate(selector, &opcode, m.right().node(), &input_count, &inputs[1])) { inputs[0] = g.UseRegister(m.left().node()); input_count++; } else if (has_reverse_opcode && TryMatchImmediate(selector, &reverse_opcode, m.left().node(), &input_count, &inputs[1])) { inputs[0] = g.UseRegister(m.right().node()); opcode = reverse_opcode; input_count++; } else { inputs[input_count++] = g.UseRegister(m.left().node()); inputs[input_count++] = g.UseOperand(m.right().node(), opcode); } if (cont->IsBranch()) { inputs[input_count++] = g.Label(cont->true_block()); inputs[input_count++] = g.Label(cont->false_block()); } else if (cont->IsTrap()) { inputs[input_count++] = g.TempImmediate(cont->trap_id()); } if (cont->IsDeoptimize()) { // If we can deoptimize as a result of the binop, we need to make sure that // the deopt inputs are not overwritten by the binop result. One way // to achieve that is to declare the output register as same-as-first. outputs[output_count++] = g.DefineSameAsFirst(node); } else { outputs[output_count++] = g.DefineAsRegister(node); } if (cont->IsSet()) { outputs[output_count++] = g.DefineAsRegister(cont->result()); } DCHECK_NE(0u, input_count); DCHECK_NE(0u, output_count); DCHECK_GE(arraysize(inputs), input_count); DCHECK_GE(arraysize(outputs), output_count); opcode = cont->Encode(opcode); if (cont->IsDeoptimize()) { selector->EmitDeoptimize(opcode, output_count, outputs, input_count, inputs, cont->kind(), cont->reason(), cont->frame_state()); } else { selector->Emit(opcode, output_count, outputs, input_count, inputs); } } static void VisitBinop(InstructionSelector* selector, Node* node, InstructionCode opcode, bool has_reverse_opcode, InstructionCode reverse_opcode) { FlagsContinuation cont; VisitBinop(selector, node, opcode, has_reverse_opcode, reverse_opcode, &cont); } static void VisitBinop(InstructionSelector* selector, Node* node, InstructionCode opcode, FlagsContinuation* cont) { VisitBinop(selector, node, opcode, false, kArchNop, cont); } static void VisitBinop(InstructionSelector* selector, Node* node, InstructionCode opcode) { VisitBinop(selector, node, opcode, false, kArchNop); } void InstructionSelector::VisitLoad(Node* node) { LoadRepresentation load_rep = LoadRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); ArchOpcode opcode = kArchNop; switch (load_rep.representation()) { case MachineRepresentation::kFloat32: opcode = kMipsLwc1; break; case MachineRepresentation::kFloat64: opcode = kMipsLdc1; break; case MachineRepresentation::kBit: // Fall through. case MachineRepresentation::kWord8: opcode = load_rep.IsUnsigned() ? kMipsLbu : kMipsLb; break; case MachineRepresentation::kWord16: opcode = load_rep.IsUnsigned() ? kMipsLhu : kMipsLh; break; case MachineRepresentation::kTaggedSigned: // Fall through. case MachineRepresentation::kTaggedPointer: // Fall through. case MachineRepresentation::kTagged: // Fall through. case MachineRepresentation::kWord32: opcode = kMipsLw; break; case MachineRepresentation::kWord64: // Fall through. case MachineRepresentation::kSimd128: // Fall through. case MachineRepresentation::kSimd1x4: // Fall through. case MachineRepresentation::kSimd1x8: // Fall through. case MachineRepresentation::kSimd1x16: // Fall through. case MachineRepresentation::kNone: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired load opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), addr_reg, g.TempImmediate(0)); } } void InstructionSelector::VisitProtectedLoad(Node* node) { // TODO(eholk) UNIMPLEMENTED(); } void InstructionSelector::VisitStore(Node* node) { MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); Node* value = node->InputAt(2); StoreRepresentation store_rep = StoreRepresentationOf(node->op()); WriteBarrierKind write_barrier_kind = store_rep.write_barrier_kind(); MachineRepresentation rep = store_rep.representation(); // TODO(mips): I guess this could be done in a better way. if (write_barrier_kind != kNoWriteBarrier) { DCHECK(CanBeTaggedPointer(rep)); InstructionOperand inputs[3]; size_t input_count = 0; inputs[input_count++] = g.UseUniqueRegister(base); inputs[input_count++] = g.UseUniqueRegister(index); inputs[input_count++] = g.UseUniqueRegister(value); RecordWriteMode record_write_mode = RecordWriteMode::kValueIsAny; switch (write_barrier_kind) { case kNoWriteBarrier: UNREACHABLE(); break; case kMapWriteBarrier: record_write_mode = RecordWriteMode::kValueIsMap; break; case kPointerWriteBarrier: record_write_mode = RecordWriteMode::kValueIsPointer; break; case kFullWriteBarrier: record_write_mode = RecordWriteMode::kValueIsAny; break; } InstructionOperand temps[] = {g.TempRegister(), g.TempRegister()}; size_t const temp_count = arraysize(temps); InstructionCode code = kArchStoreWithWriteBarrier; code |= MiscField::encode(static_cast(record_write_mode)); Emit(code, 0, nullptr, input_count, inputs, temp_count, temps); } else { ArchOpcode opcode = kArchNop; switch (rep) { case MachineRepresentation::kFloat32: opcode = kMipsSwc1; break; case MachineRepresentation::kFloat64: opcode = kMipsSdc1; break; case MachineRepresentation::kBit: // Fall through. case MachineRepresentation::kWord8: opcode = kMipsSb; break; case MachineRepresentation::kWord16: opcode = kMipsSh; break; case MachineRepresentation::kTaggedSigned: // Fall through. case MachineRepresentation::kTaggedPointer: // Fall through. case MachineRepresentation::kTagged: // Fall through. case MachineRepresentation::kWord32: opcode = kMipsSw; break; case MachineRepresentation::kWord64: // Fall through. case MachineRepresentation::kSimd128: // Fall through. case MachineRepresentation::kSimd1x4: // Fall through. case MachineRepresentation::kSimd1x8: // Fall through. case MachineRepresentation::kSimd1x16: // Fall through. case MachineRepresentation::kNone: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), g.UseRegister(base), g.UseImmediate(index), g.UseRegisterOrImmediateZero(value)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired store opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), addr_reg, g.TempImmediate(0), g.UseRegisterOrImmediateZero(value)); } } } void InstructionSelector::VisitProtectedStore(Node* node) { // TODO(eholk) UNIMPLEMENTED(); } void InstructionSelector::VisitWord32And(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); if (m.left().IsWord32Shr() && CanCover(node, m.left().node()) && m.right().HasValue()) { uint32_t mask = m.right().Value(); uint32_t mask_width = base::bits::CountPopulation32(mask); uint32_t mask_msb = base::bits::CountLeadingZeros32(mask); if ((mask_width != 0) && (mask_msb + mask_width == 32)) { // The mask must be contiguous, and occupy the least-significant bits. DCHECK_EQ(0u, base::bits::CountTrailingZeros32(mask)); // Select Ext for And(Shr(x, imm), mask) where the mask is in the least // significant bits. Int32BinopMatcher mleft(m.left().node()); if (mleft.right().HasValue()) { // Any shift value can match; int32 shifts use `value % 32`. uint32_t lsb = mleft.right().Value() & 0x1f; // Ext cannot extract bits past the register size, however since // shifting the original value would have introduced some zeros we can // still use Ext with a smaller mask and the remaining bits will be // zeros. if (lsb + mask_width > 32) mask_width = 32 - lsb; if (lsb == 0 && mask_width == 32) { Emit(kArchNop, g.DefineSameAsFirst(node), g.Use(mleft.left().node())); } else { Emit(kMipsExt, g.DefineAsRegister(node), g.UseRegister(mleft.left().node()), g.TempImmediate(lsb), g.TempImmediate(mask_width)); } return; } // Other cases fall through to the normal And operation. } } if (m.right().HasValue()) { uint32_t mask = m.right().Value(); uint32_t shift = base::bits::CountPopulation32(~mask); uint32_t msb = base::bits::CountLeadingZeros32(~mask); if (shift != 0 && shift != 32 && msb + shift == 32) { // Insert zeros for (x >> K) << K => x & ~(2^K - 1) expression reduction // and remove constant loading of invereted mask. Emit(kMipsIns, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()), g.TempImmediate(0), g.TempImmediate(shift)); return; } } VisitBinop(this, node, kMipsAnd, true, kMipsAnd); } void InstructionSelector::VisitWord32Or(Node* node) { VisitBinop(this, node, kMipsOr, true, kMipsOr); } void InstructionSelector::VisitWord32Xor(Node* node) { Int32BinopMatcher m(node); if (m.left().IsWord32Or() && CanCover(node, m.left().node()) && m.right().Is(-1)) { Int32BinopMatcher mleft(m.left().node()); if (!mleft.right().HasValue()) { MipsOperandGenerator g(this); Emit(kMipsNor, g.DefineAsRegister(node), g.UseRegister(mleft.left().node()), g.UseRegister(mleft.right().node())); return; } } if (m.right().Is(-1)) { // Use Nor for bit negation and eliminate constant loading for xori. MipsOperandGenerator g(this); Emit(kMipsNor, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.TempImmediate(0)); return; } VisitBinop(this, node, kMipsXor, true, kMipsXor); } void InstructionSelector::VisitWord32Shl(Node* node) { Int32BinopMatcher m(node); if (m.left().IsWord32And() && CanCover(node, m.left().node()) && m.right().IsInRange(1, 31)) { MipsOperandGenerator g(this); Int32BinopMatcher mleft(m.left().node()); // Match Word32Shl(Word32And(x, mask), imm) to Shl where the mask is // contiguous, and the shift immediate non-zero. if (mleft.right().HasValue()) { uint32_t mask = mleft.right().Value(); uint32_t mask_width = base::bits::CountPopulation32(mask); uint32_t mask_msb = base::bits::CountLeadingZeros32(mask); if ((mask_width != 0) && (mask_msb + mask_width == 32)) { uint32_t shift = m.right().Value(); DCHECK_EQ(0u, base::bits::CountTrailingZeros32(mask)); DCHECK_NE(0u, shift); if ((shift + mask_width) >= 32) { // If the mask is contiguous and reaches or extends beyond the top // bit, only the shift is needed. Emit(kMipsShl, g.DefineAsRegister(node), g.UseRegister(mleft.left().node()), g.UseImmediate(m.right().node())); return; } } } } VisitRRO(this, kMipsShl, node); } void InstructionSelector::VisitWord32Shr(Node* node) { Int32BinopMatcher m(node); if (m.left().IsWord32And() && m.right().HasValue()) { uint32_t lsb = m.right().Value() & 0x1f; Int32BinopMatcher mleft(m.left().node()); if (mleft.right().HasValue()) { // Select Ext for Shr(And(x, mask), imm) where the result of the mask is // shifted into the least-significant bits. uint32_t mask = (mleft.right().Value() >> lsb) << lsb; unsigned mask_width = base::bits::CountPopulation32(mask); unsigned mask_msb = base::bits::CountLeadingZeros32(mask); if ((mask_msb + mask_width + lsb) == 32) { MipsOperandGenerator g(this); DCHECK_EQ(lsb, base::bits::CountTrailingZeros32(mask)); Emit(kMipsExt, g.DefineAsRegister(node), g.UseRegister(mleft.left().node()), g.TempImmediate(lsb), g.TempImmediate(mask_width)); return; } } } VisitRRO(this, kMipsShr, node); } void InstructionSelector::VisitWord32Sar(Node* node) { Int32BinopMatcher m(node); if (m.left().IsWord32Shl() && CanCover(node, m.left().node())) { Int32BinopMatcher mleft(m.left().node()); if (m.right().HasValue() && mleft.right().HasValue()) { MipsOperandGenerator g(this); uint32_t sar = m.right().Value(); uint32_t shl = mleft.right().Value(); if ((sar == shl) && (sar == 16)) { Emit(kMipsSeh, g.DefineAsRegister(node), g.UseRegister(mleft.left().node())); return; } else if ((sar == shl) && (sar == 24)) { Emit(kMipsSeb, g.DefineAsRegister(node), g.UseRegister(mleft.left().node())); return; } } } VisitRRO(this, kMipsSar, node); } static void VisitInt32PairBinop(InstructionSelector* selector, InstructionCode pair_opcode, InstructionCode single_opcode, Node* node) { MipsOperandGenerator g(selector); Node* projection1 = NodeProperties::FindProjection(node, 1); if (projection1) { // We use UseUniqueRegister here to avoid register sharing with the output // register. InstructionOperand inputs[] = {g.UseUniqueRegister(node->InputAt(0)), g.UseUniqueRegister(node->InputAt(1)), g.UseUniqueRegister(node->InputAt(2)), g.UseUniqueRegister(node->InputAt(3))}; InstructionOperand outputs[] = { g.DefineAsRegister(node), g.DefineAsRegister(NodeProperties::FindProjection(node, 1))}; selector->Emit(pair_opcode, 2, outputs, 4, inputs); } else { // The high word of the result is not used, so we emit the standard 32 bit // instruction. selector->Emit(single_opcode, g.DefineSameAsFirst(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(2))); } } void InstructionSelector::VisitInt32PairAdd(Node* node) { VisitInt32PairBinop(this, kMipsAddPair, kMipsAdd, node); } void InstructionSelector::VisitInt32PairSub(Node* node) { VisitInt32PairBinop(this, kMipsSubPair, kMipsSub, node); } void InstructionSelector::VisitInt32PairMul(Node* node) { VisitInt32PairBinop(this, kMipsMulPair, kMipsMul, node); } // Shared routine for multiple shift operations. static void VisitWord32PairShift(InstructionSelector* selector, InstructionCode opcode, Node* node) { MipsOperandGenerator g(selector); Int32Matcher m(node->InputAt(2)); InstructionOperand shift_operand; if (m.HasValue()) { shift_operand = g.UseImmediate(m.node()); } else { shift_operand = g.UseUniqueRegister(m.node()); } // We use UseUniqueRegister here to avoid register sharing with the output // register. InstructionOperand inputs[] = {g.UseUniqueRegister(node->InputAt(0)), g.UseUniqueRegister(node->InputAt(1)), shift_operand}; Node* projection1 = NodeProperties::FindProjection(node, 1); InstructionOperand outputs[2]; InstructionOperand temps[1]; int32_t output_count = 0; int32_t temp_count = 0; outputs[output_count++] = g.DefineAsRegister(node); if (projection1) { outputs[output_count++] = g.DefineAsRegister(projection1); } else { temps[temp_count++] = g.TempRegister(); } selector->Emit(opcode, output_count, outputs, 3, inputs, temp_count, temps); } void InstructionSelector::VisitWord32PairShl(Node* node) { VisitWord32PairShift(this, kMipsShlPair, node); } void InstructionSelector::VisitWord32PairShr(Node* node) { VisitWord32PairShift(this, kMipsShrPair, node); } void InstructionSelector::VisitWord32PairSar(Node* node) { VisitWord32PairShift(this, kMipsSarPair, node); } void InstructionSelector::VisitWord32Ror(Node* node) { VisitRRO(this, kMipsRor, node); } void InstructionSelector::VisitWord32Clz(Node* node) { VisitRR(this, kMipsClz, node); } void InstructionSelector::VisitWord32ReverseBits(Node* node) { UNREACHABLE(); } void InstructionSelector::VisitWord64ReverseBytes(Node* node) { UNREACHABLE(); } void InstructionSelector::VisitWord32ReverseBytes(Node* node) { MipsOperandGenerator g(this); Emit(kMipsByteSwap32, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitWord32Ctz(Node* node) { MipsOperandGenerator g(this); Emit(kMipsCtz, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitWord32Popcnt(Node* node) { MipsOperandGenerator g(this); Emit(kMipsPopcnt, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitInt32Add(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); // Select Lsa for (left + (left_of_right << imm)). if (m.right().opcode() == IrOpcode::kWord32Shl && CanCover(node, m.left().node()) && CanCover(node, m.right().node())) { Int32BinopMatcher mright(m.right().node()); if (mright.right().HasValue() && !m.left().HasValue()) { int32_t shift_value = static_cast(mright.right().Value()); Emit(kMipsLsa, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(mright.left().node()), g.TempImmediate(shift_value)); return; } } // Select Lsa for ((left_of_left << imm) + right). if (m.left().opcode() == IrOpcode::kWord32Shl && CanCover(node, m.right().node()) && CanCover(node, m.left().node())) { Int32BinopMatcher mleft(m.left().node()); if (mleft.right().HasValue() && !m.right().HasValue()) { int32_t shift_value = static_cast(mleft.right().Value()); Emit(kMipsLsa, g.DefineAsRegister(node), g.UseRegister(m.right().node()), g.UseRegister(mleft.left().node()), g.TempImmediate(shift_value)); return; } } VisitBinop(this, node, kMipsAdd, true, kMipsAdd); } void InstructionSelector::VisitInt32Sub(Node* node) { VisitBinop(this, node, kMipsSub); } void InstructionSelector::VisitInt32Mul(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); if (m.right().HasValue() && m.right().Value() > 0) { int32_t value = m.right().Value(); if (base::bits::IsPowerOfTwo32(value)) { Emit(kMipsShl | AddressingModeField::encode(kMode_None), g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.TempImmediate(WhichPowerOf2(value))); return; } if (base::bits::IsPowerOfTwo32(value - 1)) { Emit(kMipsLsa, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(m.left().node()), g.TempImmediate(WhichPowerOf2(value - 1))); return; } if (base::bits::IsPowerOfTwo32(value + 1)) { InstructionOperand temp = g.TempRegister(); Emit(kMipsShl | AddressingModeField::encode(kMode_None), temp, g.UseRegister(m.left().node()), g.TempImmediate(WhichPowerOf2(value + 1))); Emit(kMipsSub | AddressingModeField::encode(kMode_None), g.DefineAsRegister(node), temp, g.UseRegister(m.left().node())); return; } } VisitRRR(this, kMipsMul, node); } void InstructionSelector::VisitInt32MulHigh(Node* node) { VisitRRR(this, kMipsMulHigh, node); } void InstructionSelector::VisitUint32MulHigh(Node* node) { MipsOperandGenerator g(this); Emit(kMipsMulHighU, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } void InstructionSelector::VisitInt32Div(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); Emit(kMipsDiv, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()), g.UseRegister(m.right().node())); } void InstructionSelector::VisitUint32Div(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); Emit(kMipsDivU, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()), g.UseRegister(m.right().node())); } void InstructionSelector::VisitInt32Mod(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); Emit(kMipsMod, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(m.right().node())); } void InstructionSelector::VisitUint32Mod(Node* node) { MipsOperandGenerator g(this); Int32BinopMatcher m(node); Emit(kMipsModU, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(m.right().node())); } void InstructionSelector::VisitChangeFloat32ToFloat64(Node* node) { VisitRR(this, kMipsCvtDS, node); } void InstructionSelector::VisitRoundInt32ToFloat32(Node* node) { VisitRR(this, kMipsCvtSW, node); } void InstructionSelector::VisitRoundUint32ToFloat32(Node* node) { VisitRR(this, kMipsCvtSUw, node); } void InstructionSelector::VisitChangeInt32ToFloat64(Node* node) { VisitRR(this, kMipsCvtDW, node); } void InstructionSelector::VisitChangeUint32ToFloat64(Node* node) { VisitRR(this, kMipsCvtDUw, node); } void InstructionSelector::VisitTruncateFloat32ToInt32(Node* node) { VisitRR(this, kMipsTruncWS, node); } void InstructionSelector::VisitTruncateFloat32ToUint32(Node* node) { VisitRR(this, kMipsTruncUwS, node); } void InstructionSelector::VisitChangeFloat64ToInt32(Node* node) { MipsOperandGenerator g(this); Node* value = node->InputAt(0); // Match ChangeFloat64ToInt32(Float64Round##OP) to corresponding instruction // which does rounding and conversion to integer format. if (CanCover(node, value)) { switch (value->opcode()) { case IrOpcode::kFloat64RoundDown: Emit(kMipsFloorWD, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; case IrOpcode::kFloat64RoundUp: Emit(kMipsCeilWD, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; case IrOpcode::kFloat64RoundTiesEven: Emit(kMipsRoundWD, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; case IrOpcode::kFloat64RoundTruncate: Emit(kMipsTruncWD, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; default: break; } if (value->opcode() == IrOpcode::kChangeFloat32ToFloat64) { Node* next = value->InputAt(0); if (CanCover(value, next)) { // Match ChangeFloat64ToInt32(ChangeFloat32ToFloat64(Float64Round##OP)) switch (next->opcode()) { case IrOpcode::kFloat32RoundDown: Emit(kMipsFloorWS, g.DefineAsRegister(node), g.UseRegister(next->InputAt(0))); return; case IrOpcode::kFloat32RoundUp: Emit(kMipsCeilWS, g.DefineAsRegister(node), g.UseRegister(next->InputAt(0))); return; case IrOpcode::kFloat32RoundTiesEven: Emit(kMipsRoundWS, g.DefineAsRegister(node), g.UseRegister(next->InputAt(0))); return; case IrOpcode::kFloat32RoundTruncate: Emit(kMipsTruncWS, g.DefineAsRegister(node), g.UseRegister(next->InputAt(0))); return; default: Emit(kMipsTruncWS, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; } } else { // Match float32 -> float64 -> int32 representation change path. Emit(kMipsTruncWS, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; } } } VisitRR(this, kMipsTruncWD, node); } void InstructionSelector::VisitChangeFloat64ToUint32(Node* node) { VisitRR(this, kMipsTruncUwD, node); } void InstructionSelector::VisitTruncateFloat64ToUint32(Node* node) { VisitRR(this, kMipsTruncUwD, node); } void InstructionSelector::VisitTruncateFloat64ToFloat32(Node* node) { MipsOperandGenerator g(this); Node* value = node->InputAt(0); // Match TruncateFloat64ToFloat32(ChangeInt32ToFloat64) to corresponding // instruction. if (CanCover(node, value) && value->opcode() == IrOpcode::kChangeInt32ToFloat64) { Emit(kMipsCvtSW, g.DefineAsRegister(node), g.UseRegister(value->InputAt(0))); return; } VisitRR(this, kMipsCvtSD, node); } void InstructionSelector::VisitTruncateFloat64ToWord32(Node* node) { VisitRR(this, kArchTruncateDoubleToI, node); } void InstructionSelector::VisitRoundFloat64ToInt32(Node* node) { VisitRR(this, kMipsTruncWD, node); } void InstructionSelector::VisitBitcastFloat32ToInt32(Node* node) { VisitRR(this, kMipsFloat64ExtractLowWord32, node); } void InstructionSelector::VisitBitcastInt32ToFloat32(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat64InsertLowWord32, g.DefineAsRegister(node), ImmediateOperand(ImmediateOperand::INLINE, 0), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitFloat32Add(Node* node) { MipsOperandGenerator g(this); if (IsMipsArchVariant(kMips32r2)) { // Select Madd.S(z, x, y). Float32BinopMatcher m(node); if (m.left().IsFloat32Mul() && CanCover(node, m.left().node())) { // For Add.S(Mul.S(x, y), z): Float32BinopMatcher mleft(m.left().node()); Emit(kMipsMaddS, g.DefineAsRegister(node), g.UseRegister(m.right().node()), g.UseRegister(mleft.left().node()), g.UseRegister(mleft.right().node())); return; } if (m.right().IsFloat32Mul() && CanCover(node, m.right().node())) { // For Add.S(x, Mul.S(y, z)): Float32BinopMatcher mright(m.right().node()); Emit(kMipsMaddS, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(mright.left().node()), g.UseRegister(mright.right().node())); return; } } VisitRRR(this, kMipsAddS, node); } void InstructionSelector::VisitFloat64Add(Node* node) { MipsOperandGenerator g(this); if (IsMipsArchVariant(kMips32r2)) { // Select Madd.S(z, x, y). Float64BinopMatcher m(node); if (m.left().IsFloat64Mul() && CanCover(node, m.left().node())) { // For Add.D(Mul.D(x, y), z): Float64BinopMatcher mleft(m.left().node()); Emit(kMipsMaddD, g.DefineAsRegister(node), g.UseRegister(m.right().node()), g.UseRegister(mleft.left().node()), g.UseRegister(mleft.right().node())); return; } if (m.right().IsFloat64Mul() && CanCover(node, m.right().node())) { // For Add.D(x, Mul.D(y, z)): Float64BinopMatcher mright(m.right().node()); Emit(kMipsMaddD, g.DefineAsRegister(node), g.UseRegister(m.left().node()), g.UseRegister(mright.left().node()), g.UseRegister(mright.right().node())); return; } } VisitRRR(this, kMipsAddD, node); } void InstructionSelector::VisitFloat32Sub(Node* node) { MipsOperandGenerator g(this); if (IsMipsArchVariant(kMips32r2)) { // Select Madd.S(z, x, y). Float32BinopMatcher m(node); if (m.left().IsFloat32Mul() && CanCover(node, m.left().node())) { // For Sub.S(Mul.S(x,y), z) select Msub.S(z, x, y). Float32BinopMatcher mleft(m.left().node()); Emit(kMipsMsubS, g.DefineAsRegister(node), g.UseRegister(m.right().node()), g.UseRegister(mleft.left().node()), g.UseRegister(mleft.right().node())); return; } } VisitRRR(this, kMipsSubS, node); } void InstructionSelector::VisitFloat64Sub(Node* node) { MipsOperandGenerator g(this); if (IsMipsArchVariant(kMips32r2)) { // Select Madd.S(z, x, y). Float64BinopMatcher m(node); if (m.left().IsFloat64Mul() && CanCover(node, m.left().node())) { // For Sub.D(Mul.S(x,y), z) select Msub.D(z, x, y). Float64BinopMatcher mleft(m.left().node()); Emit(kMipsMsubD, g.DefineAsRegister(node), g.UseRegister(m.right().node()), g.UseRegister(mleft.left().node()), g.UseRegister(mleft.right().node())); return; } } VisitRRR(this, kMipsSubD, node); } void InstructionSelector::VisitFloat32Mul(Node* node) { VisitRRR(this, kMipsMulS, node); } void InstructionSelector::VisitFloat64Mul(Node* node) { VisitRRR(this, kMipsMulD, node); } void InstructionSelector::VisitFloat32Div(Node* node) { VisitRRR(this, kMipsDivS, node); } void InstructionSelector::VisitFloat64Div(Node* node) { VisitRRR(this, kMipsDivD, node); } void InstructionSelector::VisitFloat64Mod(Node* node) { MipsOperandGenerator g(this); Emit(kMipsModD, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f12), g.UseFixed(node->InputAt(1), f14))->MarkAsCall(); } void InstructionSelector::VisitFloat32Max(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat32Max, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } void InstructionSelector::VisitFloat64Max(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat64Max, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } void InstructionSelector::VisitFloat32Min(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat32Min, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } void InstructionSelector::VisitFloat64Min(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat64Min, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1))); } void InstructionSelector::VisitFloat32Abs(Node* node) { VisitRR(this, kMipsAbsS, node); } void InstructionSelector::VisitFloat64Abs(Node* node) { VisitRR(this, kMipsAbsD, node); } void InstructionSelector::VisitFloat32Sqrt(Node* node) { VisitRR(this, kMipsSqrtS, node); } void InstructionSelector::VisitFloat64Sqrt(Node* node) { VisitRR(this, kMipsSqrtD, node); } void InstructionSelector::VisitFloat32RoundDown(Node* node) { VisitRR(this, kMipsFloat32RoundDown, node); } void InstructionSelector::VisitFloat64RoundDown(Node* node) { VisitRR(this, kMipsFloat64RoundDown, node); } void InstructionSelector::VisitFloat32RoundUp(Node* node) { VisitRR(this, kMipsFloat32RoundUp, node); } void InstructionSelector::VisitFloat64RoundUp(Node* node) { VisitRR(this, kMipsFloat64RoundUp, node); } void InstructionSelector::VisitFloat32RoundTruncate(Node* node) { VisitRR(this, kMipsFloat32RoundTruncate, node); } void InstructionSelector::VisitFloat64RoundTruncate(Node* node) { VisitRR(this, kMipsFloat64RoundTruncate, node); } void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) { UNREACHABLE(); } void InstructionSelector::VisitFloat32RoundTiesEven(Node* node) { VisitRR(this, kMipsFloat32RoundTiesEven, node); } void InstructionSelector::VisitFloat64RoundTiesEven(Node* node) { VisitRR(this, kMipsFloat64RoundTiesEven, node); } void InstructionSelector::VisitFloat32Neg(Node* node) { VisitRR(this, kMipsNegS, node); } void InstructionSelector::VisitFloat64Neg(Node* node) { VisitRR(this, kMipsNegD, node); } void InstructionSelector::VisitFloat64Ieee754Binop(Node* node, InstructionCode opcode) { MipsOperandGenerator g(this); Emit(opcode, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f2), g.UseFixed(node->InputAt(1), f4)) ->MarkAsCall(); } void InstructionSelector::VisitFloat64Ieee754Unop(Node* node, InstructionCode opcode) { MipsOperandGenerator g(this); Emit(opcode, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f12)) ->MarkAsCall(); } void InstructionSelector::EmitPrepareArguments( ZoneVector* arguments, const CallDescriptor* descriptor, Node* node) { MipsOperandGenerator g(this); // Prepare for C function call. if (descriptor->IsCFunctionCall()) { Emit(kArchPrepareCallCFunction | MiscField::encode(static_cast(descriptor->ParameterCount())), 0, nullptr, 0, nullptr); // Poke any stack arguments. int slot = kCArgSlotCount; for (PushParameter input : (*arguments)) { if (input.node()) { Emit(kMipsStoreToStackSlot, g.NoOutput(), g.UseRegister(input.node()), g.TempImmediate(slot << kPointerSizeLog2)); ++slot; } } } else { // Possibly align stack here for functions. int push_count = static_cast(descriptor->StackParameterCount()); if (push_count > 0) { Emit(kMipsStackClaim, g.NoOutput(), g.TempImmediate(push_count << kPointerSizeLog2)); } for (size_t n = 0; n < arguments->size(); ++n) { PushParameter input = (*arguments)[n]; if (input.node()) { Emit(kMipsStoreToStackSlot, g.NoOutput(), g.UseRegister(input.node()), g.TempImmediate(n << kPointerSizeLog2)); } } } } bool InstructionSelector::IsTailCallAddressImmediate() { return false; } int InstructionSelector::GetTempsCountForTailCallFromJSFunction() { return 3; } void InstructionSelector::VisitUnalignedLoad(Node* node) { UnalignedLoadRepresentation load_rep = UnalignedLoadRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); ArchOpcode opcode = kArchNop; switch (load_rep.representation()) { case MachineRepresentation::kBit: // Fall through. case MachineRepresentation::kWord8: UNREACHABLE(); break; case MachineRepresentation::kWord16: opcode = load_rep.IsUnsigned() ? kMipsUlhu : kMipsUlh; break; case MachineRepresentation::kTaggedSigned: // Fall through. case MachineRepresentation::kTaggedPointer: // Fall through. case MachineRepresentation::kTagged: // Fall through. case MachineRepresentation::kWord32: opcode = kMipsUlw; break; case MachineRepresentation::kFloat32: opcode = kMipsUlwc1; break; case MachineRepresentation::kFloat64: opcode = kMipsUldc1; break; case MachineRepresentation::kWord64: // Fall through. case MachineRepresentation::kSimd128: // Fall through. case MachineRepresentation::kSimd1x4: // Fall through. case MachineRepresentation::kSimd1x8: // Fall through. case MachineRepresentation::kSimd1x16: // Fall through. case MachineRepresentation::kNone: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired load opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), addr_reg, g.TempImmediate(0)); } } void InstructionSelector::VisitUnalignedStore(Node* node) { MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); Node* value = node->InputAt(2); UnalignedStoreRepresentation rep = UnalignedStoreRepresentationOf(node->op()); // TODO(mips): I guess this could be done in a better way. ArchOpcode opcode = kArchNop; switch (rep) { case MachineRepresentation::kFloat32: opcode = kMipsUswc1; break; case MachineRepresentation::kFloat64: opcode = kMipsUsdc1; break; case MachineRepresentation::kBit: // Fall through. case MachineRepresentation::kWord8: UNREACHABLE(); break; case MachineRepresentation::kWord16: opcode = kMipsUsh; break; case MachineRepresentation::kTaggedSigned: // Fall through. case MachineRepresentation::kTaggedPointer: // Fall through. case MachineRepresentation::kTagged: // Fall through. case MachineRepresentation::kWord32: opcode = kMipsUsw; break; case MachineRepresentation::kWord64: // Fall through. case MachineRepresentation::kSimd128: // Fall through. case MachineRepresentation::kSimd1x4: // Fall through. case MachineRepresentation::kSimd1x8: // Fall through. case MachineRepresentation::kSimd1x16: // Fall through. case MachineRepresentation::kNone: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), g.UseRegister(base), g.UseImmediate(index), g.UseRegisterOrImmediateZero(value)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired store opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), addr_reg, g.TempImmediate(0), g.UseRegisterOrImmediateZero(value)); } } void InstructionSelector::VisitCheckedLoad(Node* node) { CheckedLoadRepresentation load_rep = CheckedLoadRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* const buffer = node->InputAt(0); Node* const offset = node->InputAt(1); Node* const length = node->InputAt(2); ArchOpcode opcode = kArchNop; switch (load_rep.representation()) { case MachineRepresentation::kWord8: opcode = load_rep.IsSigned() ? kCheckedLoadInt8 : kCheckedLoadUint8; break; case MachineRepresentation::kWord16: opcode = load_rep.IsSigned() ? kCheckedLoadInt16 : kCheckedLoadUint16; break; case MachineRepresentation::kWord32: opcode = kCheckedLoadWord32; break; case MachineRepresentation::kFloat32: opcode = kCheckedLoadFloat32; break; case MachineRepresentation::kFloat64: opcode = kCheckedLoadFloat64; break; case MachineRepresentation::kBit: // Fall through. case MachineRepresentation::kTaggedSigned: // Fall through. case MachineRepresentation::kTaggedPointer: // Fall through. case MachineRepresentation::kTagged: // Fall through. case MachineRepresentation::kWord64: // Fall through. case MachineRepresentation::kSimd128: // Fall through. case MachineRepresentation::kSimd1x4: // Fall through. case MachineRepresentation::kSimd1x8: // Fall through. case MachineRepresentation::kSimd1x16: // Fall through. case MachineRepresentation::kNone: UNREACHABLE(); return; } InstructionOperand offset_operand = g.CanBeImmediate(offset, opcode) ? g.UseImmediate(offset) : g.UseRegister(offset); InstructionOperand length_operand = (!g.CanBeImmediate(offset, opcode)) ? g.CanBeImmediate(length, opcode) ? g.UseImmediate(length) : g.UseRegister(length) : g.UseRegister(length); Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), offset_operand, length_operand, g.UseRegister(buffer)); } void InstructionSelector::VisitCheckedStore(Node* node) { MachineRepresentation rep = CheckedStoreRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* const buffer = node->InputAt(0); Node* const offset = node->InputAt(1); Node* const length = node->InputAt(2); Node* const value = node->InputAt(3); ArchOpcode opcode = kArchNop; switch (rep) { case MachineRepresentation::kWord8: opcode = kCheckedStoreWord8; break; case MachineRepresentation::kWord16: opcode = kCheckedStoreWord16; break; case MachineRepresentation::kWord32: opcode = kCheckedStoreWord32; break; case MachineRepresentation::kFloat32: opcode = kCheckedStoreFloat32; break; case MachineRepresentation::kFloat64: opcode = kCheckedStoreFloat64; break; default: UNREACHABLE(); return; } InstructionOperand offset_operand = g.CanBeImmediate(offset, opcode) ? g.UseImmediate(offset) : g.UseRegister(offset); InstructionOperand length_operand = (!g.CanBeImmediate(offset, opcode)) ? g.CanBeImmediate(length, opcode) ? g.UseImmediate(length) : g.UseRegister(length) : g.UseRegister(length); Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), offset_operand, length_operand, g.UseRegisterOrImmediateZero(value), g.UseRegister(buffer)); } namespace { // Shared routine for multiple compare operations. static void VisitCompare(InstructionSelector* selector, InstructionCode opcode, InstructionOperand left, InstructionOperand right, FlagsContinuation* cont) { MipsOperandGenerator g(selector); opcode = cont->Encode(opcode); if (cont->IsBranch()) { selector->Emit(opcode, g.NoOutput(), left, right, g.Label(cont->true_block()), g.Label(cont->false_block())); } else if (cont->IsDeoptimize()) { selector->EmitDeoptimize(opcode, g.NoOutput(), left, right, cont->kind(), cont->reason(), cont->frame_state()); } else if (cont->IsSet()) { selector->Emit(opcode, g.DefineAsRegister(cont->result()), left, right); } else { DCHECK(cont->IsTrap()); selector->Emit(opcode, g.NoOutput(), left, right, g.TempImmediate(cont->trap_id())); } } // Shared routine for multiple float32 compare operations. void VisitFloat32Compare(InstructionSelector* selector, Node* node, FlagsContinuation* cont) { MipsOperandGenerator g(selector); Float32BinopMatcher m(node); InstructionOperand lhs, rhs; lhs = m.left().IsZero() ? g.UseImmediate(m.left().node()) : g.UseRegister(m.left().node()); rhs = m.right().IsZero() ? g.UseImmediate(m.right().node()) : g.UseRegister(m.right().node()); VisitCompare(selector, kMipsCmpS, lhs, rhs, cont); } // Shared routine for multiple float64 compare operations. void VisitFloat64Compare(InstructionSelector* selector, Node* node, FlagsContinuation* cont) { MipsOperandGenerator g(selector); Float64BinopMatcher m(node); InstructionOperand lhs, rhs; lhs = m.left().IsZero() ? g.UseImmediate(m.left().node()) : g.UseRegister(m.left().node()); rhs = m.right().IsZero() ? g.UseImmediate(m.right().node()) : g.UseRegister(m.right().node()); VisitCompare(selector, kMipsCmpD, lhs, rhs, cont); } // Shared routine for multiple word compare operations. void VisitWordCompare(InstructionSelector* selector, Node* node, InstructionCode opcode, FlagsContinuation* cont, bool commutative) { MipsOperandGenerator g(selector); Node* left = node->InputAt(0); Node* right = node->InputAt(1); // Match immediates on left or right side of comparison. if (g.CanBeImmediate(right, opcode)) { if (opcode == kMipsTst) { VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right), cont); } else { switch (cont->condition()) { case kEqual: case kNotEqual: if (cont->IsSet()) { VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right), cont); } else { VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right), cont); } break; case kSignedLessThan: case kSignedGreaterThanOrEqual: case kUnsignedLessThan: case kUnsignedGreaterThanOrEqual: VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right), cont); break; default: VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right), cont); } } } else if (g.CanBeImmediate(left, opcode)) { if (!commutative) cont->Commute(); if (opcode == kMipsTst) { VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left), cont); } else { switch (cont->condition()) { case kEqual: case kNotEqual: if (cont->IsSet()) { VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left), cont); } else { VisitCompare(selector, opcode, g.UseRegister(right), g.UseRegister(left), cont); } break; case kSignedLessThan: case kSignedGreaterThanOrEqual: case kUnsignedLessThan: case kUnsignedGreaterThanOrEqual: VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left), cont); break; default: VisitCompare(selector, opcode, g.UseRegister(right), g.UseRegister(left), cont); } } } else { VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right), cont); } } void VisitWordCompare(InstructionSelector* selector, Node* node, FlagsContinuation* cont) { VisitWordCompare(selector, node, kMipsCmp, cont, false); } // Shared routine for word comparisons against zero. void VisitWordCompareZero(InstructionSelector* selector, Node* user, Node* value, FlagsContinuation* cont) { // Try to combine with comparisons against 0 by simply inverting the branch. while (value->opcode() == IrOpcode::kWord32Equal && selector->CanCover(user, value)) { Int32BinopMatcher m(value); if (!m.right().Is(0)) break; user = value; value = m.left().node(); cont->Negate(); } if (selector->CanCover(user, value)) { switch (value->opcode()) { case IrOpcode::kWord32Equal: cont->OverwriteAndNegateIfEqual(kEqual); return VisitWordCompare(selector, value, cont); case IrOpcode::kInt32LessThan: cont->OverwriteAndNegateIfEqual(kSignedLessThan); return VisitWordCompare(selector, value, cont); case IrOpcode::kInt32LessThanOrEqual: cont->OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); return VisitWordCompare(selector, value, cont); case IrOpcode::kUint32LessThan: cont->OverwriteAndNegateIfEqual(kUnsignedLessThan); return VisitWordCompare(selector, value, cont); case IrOpcode::kUint32LessThanOrEqual: cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); return VisitWordCompare(selector, value, cont); case IrOpcode::kFloat32Equal: cont->OverwriteAndNegateIfEqual(kEqual); return VisitFloat32Compare(selector, value, cont); case IrOpcode::kFloat32LessThan: cont->OverwriteAndNegateIfEqual(kUnsignedLessThan); return VisitFloat32Compare(selector, value, cont); case IrOpcode::kFloat32LessThanOrEqual: cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); return VisitFloat32Compare(selector, value, cont); case IrOpcode::kFloat64Equal: cont->OverwriteAndNegateIfEqual(kEqual); return VisitFloat64Compare(selector, value, cont); case IrOpcode::kFloat64LessThan: cont->OverwriteAndNegateIfEqual(kUnsignedLessThan); return VisitFloat64Compare(selector, value, cont); case IrOpcode::kFloat64LessThanOrEqual: cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); return VisitFloat64Compare(selector, value, cont); case IrOpcode::kProjection: // Check if this is the overflow output projection of an // WithOverflow node. if (ProjectionIndexOf(value->op()) == 1u) { // We cannot combine the WithOverflow with this branch // unless the 0th projection (the use of the actual value of the // is either nullptr, which means there's no use of the // actual value, or was already defined, which means it is scheduled // *AFTER* this branch). Node* const node = value->InputAt(0); Node* const result = NodeProperties::FindProjection(node, 0); if (!result || selector->IsDefined(result)) { switch (node->opcode()) { case IrOpcode::kInt32AddWithOverflow: cont->OverwriteAndNegateIfEqual(kOverflow); return VisitBinop(selector, node, kMipsAddOvf, cont); case IrOpcode::kInt32SubWithOverflow: cont->OverwriteAndNegateIfEqual(kOverflow); return VisitBinop(selector, node, kMipsSubOvf, cont); case IrOpcode::kInt32MulWithOverflow: cont->OverwriteAndNegateIfEqual(kOverflow); return VisitBinop(selector, node, kMipsMulOvf, cont); default: break; } } } break; case IrOpcode::kWord32And: return VisitWordCompare(selector, value, kMipsTst, cont, true); default: break; } } // Continuation could not be combined with a compare, emit compare against 0. MipsOperandGenerator g(selector); InstructionCode const opcode = cont->Encode(kMipsCmp); InstructionOperand const value_operand = g.UseRegister(value); if (cont->IsBranch()) { selector->Emit(opcode, g.NoOutput(), value_operand, g.TempImmediate(0), g.Label(cont->true_block()), g.Label(cont->false_block())); } else if (cont->IsDeoptimize()) { selector->EmitDeoptimize(opcode, g.NoOutput(), value_operand, g.TempImmediate(0), cont->kind(), cont->reason(), cont->frame_state()); } else if (cont->IsSet()) { selector->Emit(opcode, g.DefineAsRegister(cont->result()), value_operand, g.TempImmediate(0)); } else { DCHECK(cont->IsTrap()); selector->Emit(opcode, g.NoOutput(), value_operand, g.TempImmediate(0), g.TempImmediate(cont->trap_id())); } } } // namespace void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, BasicBlock* fbranch) { FlagsContinuation cont(kNotEqual, tbranch, fbranch); VisitWordCompareZero(this, branch, branch->InputAt(0), &cont); } void InstructionSelector::VisitDeoptimizeIf(Node* node) { DeoptimizeParameters p = DeoptimizeParametersOf(node->op()); FlagsContinuation cont = FlagsContinuation::ForDeoptimize( kNotEqual, p.kind(), p.reason(), node->InputAt(1)); VisitWordCompareZero(this, node, node->InputAt(0), &cont); } void InstructionSelector::VisitDeoptimizeUnless(Node* node) { DeoptimizeParameters p = DeoptimizeParametersOf(node->op()); FlagsContinuation cont = FlagsContinuation::ForDeoptimize( kEqual, p.kind(), p.reason(), node->InputAt(1)); VisitWordCompareZero(this, node, node->InputAt(0), &cont); } void InstructionSelector::VisitTrapIf(Node* node, Runtime::FunctionId func_id) { FlagsContinuation cont = FlagsContinuation::ForTrap(kNotEqual, func_id, node->InputAt(1)); VisitWordCompareZero(this, node, node->InputAt(0), &cont); } void InstructionSelector::VisitTrapUnless(Node* node, Runtime::FunctionId func_id) { FlagsContinuation cont = FlagsContinuation::ForTrap(kEqual, func_id, node->InputAt(1)); VisitWordCompareZero(this, node, node->InputAt(0), &cont); } void InstructionSelector::VisitSwitch(Node* node, const SwitchInfo& sw) { MipsOperandGenerator g(this); InstructionOperand value_operand = g.UseRegister(node->InputAt(0)); // Emit either ArchTableSwitch or ArchLookupSwitch. size_t table_space_cost = 9 + sw.value_range; size_t table_time_cost = 3; size_t lookup_space_cost = 2 + 2 * sw.case_count; size_t lookup_time_cost = sw.case_count; if (sw.case_count > 0 && table_space_cost + 3 * table_time_cost <= lookup_space_cost + 3 * lookup_time_cost && sw.min_value > std::numeric_limits::min()) { InstructionOperand index_operand = value_operand; if (sw.min_value) { index_operand = g.TempRegister(); Emit(kMipsSub, index_operand, value_operand, g.TempImmediate(sw.min_value)); } // Generate a table lookup. return EmitTableSwitch(sw, index_operand); } // Generate a sequence of conditional jumps. return EmitLookupSwitch(sw, value_operand); } void InstructionSelector::VisitWord32Equal(Node* const node) { FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node); Int32BinopMatcher m(node); if (m.right().Is(0)) { return VisitWordCompareZero(this, m.node(), m.left().node(), &cont); } VisitWordCompare(this, node, &cont); } void InstructionSelector::VisitInt32LessThan(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kSignedLessThan, node); VisitWordCompare(this, node, &cont); } void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kSignedLessThanOrEqual, node); VisitWordCompare(this, node, &cont); } void InstructionSelector::VisitUint32LessThan(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node); VisitWordCompare(this, node, &cont); } void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node); VisitWordCompare(this, node, &cont); } void InstructionSelector::VisitInt32AddWithOverflow(Node* node) { if (Node* ovf = NodeProperties::FindProjection(node, 1)) { FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf); return VisitBinop(this, node, kMipsAddOvf, &cont); } FlagsContinuation cont; VisitBinop(this, node, kMipsAddOvf, &cont); } void InstructionSelector::VisitInt32SubWithOverflow(Node* node) { if (Node* ovf = NodeProperties::FindProjection(node, 1)) { FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf); return VisitBinop(this, node, kMipsSubOvf, &cont); } FlagsContinuation cont; VisitBinop(this, node, kMipsSubOvf, &cont); } void InstructionSelector::VisitInt32MulWithOverflow(Node* node) { if (Node* ovf = NodeProperties::FindProjection(node, 1)) { FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf); return VisitBinop(this, node, kMipsMulOvf, &cont); } FlagsContinuation cont; VisitBinop(this, node, kMipsMulOvf, &cont); } void InstructionSelector::VisitFloat32Equal(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node); VisitFloat32Compare(this, node, &cont); } void InstructionSelector::VisitFloat32LessThan(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node); VisitFloat32Compare(this, node, &cont); } void InstructionSelector::VisitFloat32LessThanOrEqual(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node); VisitFloat32Compare(this, node, &cont); } void InstructionSelector::VisitFloat64Equal(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node); VisitFloat64Compare(this, node, &cont); } void InstructionSelector::VisitFloat64LessThan(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node); VisitFloat64Compare(this, node, &cont); } void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) { FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node); VisitFloat64Compare(this, node, &cont); } void InstructionSelector::VisitFloat64ExtractLowWord32(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat64ExtractLowWord32, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitFloat64ExtractHighWord32(Node* node) { MipsOperandGenerator g(this); Emit(kMipsFloat64ExtractHighWord32, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); } void InstructionSelector::VisitFloat64InsertLowWord32(Node* node) { MipsOperandGenerator g(this); Node* left = node->InputAt(0); Node* right = node->InputAt(1); Emit(kMipsFloat64InsertLowWord32, g.DefineSameAsFirst(node), g.UseRegister(left), g.UseRegister(right)); } void InstructionSelector::VisitFloat64InsertHighWord32(Node* node) { MipsOperandGenerator g(this); Node* left = node->InputAt(0); Node* right = node->InputAt(1); Emit(kMipsFloat64InsertHighWord32, g.DefineSameAsFirst(node), g.UseRegister(left), g.UseRegister(right)); } void InstructionSelector::VisitFloat64SilenceNaN(Node* node) { MipsOperandGenerator g(this); Node* left = node->InputAt(0); InstructionOperand temps[] = {g.TempRegister()}; Emit(kMipsFloat64SilenceNaN, g.DefineSameAsFirst(node), g.UseRegister(left), arraysize(temps), temps); } void InstructionSelector::VisitAtomicLoad(Node* node) { LoadRepresentation load_rep = LoadRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); ArchOpcode opcode = kArchNop; switch (load_rep.representation()) { case MachineRepresentation::kWord8: opcode = load_rep.IsSigned() ? kAtomicLoadInt8 : kAtomicLoadUint8; break; case MachineRepresentation::kWord16: opcode = load_rep.IsSigned() ? kAtomicLoadInt16 : kAtomicLoadUint16; break; case MachineRepresentation::kWord32: opcode = kAtomicLoadWord32; break; default: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired load opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.DefineAsRegister(node), addr_reg, g.TempImmediate(0)); } } void InstructionSelector::VisitAtomicStore(Node* node) { MachineRepresentation rep = AtomicStoreRepresentationOf(node->op()); MipsOperandGenerator g(this); Node* base = node->InputAt(0); Node* index = node->InputAt(1); Node* value = node->InputAt(2); ArchOpcode opcode = kArchNop; switch (rep) { case MachineRepresentation::kWord8: opcode = kAtomicStoreWord8; break; case MachineRepresentation::kWord16: opcode = kAtomicStoreWord16; break; case MachineRepresentation::kWord32: opcode = kAtomicStoreWord32; break; default: UNREACHABLE(); return; } if (g.CanBeImmediate(index, opcode)) { Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), g.UseRegister(base), g.UseImmediate(index), g.UseRegisterOrImmediateZero(value)); } else { InstructionOperand addr_reg = g.TempRegister(); Emit(kMipsAdd | AddressingModeField::encode(kMode_None), addr_reg, g.UseRegister(index), g.UseRegister(base)); // Emit desired store opcode, using temp addr_reg. Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(), addr_reg, g.TempImmediate(0), g.UseRegisterOrImmediateZero(value)); } } // static MachineOperatorBuilder::Flags InstructionSelector::SupportedMachineOperatorFlags() { MachineOperatorBuilder::Flags flags = MachineOperatorBuilder::kNoFlags; if ((IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) && IsFp64Mode()) { flags |= MachineOperatorBuilder::kFloat64RoundDown | MachineOperatorBuilder::kFloat64RoundUp | MachineOperatorBuilder::kFloat64RoundTruncate | MachineOperatorBuilder::kFloat64RoundTiesEven; } return flags | MachineOperatorBuilder::kWord32Ctz | MachineOperatorBuilder::kWord32Popcnt | MachineOperatorBuilder::kInt32DivIsSafe | MachineOperatorBuilder::kUint32DivIsSafe | MachineOperatorBuilder::kWord32ShiftIsSafe | MachineOperatorBuilder::kFloat32RoundDown | MachineOperatorBuilder::kFloat32RoundUp | MachineOperatorBuilder::kFloat32RoundTruncate | MachineOperatorBuilder::kFloat32RoundTiesEven | MachineOperatorBuilder::kWord32ReverseBytes | MachineOperatorBuilder::kWord64ReverseBytes; } // static MachineOperatorBuilder::AlignmentRequirements InstructionSelector::AlignmentRequirements() { if (IsMipsArchVariant(kMips32r6)) { return MachineOperatorBuilder::AlignmentRequirements:: FullUnalignedAccessSupport(); } else { DCHECK(IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r1) || IsMipsArchVariant(kMips32r2)); return MachineOperatorBuilder::AlignmentRequirements:: NoUnalignedAccessSupport(); } } } // namespace compiler } // namespace internal } // namespace v8