[llvm-branch-commits] [Mips] Port MicroSizeReduc to NewPM (PR #237139)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Oct 30 01:12:04 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-mips

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

Standard NewPM pass porting.


---

Patch is 35.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/237139.diff


9 Files Affected:

- (modified) llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp (+256-266) 
- (modified) llvm/lib/Target/Mips/Mips.h (+10-2) 
- (modified) llvm/lib/Target/Mips/MipsCodeGenPassBuilder.cpp (+1-1) 
- (modified) llvm/lib/Target/Mips/MipsPassRegistry.def (+1) 
- (modified) llvm/lib/Target/Mips/MipsTargetMachine.cpp (+2-2) 
- (modified) llvm/test/CodeGen/Mips/micromips-sizereduction/micromips-lwp-swp.mir (+1-1) 
- (modified) llvm/test/CodeGen/Mips/micromips-sizereduction/micromips-movep.mir (+1-1) 
- (modified) llvm/test/CodeGen/Mips/micromips-sizereduction/micromips-no-lwp-swp.mir (+1-1) 
- (modified) llvm/test/CodeGen/Mips/newpm-pipeline.ll (+3) 


``````````diff
diff --git a/llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp b/llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp
index 4b82c5b2a6593f..4da19cd9022128 100644
--- a/llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp
+++ b/llvm/lib/Target/Mips/MicroMipsSizeReduction.cpp
@@ -14,12 +14,16 @@
 #include "MipsInstrInfo.h"
 #include "MipsSubtarget.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachinePassManager.h"
+#include "llvm/IR/Analysis.h"
+#include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
 
 using namespace llvm;
 
-#define DEBUG_TYPE "micromips-reduce-size"
+#define DEBUG_TYPE "mips-micro-reduce-size"
 #define MICROMIPS_SIZE_REDUCE_NAME "MicroMips instruction size reduce pass"
 
 STATISTIC(NumReduced, "Number of instructions reduced (32-bit to 16-bit ones, "
@@ -122,23 +126,22 @@ struct ReduceEntry {
 struct ReduceEntryFunArgs {
   MachineInstr *MI;         // Instruction
   const ReduceEntry &Entry; // Entry field
+  const MipsInstrInfo *MipsII;
   MachineBasicBlock::instr_iterator
       &NextMII; // Iterator to next instruction in block
 
   ReduceEntryFunArgs(MachineInstr *argMI, const ReduceEntry &argEntry,
+                     const MipsInstrInfo *argMipsII,
                      MachineBasicBlock::instr_iterator &argNextMII)
-      : MI(argMI), Entry(argEntry), NextMII(argNextMII) {}
+      : MI(argMI), Entry(argEntry), MipsII(argMipsII), NextMII(argNextMII) {}
 };
 
 typedef llvm::SmallVector<ReduceEntry, 32> ReduceEntryVector;
 
-class MicroMipsSizeReduce : public MachineFunctionPass {
+class MipsMicroSizeReduceLegacy : public MachineFunctionPass {
 public:
   static char ID;
-  MicroMipsSizeReduce();
-
-  static const MipsInstrInfo *MipsII;
-  const MipsSubtarget *Subtarget;
+  MipsMicroSizeReduceLegacy();
 
   bool runOnMachineFunction(MachineFunction &MF) override;
 
@@ -146,141 +149,18 @@ class MicroMipsSizeReduce : public MachineFunctionPass {
     return "microMIPS instruction size reduction pass";
   }
 
-private:
-  /// Reduces width of instructions in the specified basic block.
-  bool ReduceMBB(MachineBasicBlock &MBB);
-
-  /// Attempts to reduce MI, returns true on success.
-  bool ReduceMI(const MachineBasicBlock::instr_iterator &MII,
-                MachineBasicBlock::instr_iterator &NextMII);
-
-  // Attempts to reduce LW/SW instruction into LWSP/SWSP,
-  // returns true on success.
-  static bool ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce two LW/SW instructions into LWP/SWP instruction,
-  // returns true on success.
-  static bool ReduceXWtoXWP(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce LBU/LHU instruction into LBU16/LHU16,
-  // returns true on success.
-  static bool ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce SB/SH instruction into SB16/SH16,
-  // returns true on success.
-  static bool ReduceSXtoSX16(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce two MOVE instructions into MOVEP instruction,
-  // returns true on success.
-  static bool ReduceMoveToMovep(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce arithmetic instructions, returns true on success.
-  static bool ReduceArithmeticInstructions(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce ADDIU into ADDIUSP instruction,
-  // returns true on success.
-  static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce ADDIU into ADDIUR1SP instruction,
-  // returns true on success.
-  static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs *Arguments);
-
-  // Attempts to reduce XOR into XOR16 instruction,
-  // returns true on success.
-  static bool ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments);
-
-  // Changes opcode of an instruction, replaces an instruction with a
-  // new one, or replaces two instructions with a new instruction
-  // depending on their order i.e. if these are consecutive forward
-  // or consecutive backward
-  static bool ReplaceInstruction(MachineInstr *MI, const ReduceEntry &Entry,
-                                 MachineInstr *MI2 = nullptr,
-                                 bool ConsecutiveForward = true);
-
-  // Table with transformation rules for each instruction.
-  static ReduceEntryVector ReduceTable;
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesCFG();
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
 };
 
-char MicroMipsSizeReduce::ID = 0;
-const MipsInstrInfo *MicroMipsSizeReduce::MipsII;
+char MipsMicroSizeReduceLegacy::ID = 0;
 
-// This table must be sorted by WideOpc as a main criterion and
-// ReduceType as a sub-criterion (when wide opcodes are the same).
-ReduceEntryVector MicroMipsSizeReduce::ReduceTable = {
+} // namespace
 
-    // ReduceType, OpCodes, ReduceFunction,
-    // OpInfo(TransferOperands),
-    // ImmField(Shift, LBound, HBound, ImmFieldPosition)
-    {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUR1SP_MM),
-     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
-    {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUSP_MM), ReduceADDIUToADDIUSP,
-     OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
-    {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUR1SP_MM),
-     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
-    {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUSP_MM),
-     ReduceADDIUToADDIUSP, OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
-    {RT_OneInstr, OpCodes(Mips::ADDu, Mips::ADDU16_MM),
-     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
-     ImmField(0, 0, 0, -1)},
-    {RT_OneInstr, OpCodes(Mips::ADDu_MM, Mips::ADDU16_MM),
-     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
-     ImmField(0, 0, 0, -1)},
-    {RT_OneInstr, OpCodes(Mips::LBu, Mips::LBU16_MM), ReduceLXUtoLXU16,
-     OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
-    {RT_OneInstr, OpCodes(Mips::LBu_MM, Mips::LBU16_MM), ReduceLXUtoLXU16,
-     OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
-    {RT_OneInstr, OpCodes(Mips::LEA_ADDiu, Mips::ADDIUR1SP_MM),
-     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
-    {RT_OneInstr, OpCodes(Mips::LEA_ADDiu_MM, Mips::ADDIUR1SP_MM),
-     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
-    {RT_OneInstr, OpCodes(Mips::LHu, Mips::LHU16_MM), ReduceLXUtoLXU16,
-     OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
-    {RT_OneInstr, OpCodes(Mips::LHu_MM, Mips::LHU16_MM), ReduceLXUtoLXU16,
-     OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
-    {RT_TwoInstr, OpCodes(Mips::LW, Mips::LWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
-    {RT_OneInstr, OpCodes(Mips::LW, Mips::LWSP_MM), ReduceXWtoXWSP,
-     OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
-    {RT_TwoInstr, OpCodes(Mips::LW16_MM, Mips::LWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
-    {RT_TwoInstr, OpCodes(Mips::LW_MM, Mips::LWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
-    {RT_OneInstr, OpCodes(Mips::LW_MM, Mips::LWSP_MM), ReduceXWtoXWSP,
-     OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
-    {RT_TwoInstr, OpCodes(Mips::MOVE16_MM, Mips::MOVEP_MM), ReduceMoveToMovep,
-     OpInfo(OT_OperandsMovep), ImmField(0, 0, 0, -1)},
-    {RT_OneInstr, OpCodes(Mips::SB, Mips::SB16_MM), ReduceSXtoSX16,
-     OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
-    {RT_OneInstr, OpCodes(Mips::SB_MM, Mips::SB16_MM), ReduceSXtoSX16,
-     OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
-    {RT_OneInstr, OpCodes(Mips::SH, Mips::SH16_MM), ReduceSXtoSX16,
-     OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
-    {RT_OneInstr, OpCodes(Mips::SH_MM, Mips::SH16_MM), ReduceSXtoSX16,
-     OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
-    {RT_OneInstr, OpCodes(Mips::SUBu, Mips::SUBU16_MM),
-     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
-     ImmField(0, 0, 0, -1)},
-    {RT_OneInstr, OpCodes(Mips::SUBu_MM, Mips::SUBU16_MM),
-     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
-     ImmField(0, 0, 0, -1)},
-    {RT_TwoInstr, OpCodes(Mips::SW, Mips::SWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
-    {RT_OneInstr, OpCodes(Mips::SW, Mips::SWSP_MM), ReduceXWtoXWSP,
-     OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
-    {RT_TwoInstr, OpCodes(Mips::SW16_MM, Mips::SWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
-    {RT_TwoInstr, OpCodes(Mips::SW_MM, Mips::SWP_MM), ReduceXWtoXWP,
-     OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
-    {RT_OneInstr, OpCodes(Mips::SW_MM, Mips::SWSP_MM), ReduceXWtoXWSP,
-     OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
-    {RT_OneInstr, OpCodes(Mips::XOR, Mips::XOR16_MM), ReduceXORtoXOR16,
-     OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)},
-    {RT_OneInstr, OpCodes(Mips::XOR_MM, Mips::XOR16_MM), ReduceXORtoXOR16,
-     OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)}};
-} // end anonymous namespace
-
-INITIALIZE_PASS(MicroMipsSizeReduce, DEBUG_TYPE, MICROMIPS_SIZE_REDUCE_NAME,
-                false, false)
+INITIALIZE_PASS(MipsMicroSizeReduceLegacy, DEBUG_TYPE,
+                MICROMIPS_SIZE_REDUCE_NAME, false, false)
 
 // Returns true if the machine operand MO is register SP.
 static bool IsSP(const MachineOperand &MO) {
@@ -410,35 +290,98 @@ static bool ConsecutiveInstr(MachineInstr *MI1, MachineInstr *MI2) {
   return ((Offset1 == (Offset2 - 4)) && (ConsecutiveRegisters(Reg1, Reg2)));
 }
 
-MicroMipsSizeReduce::MicroMipsSizeReduce() : MachineFunctionPass(ID) {}
+MipsMicroSizeReduceLegacy::MipsMicroSizeReduceLegacy()
+    : MachineFunctionPass(ID) {}
 
-bool MicroMipsSizeReduce::ReduceMI(const MachineBasicBlock::instr_iterator &MII,
-                                   MachineBasicBlock::instr_iterator &NextMII) {
+// Changes opcode of an instruction, replaces an instruction with a
+// new one, or replaces two instructions with a new instruction
+// depending on their order i.e. if these are consecutive forward
+// or consecutive backward
+static bool ReplaceInstruction(MachineInstr *MI, const ReduceEntry &Entry,
+                               const MipsInstrInfo *MipsII,
+                               MachineInstr *MI2 = nullptr,
+                               bool ConsecutiveForward = true) {
 
-  MachineInstr *MI = &*MII;
-  unsigned Opcode = MI->getOpcode();
+  enum OperandTransfer OpTransfer = Entry.TransferOperands();
 
-  // Search the table.
-  ReduceEntryVector::const_iterator Start = std::begin(ReduceTable);
-  ReduceEntryVector::const_iterator End = std::end(ReduceTable);
+  LLVM_DEBUG(dbgs() << "Converting 32-bit: " << *MI);
+  ++NumReduced;
 
-  std::pair<ReduceEntryVector::const_iterator,
-            ReduceEntryVector::const_iterator>
-      Range = std::equal_range(Start, End, Opcode);
+  if (OpTransfer == OT_OperandsAll) {
+    MI->setDesc(MipsII->get(Entry.NarrowOpc()));
+    LLVM_DEBUG(dbgs() << "       to 16-bit: " << *MI);
+    return true;
+  } else {
+    MachineBasicBlock &MBB = *MI->getParent();
+    const MCInstrDesc &NewMCID = MipsII->get(Entry.NarrowOpc());
+    DebugLoc dl = MI->getDebugLoc();
+    MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+    switch (OpTransfer) {
+    case OT_Operand2:
+      MIB.add(MI->getOperand(2));
+      break;
+    case OT_Operands02: {
+      MIB.add(MI->getOperand(0));
+      MIB.add(MI->getOperand(2));
+      break;
+    }
+    case OT_OperandsXOR: {
+      if (MI->getOperand(0).getReg() == MI->getOperand(2).getReg()) {
+        MIB.add(MI->getOperand(0));
+        MIB.add(MI->getOperand(1));
+        MIB.add(MI->getOperand(2));
+      } else {
+        MIB.add(MI->getOperand(0));
+        MIB.add(MI->getOperand(2));
+        MIB.add(MI->getOperand(1));
+      }
+      break;
+    }
+    case OT_OperandsMovep:
+    case OT_OperandsLwp:
+    case OT_OperandsSwp: {
+      if (ConsecutiveForward) {
+        MIB.add(MI->getOperand(0));
+        MIB.add(MI2->getOperand(0));
+        MIB.add(MI->getOperand(1));
+        if (OpTransfer == OT_OperandsMovep)
+          MIB.add(MI2->getOperand(1));
+        else
+          MIB.add(MI->getOperand(2));
+      } else { // consecutive backward
+        MIB.add(MI2->getOperand(0));
+        MIB.add(MI->getOperand(0));
+        MIB.add(MI2->getOperand(1));
+        if (OpTransfer == OT_OperandsMovep)
+          MIB.add(MI->getOperand(1));
+        else
+          MIB.add(MI2->getOperand(2));
+      }
 
-  if (Range.first == Range.second)
-    return false;
+      LLVM_DEBUG(dbgs() << "and converting 32-bit: " << *MI2
+                        << "       to: " << *MIB);
 
-  for (ReduceEntryVector::const_iterator Entry = Range.first;
-       Entry != Range.second; ++Entry) {
-    ReduceEntryFunArgs Arguments(&(*MII), *Entry, NextMII);
-    if (((*Entry).ReduceFunction)(&Arguments))
+      MBB.erase_instr(MI);
+      MBB.erase_instr(MI2);
       return true;
+    }
+    default:
+      llvm_unreachable("Unknown operand transfer!");
+    }
+
+    // Transfer MI flags.
+    MIB.setMIFlags(MI->getFlags());
+
+    LLVM_DEBUG(dbgs() << "       to 16-bit: " << *MIB);
+    MBB.erase_instr(MI);
+    return true;
   }
   return false;
 }
 
-bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce LW/SW instruction into LWSP/SWSP,
+// returns true on success.
+static bool ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -449,10 +392,12 @@ bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs *Arguments) {
   if (!IsSP(MI->getOperand(1)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce two LW/SW instructions into LWP/SWP instruction,
+// returns true on success.
+static bool ReduceXWtoXWP(ReduceEntryFunArgs *Arguments) {
 
   const ReduceEntry &Entry = Arguments->Entry;
   MachineBasicBlock::instr_iterator &NextMII = Arguments->NextMII;
@@ -489,11 +434,12 @@ bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs *Arguments) {
     return false;
 
   NextMII = std::next(NextMII);
-  return ReplaceInstruction(MI1, Entry, MI2, ConsecutiveForward);
+  return ReplaceInstruction(MI1, Entry, Arguments->MipsII, MI2,
+                            ConsecutiveForward);
 }
 
-bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
-    ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce arithmetic instructions, returns true on success.
+static bool ReduceArithmeticInstructions(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -503,11 +449,12 @@ bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
       !isMMThreeBitGPRegister(MI->getOperand(2)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(
-    ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce ADDIU into ADDIUR1SP instruction,
+// returns true on success.
+static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -518,10 +465,12 @@ bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(
   if (!isMMThreeBitGPRegister(MI->getOperand(0)) || !IsSP(MI->getOperand(1)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce ADDIU into ADDIUSP instruction,
+// returns true on success.
+static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -536,10 +485,12 @@ bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments) {
   if (!IsSP(MI->getOperand(0)) || !IsSP(MI->getOperand(1)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce LBU/LHU instruction into LBU16/LHU16,
+// returns true on success.
+static bool ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -551,10 +502,12 @@ bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments) {
       !isMMThreeBitGPRegister(MI->getOperand(1)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce SB/SH instruction into SB16/SH16,
+// returns true on success.
+static bool ReduceSXtoSX16(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -566,7 +519,7 @@ bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs *Arguments) {
       !isMMThreeBitGPRegister(MI->getOperand(1)))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
 // Returns true if Reg can be a source register
@@ -608,7 +561,9 @@ static bool IsMovepDestinationRegPair(unsigned R0, unsigned R1) {
   return false;
 }
 
-bool MicroMipsSizeReduce::ReduceMoveToMovep(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce two MOVE instructions into MOVEP instruction,
+// returns true on success.
+static bool ReduceMoveToMovep(ReduceEntryFunArgs *Arguments) {
 
   const ReduceEntry &Entry = Arguments->Entry;
   MachineBasicBlock::instr_iterator &NextMII = Arguments->NextMII;
@@ -648,10 +603,13 @@ bool MicroMipsSizeReduce::ReduceMoveToMovep(ReduceEntryFunArgs *Arguments) {
     return false;
 
   NextMII = std::next(NextMII);
-  return ReplaceInstruction(MI1, Entry, MI2, ConsecutiveForward);
+  return ReplaceInstruction(MI1, Entry, Arguments->MipsII, MI2,
+                            ConsecutiveForward);
 }
 
-bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments) {
+// Attempts to reduce XOR into XOR16 instruction,
+// returns true on success.
+static bool ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments) {
 
   MachineInstr *MI = Arguments->MI;
   const ReduceEntry &Entry = Arguments->Entry;
@@ -665,10 +623,113 @@ bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments) {
       !(MI->getOperand(0).getReg() == MI->getOperand(1).getReg()))
     return false;
 
-  return ReplaceInstruction(MI, Entry);
+  return ReplaceInstruction(MI, Entry, Arguments->MipsII);
 }
 
-bool MicroMipsSizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
+// Table with transformation rules for each instruction.
+// This table must be sorted by WideOpc as a main criterion and
+// ReduceType as a sub-criterion (when wide opcodes are the same).
+static ReduceEntryVector ReduceTable = {
+    // ReduceType, OpCodes, ReduceFunction,
+    // OpInfo(TransferOperands),
+    // ImmField(Shift, LBound, HBound, ImmFieldPosition)
+    {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUR1SP_MM),
+     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
+    {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUSP_MM), ReduceADDIUToADDIUSP,
+     OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
+    {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUR1SP_MM),
+     ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
+    {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUSP_MM),
+     ReduceADDIUToADDIUSP, OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
+    {RT_OneInstr, OpCodes(Mips::ADDu, Mips::ADDU16_MM),
+     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
+     ImmField(0, 0, 0, -1)},
+    {RT_OneInstr, OpCodes(Mips::ADDu_MM, Mips::ADDU16_MM),
+     ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
+     ImmField(0, 0, 0, -1)},
+    {RT_OneInstr, OpCodes(Mips::LBu, Mips::LBU16_MM), ReduceLXUtoLXU16,
+     OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
+    {RT_OneInstr, OpCodes(Mips::LBu_MM, Mips::LBU16...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/237139


More information about the llvm-branch-commits mailing list