Path: blob/main/contrib/llvm-project/llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp
35269 views
//=== MicroMipsSizeReduction.cpp - MicroMips size reduction pass --------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///\file8/// This pass is used to reduce the size of instructions where applicable.9///10/// TODO: Implement microMIPS64 support.11//===----------------------------------------------------------------------===//12#include "Mips.h"13#include "MipsInstrInfo.h"14#include "MipsSubtarget.h"15#include "llvm/ADT/Statistic.h"16#include "llvm/CodeGen/MachineFunctionPass.h"17#include "llvm/Support/Debug.h"1819using namespace llvm;2021#define DEBUG_TYPE "micromips-reduce-size"22#define MICROMIPS_SIZE_REDUCE_NAME "MicroMips instruction size reduce pass"2324STATISTIC(NumReduced, "Number of instructions reduced (32-bit to 16-bit ones, "25"or two instructions into one");2627namespace {2829/// Order of operands to transfer30// TODO: Will be extended when additional optimizations are added31enum OperandTransfer {32OT_NA, ///< Not applicable33OT_OperandsAll, ///< Transfer all operands34OT_Operands02, ///< Transfer operands 0 and 235OT_Operand2, ///< Transfer just operand 236OT_OperandsXOR, ///< Transfer operands for XOR1637OT_OperandsLwp, ///< Transfer operands for LWP38OT_OperandsSwp, ///< Transfer operands for SWP39OT_OperandsMovep, ///< Transfer operands for MOVEP40};4142/// Reduction type43// TODO: Will be extended when additional optimizations are added44enum ReduceType {45RT_TwoInstr, ///< Reduce two instructions into one instruction46RT_OneInstr ///< Reduce one instruction into a smaller instruction47};4849// Information about immediate field restrictions50struct ImmField {51ImmField() : ImmFieldOperand(-1), Shift(0), LBound(0), HBound(0) {}52ImmField(uint8_t Shift, int16_t LBound, int16_t HBound,53int8_t ImmFieldOperand)54: ImmFieldOperand(ImmFieldOperand), Shift(Shift), LBound(LBound),55HBound(HBound) {}56int8_t ImmFieldOperand; // Immediate operand, -1 if it does not exist57uint8_t Shift; // Shift value58int16_t LBound; // Low bound of the immediate operand59int16_t HBound; // High bound of the immediate operand60};6162/// Information about operands63// TODO: Will be extended when additional optimizations are added64struct OpInfo {65OpInfo(enum OperandTransfer TransferOperands)66: TransferOperands(TransferOperands) {}67OpInfo() : TransferOperands(OT_NA) {}6869enum OperandTransfer70TransferOperands; ///< Operands to transfer to the new instruction71};7273// Information about opcodes74struct OpCodes {75OpCodes(unsigned WideOpc, unsigned NarrowOpc)76: WideOpc(WideOpc), NarrowOpc(NarrowOpc) {}7778unsigned WideOpc; ///< Wide opcode79unsigned NarrowOpc; ///< Narrow opcode80};8182typedef struct ReduceEntryFunArgs ReduceEntryFunArgs;8384/// ReduceTable - A static table with information on mapping from wide85/// opcodes to narrow86struct ReduceEntry {8788enum ReduceType eRType; ///< Reduction type89bool (*ReduceFunction)(90ReduceEntryFunArgs *Arguments); ///< Pointer to reduce function91struct OpCodes Ops; ///< All relevant OpCodes92struct OpInfo OpInf; ///< Characteristics of operands93struct ImmField Imm; ///< Characteristics of immediate field9495ReduceEntry(enum ReduceType RType, struct OpCodes Op,96bool (*F)(ReduceEntryFunArgs *Arguments), struct OpInfo OpInf,97struct ImmField Imm)98: eRType(RType), ReduceFunction(F), Ops(Op), OpInf(OpInf), Imm(Imm) {}99100unsigned NarrowOpc() const { return Ops.NarrowOpc; }101unsigned WideOpc() const { return Ops.WideOpc; }102int16_t LBound() const { return Imm.LBound; }103int16_t HBound() const { return Imm.HBound; }104uint8_t Shift() const { return Imm.Shift; }105int8_t ImmField() const { return Imm.ImmFieldOperand; }106enum OperandTransfer TransferOperands() const {107return OpInf.TransferOperands;108}109enum ReduceType RType() const { return eRType; }110111// operator used by std::equal_range112bool operator<(const unsigned int r) const { return (WideOpc() < r); }113114// operator used by std::equal_range115friend bool operator<(const unsigned int r, const struct ReduceEntry &re) {116return (r < re.WideOpc());117}118};119120// Function arguments for ReduceFunction121struct ReduceEntryFunArgs {122MachineInstr *MI; // Instruction123const ReduceEntry &Entry; // Entry field124MachineBasicBlock::instr_iterator125&NextMII; // Iterator to next instruction in block126127ReduceEntryFunArgs(MachineInstr *argMI, const ReduceEntry &argEntry,128MachineBasicBlock::instr_iterator &argNextMII)129: MI(argMI), Entry(argEntry), NextMII(argNextMII) {}130};131132typedef llvm::SmallVector<ReduceEntry, 32> ReduceEntryVector;133134class MicroMipsSizeReduce : public MachineFunctionPass {135public:136static char ID;137MicroMipsSizeReduce();138139static const MipsInstrInfo *MipsII;140const MipsSubtarget *Subtarget;141142bool runOnMachineFunction(MachineFunction &MF) override;143144llvm::StringRef getPassName() const override {145return "microMIPS instruction size reduction pass";146}147148private:149/// Reduces width of instructions in the specified basic block.150bool ReduceMBB(MachineBasicBlock &MBB);151152/// Attempts to reduce MI, returns true on success.153bool ReduceMI(const MachineBasicBlock::instr_iterator &MII,154MachineBasicBlock::instr_iterator &NextMII);155156// Attempts to reduce LW/SW instruction into LWSP/SWSP,157// returns true on success.158static bool ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments);159160// Attempts to reduce two LW/SW instructions into LWP/SWP instruction,161// returns true on success.162static bool ReduceXWtoXWP(ReduceEntryFunArgs *Arguments);163164// Attempts to reduce LBU/LHU instruction into LBU16/LHU16,165// returns true on success.166static bool ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments);167168// Attempts to reduce SB/SH instruction into SB16/SH16,169// returns true on success.170static bool ReduceSXtoSX16(ReduceEntryFunArgs *Arguments);171172// Attempts to reduce two MOVE instructions into MOVEP instruction,173// returns true on success.174static bool ReduceMoveToMovep(ReduceEntryFunArgs *Arguments);175176// Attempts to reduce arithmetic instructions, returns true on success.177static bool ReduceArithmeticInstructions(ReduceEntryFunArgs *Arguments);178179// Attempts to reduce ADDIU into ADDIUSP instruction,180// returns true on success.181static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments);182183// Attempts to reduce ADDIU into ADDIUR1SP instruction,184// returns true on success.185static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs *Arguments);186187// Attempts to reduce XOR into XOR16 instruction,188// returns true on success.189static bool ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments);190191// Changes opcode of an instruction, replaces an instruction with a192// new one, or replaces two instructions with a new instruction193// depending on their order i.e. if these are consecutive forward194// or consecutive backward195static bool ReplaceInstruction(MachineInstr *MI, const ReduceEntry &Entry,196MachineInstr *MI2 = nullptr,197bool ConsecutiveForward = true);198199// Table with transformation rules for each instruction.200static ReduceEntryVector ReduceTable;201};202203char MicroMipsSizeReduce::ID = 0;204const MipsInstrInfo *MicroMipsSizeReduce::MipsII;205206// This table must be sorted by WideOpc as a main criterion and207// ReduceType as a sub-criterion (when wide opcodes are the same).208ReduceEntryVector MicroMipsSizeReduce::ReduceTable = {209210// ReduceType, OpCodes, ReduceFunction,211// OpInfo(TransferOperands),212// ImmField(Shift, LBound, HBound, ImmFieldPosition)213{RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUR1SP_MM),214ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},215{RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUSP_MM), ReduceADDIUToADDIUSP,216OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},217{RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUR1SP_MM),218ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},219{RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUSP_MM),220ReduceADDIUToADDIUSP, OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},221{RT_OneInstr, OpCodes(Mips::ADDu, Mips::ADDU16_MM),222ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),223ImmField(0, 0, 0, -1)},224{RT_OneInstr, OpCodes(Mips::ADDu_MM, Mips::ADDU16_MM),225ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),226ImmField(0, 0, 0, -1)},227{RT_OneInstr, OpCodes(Mips::LBu, Mips::LBU16_MM), ReduceLXUtoLXU16,228OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},229{RT_OneInstr, OpCodes(Mips::LBu_MM, Mips::LBU16_MM), ReduceLXUtoLXU16,230OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},231{RT_OneInstr, OpCodes(Mips::LEA_ADDiu, Mips::ADDIUR1SP_MM),232ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},233{RT_OneInstr, OpCodes(Mips::LEA_ADDiu_MM, Mips::ADDIUR1SP_MM),234ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},235{RT_OneInstr, OpCodes(Mips::LHu, Mips::LHU16_MM), ReduceLXUtoLXU16,236OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},237{RT_OneInstr, OpCodes(Mips::LHu_MM, Mips::LHU16_MM), ReduceLXUtoLXU16,238OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},239{RT_TwoInstr, OpCodes(Mips::LW, Mips::LWP_MM), ReduceXWtoXWP,240OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},241{RT_OneInstr, OpCodes(Mips::LW, Mips::LWSP_MM), ReduceXWtoXWSP,242OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},243{RT_TwoInstr, OpCodes(Mips::LW16_MM, Mips::LWP_MM), ReduceXWtoXWP,244OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},245{RT_TwoInstr, OpCodes(Mips::LW_MM, Mips::LWP_MM), ReduceXWtoXWP,246OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},247{RT_OneInstr, OpCodes(Mips::LW_MM, Mips::LWSP_MM), ReduceXWtoXWSP,248OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},249{RT_TwoInstr, OpCodes(Mips::MOVE16_MM, Mips::MOVEP_MM), ReduceMoveToMovep,250OpInfo(OT_OperandsMovep), ImmField(0, 0, 0, -1)},251{RT_OneInstr, OpCodes(Mips::SB, Mips::SB16_MM), ReduceSXtoSX16,252OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},253{RT_OneInstr, OpCodes(Mips::SB_MM, Mips::SB16_MM), ReduceSXtoSX16,254OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},255{RT_OneInstr, OpCodes(Mips::SH, Mips::SH16_MM), ReduceSXtoSX16,256OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},257{RT_OneInstr, OpCodes(Mips::SH_MM, Mips::SH16_MM), ReduceSXtoSX16,258OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},259{RT_OneInstr, OpCodes(Mips::SUBu, Mips::SUBU16_MM),260ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),261ImmField(0, 0, 0, -1)},262{RT_OneInstr, OpCodes(Mips::SUBu_MM, Mips::SUBU16_MM),263ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),264ImmField(0, 0, 0, -1)},265{RT_TwoInstr, OpCodes(Mips::SW, Mips::SWP_MM), ReduceXWtoXWP,266OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},267{RT_OneInstr, OpCodes(Mips::SW, Mips::SWSP_MM), ReduceXWtoXWSP,268OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},269{RT_TwoInstr, OpCodes(Mips::SW16_MM, Mips::SWP_MM), ReduceXWtoXWP,270OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},271{RT_TwoInstr, OpCodes(Mips::SW_MM, Mips::SWP_MM), ReduceXWtoXWP,272OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},273{RT_OneInstr, OpCodes(Mips::SW_MM, Mips::SWSP_MM), ReduceXWtoXWSP,274OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},275{RT_OneInstr, OpCodes(Mips::XOR, Mips::XOR16_MM), ReduceXORtoXOR16,276OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)},277{RT_OneInstr, OpCodes(Mips::XOR_MM, Mips::XOR16_MM), ReduceXORtoXOR16,278OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)}};279} // end anonymous namespace280281INITIALIZE_PASS(MicroMipsSizeReduce, DEBUG_TYPE, MICROMIPS_SIZE_REDUCE_NAME,282false, false)283284// Returns true if the machine operand MO is register SP.285static bool IsSP(const MachineOperand &MO) {286if (MO.isReg() && ((MO.getReg() == Mips::SP)))287return true;288return false;289}290291// Returns true if the machine operand MO is register $16, $17, or $2-$7.292static bool isMMThreeBitGPRegister(const MachineOperand &MO) {293if (MO.isReg() && Mips::GPRMM16RegClass.contains(MO.getReg()))294return true;295return false;296}297298// Returns true if the machine operand MO is register $0, $17, or $2-$7.299static bool isMMSourceRegister(const MachineOperand &MO) {300if (MO.isReg() && Mips::GPRMM16ZeroRegClass.contains(MO.getReg()))301return true;302return false;303}304305// Returns true if the operand Op is an immediate value306// and writes the immediate value into variable Imm.307static bool GetImm(MachineInstr *MI, unsigned Op, int64_t &Imm) {308309if (!MI->getOperand(Op).isImm())310return false;311Imm = MI->getOperand(Op).getImm();312return true;313}314315// Returns true if the value is a valid immediate for ADDIUSP.316static bool AddiuspImmValue(int64_t Value) {317int64_t Value2 = Value >> 2;318if (((Value & (int64_t)maskTrailingZeros<uint64_t>(2)) == Value) &&319((Value2 >= 2 && Value2 <= 257) || (Value2 >= -258 && Value2 <= -3)))320return true;321return false;322}323324// Returns true if the variable Value has the number of least-significant zero325// bits equal to Shift and if the shifted value is between the bounds.326static bool InRange(int64_t Value, unsigned short Shift, int LBound,327int HBound) {328int64_t Value2 = Value >> Shift;329if (((Value & (int64_t)maskTrailingZeros<uint64_t>(Shift)) == Value) &&330(Value2 >= LBound) && (Value2 < HBound))331return true;332return false;333}334335// Returns true if immediate operand is in range.336static bool ImmInRange(MachineInstr *MI, const ReduceEntry &Entry) {337338int64_t offset;339340if (!GetImm(MI, Entry.ImmField(), offset))341return false;342343if (!InRange(offset, Entry.Shift(), Entry.LBound(), Entry.HBound()))344return false;345346return true;347}348349// Returns true if MI can be reduced to lwp/swp instruction350static bool CheckXWPInstr(MachineInstr *MI, bool ReduceToLwp,351const ReduceEntry &Entry) {352353if (ReduceToLwp &&354!(MI->getOpcode() == Mips::LW || MI->getOpcode() == Mips::LW_MM ||355MI->getOpcode() == Mips::LW16_MM))356return false;357358if (!ReduceToLwp &&359!(MI->getOpcode() == Mips::SW || MI->getOpcode() == Mips::SW_MM ||360MI->getOpcode() == Mips::SW16_MM))361return false;362363Register reg = MI->getOperand(0).getReg();364if (reg == Mips::RA)365return false;366367if (!ImmInRange(MI, Entry))368return false;369370if (ReduceToLwp && (MI->getOperand(0).getReg() == MI->getOperand(1).getReg()))371return false;372373return true;374}375376// Returns true if the registers Reg1 and Reg2 are consecutive377static bool ConsecutiveRegisters(unsigned Reg1, unsigned Reg2) {378constexpr std::array<unsigned, 31> Registers = {379{Mips::AT, Mips::V0, Mips::V1, Mips::A0, Mips::A1, Mips::A2, Mips::A3,380Mips::T0, Mips::T1, Mips::T2, Mips::T3, Mips::T4, Mips::T5, Mips::T6,381Mips::T7, Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,382Mips::S6, Mips::S7, Mips::T8, Mips::T9, Mips::K0, Mips::K1, Mips::GP,383Mips::SP, Mips::FP, Mips::RA}};384385for (uint8_t i = 0; i < Registers.size() - 1; i++) {386if (Registers[i] == Reg1) {387if (Registers[i + 1] == Reg2)388return true;389else390return false;391}392}393return false;394}395396// Returns true if registers and offsets are consecutive397static bool ConsecutiveInstr(MachineInstr *MI1, MachineInstr *MI2) {398399int64_t Offset1, Offset2;400if (!GetImm(MI1, 2, Offset1))401return false;402if (!GetImm(MI2, 2, Offset2))403return false;404405Register Reg1 = MI1->getOperand(0).getReg();406Register Reg2 = MI2->getOperand(0).getReg();407408return ((Offset1 == (Offset2 - 4)) && (ConsecutiveRegisters(Reg1, Reg2)));409}410411MicroMipsSizeReduce::MicroMipsSizeReduce() : MachineFunctionPass(ID) {}412413bool MicroMipsSizeReduce::ReduceMI(const MachineBasicBlock::instr_iterator &MII,414MachineBasicBlock::instr_iterator &NextMII) {415416MachineInstr *MI = &*MII;417unsigned Opcode = MI->getOpcode();418419// Search the table.420ReduceEntryVector::const_iterator Start = std::begin(ReduceTable);421ReduceEntryVector::const_iterator End = std::end(ReduceTable);422423std::pair<ReduceEntryVector::const_iterator,424ReduceEntryVector::const_iterator>425Range = std::equal_range(Start, End, Opcode);426427if (Range.first == Range.second)428return false;429430for (ReduceEntryVector::const_iterator Entry = Range.first;431Entry != Range.second; ++Entry) {432ReduceEntryFunArgs Arguments(&(*MII), *Entry, NextMII);433if (((*Entry).ReduceFunction)(&Arguments))434return true;435}436return false;437}438439bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments) {440441MachineInstr *MI = Arguments->MI;442const ReduceEntry &Entry = Arguments->Entry;443444if (!ImmInRange(MI, Entry))445return false;446447if (!IsSP(MI->getOperand(1)))448return false;449450return ReplaceInstruction(MI, Entry);451}452453bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs *Arguments) {454455const ReduceEntry &Entry = Arguments->Entry;456MachineBasicBlock::instr_iterator &NextMII = Arguments->NextMII;457const MachineBasicBlock::instr_iterator &E =458Arguments->MI->getParent()->instr_end();459460if (NextMII == E)461return false;462463MachineInstr *MI1 = Arguments->MI;464MachineInstr *MI2 = &*NextMII;465466// ReduceToLwp = true/false - reduce to LWP/SWP instruction467bool ReduceToLwp = (MI1->getOpcode() == Mips::LW) ||468(MI1->getOpcode() == Mips::LW_MM) ||469(MI1->getOpcode() == Mips::LW16_MM);470471if (!CheckXWPInstr(MI1, ReduceToLwp, Entry))472return false;473474if (!CheckXWPInstr(MI2, ReduceToLwp, Entry))475return false;476477Register Reg1 = MI1->getOperand(1).getReg();478Register Reg2 = MI2->getOperand(1).getReg();479480if (Reg1 != Reg2)481return false;482483bool ConsecutiveForward = ConsecutiveInstr(MI1, MI2);484bool ConsecutiveBackward = ConsecutiveInstr(MI2, MI1);485486if (!(ConsecutiveForward || ConsecutiveBackward))487return false;488489NextMII = std::next(NextMII);490return ReplaceInstruction(MI1, Entry, MI2, ConsecutiveForward);491}492493bool MicroMipsSizeReduce::ReduceArithmeticInstructions(494ReduceEntryFunArgs *Arguments) {495496MachineInstr *MI = Arguments->MI;497const ReduceEntry &Entry = Arguments->Entry;498499if (!isMMThreeBitGPRegister(MI->getOperand(0)) ||500!isMMThreeBitGPRegister(MI->getOperand(1)) ||501!isMMThreeBitGPRegister(MI->getOperand(2)))502return false;503504return ReplaceInstruction(MI, Entry);505}506507bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(508ReduceEntryFunArgs *Arguments) {509510MachineInstr *MI = Arguments->MI;511const ReduceEntry &Entry = Arguments->Entry;512513if (!ImmInRange(MI, Entry))514return false;515516if (!isMMThreeBitGPRegister(MI->getOperand(0)) || !IsSP(MI->getOperand(1)))517return false;518519return ReplaceInstruction(MI, Entry);520}521522bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments) {523524MachineInstr *MI = Arguments->MI;525const ReduceEntry &Entry = Arguments->Entry;526527int64_t ImmValue;528if (!GetImm(MI, Entry.ImmField(), ImmValue))529return false;530531if (!AddiuspImmValue(ImmValue))532return false;533534if (!IsSP(MI->getOperand(0)) || !IsSP(MI->getOperand(1)))535return false;536537return ReplaceInstruction(MI, Entry);538}539540bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments) {541542MachineInstr *MI = Arguments->MI;543const ReduceEntry &Entry = Arguments->Entry;544545if (!ImmInRange(MI, Entry))546return false;547548if (!isMMThreeBitGPRegister(MI->getOperand(0)) ||549!isMMThreeBitGPRegister(MI->getOperand(1)))550return false;551552return ReplaceInstruction(MI, Entry);553}554555bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs *Arguments) {556557MachineInstr *MI = Arguments->MI;558const ReduceEntry &Entry = Arguments->Entry;559560if (!ImmInRange(MI, Entry))561return false;562563if (!isMMSourceRegister(MI->getOperand(0)) ||564!isMMThreeBitGPRegister(MI->getOperand(1)))565return false;566567return ReplaceInstruction(MI, Entry);568}569570// Returns true if Reg can be a source register571// of MOVEP instruction572static bool IsMovepSrcRegister(unsigned Reg) {573574if (Reg == Mips::ZERO || Reg == Mips::V0 || Reg == Mips::V1 ||575Reg == Mips::S0 || Reg == Mips::S1 || Reg == Mips::S2 ||576Reg == Mips::S3 || Reg == Mips::S4)577return true;578579return false;580}581582// Returns true if Reg can be a destination register583// of MOVEP instruction584static bool IsMovepDestinationReg(unsigned Reg) {585586if (Reg == Mips::A0 || Reg == Mips::A1 || Reg == Mips::A2 ||587Reg == Mips::A3 || Reg == Mips::S5 || Reg == Mips::S6)588return true;589590return false;591}592593// Returns true if the registers can be a pair of destination594// registers in MOVEP instruction595static bool IsMovepDestinationRegPair(unsigned R0, unsigned R1) {596597if ((R0 == Mips::A0 && R1 == Mips::S5) ||598(R0 == Mips::A0 && R1 == Mips::S6) ||599(R0 == Mips::A0 && R1 == Mips::A1) ||600(R0 == Mips::A0 && R1 == Mips::A2) ||601(R0 == Mips::A0 && R1 == Mips::A3) ||602(R0 == Mips::A1 && R1 == Mips::A2) ||603(R0 == Mips::A1 && R1 == Mips::A3) ||604(R0 == Mips::A2 && R1 == Mips::A3))605return true;606607return false;608}609610bool MicroMipsSizeReduce::ReduceMoveToMovep(ReduceEntryFunArgs *Arguments) {611612const ReduceEntry &Entry = Arguments->Entry;613MachineBasicBlock::instr_iterator &NextMII = Arguments->NextMII;614const MachineBasicBlock::instr_iterator &E =615Arguments->MI->getParent()->instr_end();616617if (NextMII == E)618return false;619620MachineInstr *MI1 = Arguments->MI;621MachineInstr *MI2 = &*NextMII;622623Register RegDstMI1 = MI1->getOperand(0).getReg();624Register RegSrcMI1 = MI1->getOperand(1).getReg();625626if (!IsMovepSrcRegister(RegSrcMI1))627return false;628629if (!IsMovepDestinationReg(RegDstMI1))630return false;631632if (MI2->getOpcode() != Entry.WideOpc())633return false;634635Register RegDstMI2 = MI2->getOperand(0).getReg();636Register RegSrcMI2 = MI2->getOperand(1).getReg();637638if (!IsMovepSrcRegister(RegSrcMI2))639return false;640641bool ConsecutiveForward;642if (IsMovepDestinationRegPair(RegDstMI1, RegDstMI2)) {643ConsecutiveForward = true;644} else if (IsMovepDestinationRegPair(RegDstMI2, RegDstMI1)) {645ConsecutiveForward = false;646} else647return false;648649NextMII = std::next(NextMII);650return ReplaceInstruction(MI1, Entry, MI2, ConsecutiveForward);651}652653bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments) {654655MachineInstr *MI = Arguments->MI;656const ReduceEntry &Entry = Arguments->Entry;657658if (!isMMThreeBitGPRegister(MI->getOperand(0)) ||659!isMMThreeBitGPRegister(MI->getOperand(1)) ||660!isMMThreeBitGPRegister(MI->getOperand(2)))661return false;662663if (!(MI->getOperand(0).getReg() == MI->getOperand(2).getReg()) &&664!(MI->getOperand(0).getReg() == MI->getOperand(1).getReg()))665return false;666667return ReplaceInstruction(MI, Entry);668}669670bool MicroMipsSizeReduce::ReduceMBB(MachineBasicBlock &MBB) {671bool Modified = false;672MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),673E = MBB.instr_end();674MachineBasicBlock::instr_iterator NextMII;675676// Iterate through the instructions in the basic block677for (; MII != E; MII = NextMII) {678NextMII = std::next(MII);679MachineInstr *MI = &*MII;680681// Don't reduce bundled instructions or pseudo operations682if (MI->isBundle() || MI->isTransient())683continue;684685// Try to reduce 32-bit instruction into 16-bit instruction686Modified |= ReduceMI(MII, NextMII);687}688689return Modified;690}691692bool MicroMipsSizeReduce::ReplaceInstruction(MachineInstr *MI,693const ReduceEntry &Entry,694MachineInstr *MI2,695bool ConsecutiveForward) {696697enum OperandTransfer OpTransfer = Entry.TransferOperands();698699LLVM_DEBUG(dbgs() << "Converting 32-bit: " << *MI);700++NumReduced;701702if (OpTransfer == OT_OperandsAll) {703MI->setDesc(MipsII->get(Entry.NarrowOpc()));704LLVM_DEBUG(dbgs() << " to 16-bit: " << *MI);705return true;706} else {707MachineBasicBlock &MBB = *MI->getParent();708const MCInstrDesc &NewMCID = MipsII->get(Entry.NarrowOpc());709DebugLoc dl = MI->getDebugLoc();710MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);711switch (OpTransfer) {712case OT_Operand2:713MIB.add(MI->getOperand(2));714break;715case OT_Operands02: {716MIB.add(MI->getOperand(0));717MIB.add(MI->getOperand(2));718break;719}720case OT_OperandsXOR: {721if (MI->getOperand(0).getReg() == MI->getOperand(2).getReg()) {722MIB.add(MI->getOperand(0));723MIB.add(MI->getOperand(1));724MIB.add(MI->getOperand(2));725} else {726MIB.add(MI->getOperand(0));727MIB.add(MI->getOperand(2));728MIB.add(MI->getOperand(1));729}730break;731}732case OT_OperandsMovep:733case OT_OperandsLwp:734case OT_OperandsSwp: {735if (ConsecutiveForward) {736MIB.add(MI->getOperand(0));737MIB.add(MI2->getOperand(0));738MIB.add(MI->getOperand(1));739if (OpTransfer == OT_OperandsMovep)740MIB.add(MI2->getOperand(1));741else742MIB.add(MI->getOperand(2));743} else { // consecutive backward744MIB.add(MI2->getOperand(0));745MIB.add(MI->getOperand(0));746MIB.add(MI2->getOperand(1));747if (OpTransfer == OT_OperandsMovep)748MIB.add(MI->getOperand(1));749else750MIB.add(MI2->getOperand(2));751}752753LLVM_DEBUG(dbgs() << "and converting 32-bit: " << *MI2754<< " to: " << *MIB);755756MBB.erase_instr(MI);757MBB.erase_instr(MI2);758return true;759}760default:761llvm_unreachable("Unknown operand transfer!");762}763764// Transfer MI flags.765MIB.setMIFlags(MI->getFlags());766767LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);768MBB.erase_instr(MI);769return true;770}771return false;772}773774bool MicroMipsSizeReduce::runOnMachineFunction(MachineFunction &MF) {775776Subtarget = &MF.getSubtarget<MipsSubtarget>();777778// TODO: Add support for the subtarget microMIPS32R6.779if (!Subtarget->inMicroMipsMode() || !Subtarget->hasMips32r2() ||780Subtarget->hasMips32r6())781return false;782783MipsII = static_cast<const MipsInstrInfo *>(Subtarget->getInstrInfo());784785bool Modified = false;786MachineFunction::iterator I = MF.begin(), E = MF.end();787788for (; I != E; ++I)789Modified |= ReduceMBB(*I);790return Modified;791}792793/// Returns an instance of the MicroMips size reduction pass.794FunctionPass *llvm::createMicroMipsSizeReducePass() {795return new MicroMipsSizeReduce();796}797798799