diff --git a/src/mir/Lowering.cpp b/src/mir/Lowering.cpp index 4a5395d..1b69997 100644 --- a/src/mir/Lowering.cpp +++ b/src/mir/Lowering.cpp @@ -368,6 +368,33 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function, int dst_slot = function.CreateFrameIndex(4); slots.emplace(&inst, dst_slot); + if (inst.GetOpcode() == ir::Opcode::Mul) { + const ir::Value* variable = nullptr; + int constant = 0; + if (auto* lhs_const = dynamic_cast(bin.GetLhs())) { + variable = bin.GetRhs(); + constant = lhs_const->GetValue(); + } else if (auto* rhs_const = dynamic_cast(bin.GetRhs())) { + variable = bin.GetLhs(); + constant = rhs_const->GetValue(); + } + + if (variable && constant > 1) { + EmitValueToReg(variable, PhysReg::W8, slots, block); + if (IsPowerOfTwo(static_cast(constant))) { + block.Append(Opcode::LslImm, {Operand::Reg(PhysReg::W8), Operand::Reg(PhysReg::W8), Operand::Imm(Log2(static_cast(constant)))}); + block.Append(Opcode::StoreStack, {Operand::Reg(PhysReg::W8), Operand::FrameIndex(dst_slot)}); + return; + } + if (IsPowerOfTwo(static_cast(constant - 1))) { + block.Append(Opcode::LslImm, {Operand::Reg(PhysReg::W9), Operand::Reg(PhysReg::W8), Operand::Imm(Log2(static_cast(constant - 1)))}); + block.Append(Opcode::AddRR, {Operand::Reg(PhysReg::W8), Operand::Reg(PhysReg::W9), Operand::Reg(PhysReg::W8)}); + block.Append(Opcode::StoreStack, {Operand::Reg(PhysReg::W8), Operand::FrameIndex(dst_slot)}); + return; + } + } + } + EmitValueToReg(bin.GetLhs(), PhysReg::W8, slots, block); EmitValueToReg(bin.GetRhs(), PhysReg::W9, slots, block);