Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeGen] Add a helper class to reuse expandMBB. NFC. #70325

Closed
wants to merge 1 commit into from

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Oct 26, 2023

This PR adds a helper class ExpandPseudoInstsPass to avoid copying and pasting expandMBB implementations.
IMHO, I am not sure this change is profitable. I just posted this patch for your opinion.

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 26, 2023

@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-backend-loongarch
@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-m68k
@llvm/pr-subscribers-backend-arm

@llvm/pr-subscribers-backend-aarch64

Author: Yingwei Zheng (dtcxzyw)

Changes

This PR adds a helper class ExpandPseudoInstsPass to avoid copying and pasting expandMBB implementations.
IMHO, I am not sure this change is profitable. I just posted this patch for your opinion.


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

14 Files Affected:

  • (added) llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h (+40)
  • (modified) llvm/lib/CodeGen/CMakeLists.txt (+1)
  • (added) llvm/lib/CodeGen/ExpandPseudoInstsPass.cpp (+34)
  • (modified) llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp (+4-20)
  • (modified) llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp (+7-22)
  • (modified) llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp (+5-19)
  • (modified) llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp (+4-18)
  • (modified) llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp (+7-35)
  • (modified) llvm/lib/Target/M68k/M68kExpandPseudo.cpp (+9-24)
  • (modified) llvm/lib/Target/Mips/MipsExpandPseudo.cpp (+4-18)
  • (modified) llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp (+4-18)
  • (modified) llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp (+7-35)
  • (modified) llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp (+4-18)
  • (modified) llvm/lib/Target/X86/X86ExpandPseudo.cpp (+10-25)
diff --git a/llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h b/llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h
new file mode 100644
index 000000000000000..b7c47dc1a410df4
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h
@@ -0,0 +1,40 @@
+//===-- ExpandPseudoInstsPass.h - Pass for expanding pseudo insts-*-C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the ExpandPseudoInstsPass class. It provides a default
+// implementation for expandMBB to simplify target-specific pseudo instruction
+// expansion passes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_EXPANDPSEUDOINSTSPASS_H
+#define LLVM_CODEGEN_EXPANDPSEUDOINSTSPASS_H
+
+#include "llvm/CodeGen/MachineFunctionPass.h"
+
+namespace llvm {
+
+/// ExpandPseudoInstsPass - Helper class for expanding pseudo instructions.
+class ExpandPseudoInstsPass : public MachineFunctionPass {
+protected:
+  explicit ExpandPseudoInstsPass(char &ID) : MachineFunctionPass(ID) {}
+
+  /// If MBBI references a pseudo instruction that should be expanded here,
+  /// do the expansion and return true.  Otherwise return false.
+  virtual bool expandMI(MachineBasicBlock &MBB,
+                        MachineBasicBlock::iterator MBBI,
+                        MachineBasicBlock::iterator &NextMBBI) = 0;
+
+  /// Iterate over the instructions in basic block MBB and expand any
+  /// pseudo instructions.  Return true if anything was modified.
+  bool expandMBB(MachineBasicBlock &MBB);
+};
+
+} // namespace llvm
+
+#endif
diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
index 389c70d04f17ba3..aa8e0c4455d3a40 100644
--- a/llvm/lib/CodeGen/CMakeLists.txt
+++ b/llvm/lib/CodeGen/CMakeLists.txt
@@ -72,6 +72,7 @@ add_llvm_component_library(LLVMCodeGen
   ExpandLargeFpConvert.cpp
   ExpandMemCmp.cpp
   ExpandPostRAPseudos.cpp
+  ExpandPseudoInstsPass.cpp
   ExpandReductions.cpp
   ExpandVectorPredication.cpp
   FaultMaps.cpp
diff --git a/llvm/lib/CodeGen/ExpandPseudoInstsPass.cpp b/llvm/lib/CodeGen/ExpandPseudoInstsPass.cpp
new file mode 100644
index 000000000000000..f64c82bd8bca344
--- /dev/null
+++ b/llvm/lib/CodeGen/ExpandPseudoInstsPass.cpp
@@ -0,0 +1,34 @@
+//===- ExpandPseudoInstsPass.cpp - Pass for expanding pseudo insts*-C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the ExpandPseudoInstsPass class. It provides a default
+// implementation for expandMBB to simplify target-specific pseudo instruction
+// expansion passes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
+
+namespace llvm {
+
+/// Iterate over the instructions in basic block MBB and expand any
+/// pseudo instructions.  Return true if anything was modified.
+bool ExpandPseudoInstsPass::expandMBB(MachineBasicBlock &MBB) {
+  bool Modified = false;
+
+  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
+  while (MBBI != E) {
+    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
+    Modified |= expandMI(MBB, MBBI, NMBBI);
+    MBBI = NMBBI;
+  }
+
+  return Modified;
+}
+
+} // namespace llvm
diff --git a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
index 42bc390a67b2b18..e20a3c9d5c9b9dd 100644
--- a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
@@ -20,11 +20,11 @@
 #include "MCTargetDesc/AArch64AddressingModes.h"
 #include "Utils/AArch64BaseInfo.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/LivePhysRegs.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineFunction.h"
-#include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineOperand.h"
@@ -48,13 +48,13 @@ using namespace llvm;
 
 namespace {
 
-class AArch64ExpandPseudo : public MachineFunctionPass {
+class AArch64ExpandPseudo : public ExpandPseudoInstsPass {
 public:
   const AArch64InstrInfo *TII;
 
   static char ID;
 
-  AArch64ExpandPseudo() : MachineFunctionPass(ID) {
+  AArch64ExpandPseudo() : ExpandPseudoInstsPass(ID) {
     initializeAArch64ExpandPseudoPass(*PassRegistry::getPassRegistry());
   }
 
@@ -63,9 +63,8 @@ class AArch64ExpandPseudo : public MachineFunctionPass {
   StringRef getPassName() const override { return AARCH64_EXPAND_PSEUDO_NAME; }
 
 private:
-  bool expandMBB(MachineBasicBlock &MBB);
   bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                MachineBasicBlock::iterator &NextMBBI);
+                MachineBasicBlock::iterator &NextMBBI) override;
   bool expandMultiVecPseudo(MachineBasicBlock &MBB,
                             MachineBasicBlock::iterator MBBI,
                             TargetRegisterClass ContiguousClass,
@@ -1648,21 +1647,6 @@ bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
   return false;
 }
 
-/// Iterate over the instructions in basic block MBB and expand any
-/// pseudo instructions.  Return true if anything was modified.
-bool AArch64ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= expandMI(MBB, MBBI, NMBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool AArch64ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
   TII = static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
 
diff --git a/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp b/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
index 2f9236bb977fc92..2830ae03cc786dd 100644
--- a/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
@@ -20,9 +20,9 @@
 #include "ARMMachineFunctionInfo.h"
 #include "ARMSubtarget.h"
 #include "MCTargetDesc/ARMAddressingModes.h"
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/LivePhysRegs.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
-#include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/Support/Debug.h"
@@ -38,10 +38,10 @@ VerifyARMPseudo("verify-arm-pseudo-expand", cl::Hidden,
 #define ARM_EXPAND_PSEUDO_NAME "ARM pseudo instruction expansion pass"
 
 namespace {
-  class ARMExpandPseudo : public MachineFunctionPass {
+  class ARMExpandPseudo : public ExpandPseudoInstsPass {
   public:
     static char ID;
-    ARMExpandPseudo() : MachineFunctionPass(ID) {}
+    ARMExpandPseudo() : ExpandPseudoInstsPass(ID) {}
 
     const ARMBaseInstrInfo *TII;
     const TargetRegisterInfo *TRI;
@@ -60,10 +60,8 @@ namespace {
     }
 
   private:
-    bool ExpandMI(MachineBasicBlock &MBB,
-                  MachineBasicBlock::iterator MBBI,
-                  MachineBasicBlock::iterator &NextMBBI);
-    bool ExpandMBB(MachineBasicBlock &MBB);
+    bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                  MachineBasicBlock::iterator &NextMBBI) override;
     void ExpandVLD(MachineBasicBlock::iterator &MBBI);
     void ExpandVST(MachineBasicBlock::iterator &MBBI);
     void ExpandLaneOp(MachineBasicBlock::iterator &MBBI);
@@ -2113,7 +2111,7 @@ static void CMSEPopCalleeSaves(const TargetInstrInfo &TII,
   }
 }
 
-bool ARMExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
+bool ARMExpandPseudo::expandMI(MachineBasicBlock &MBB,
                                MachineBasicBlock::iterator MBBI,
                                MachineBasicBlock::iterator &NextMBBI) {
   MachineInstr &MI = *MBBI;
@@ -3224,19 +3222,6 @@ bool ARMExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
   }
 }
 
-bool ARMExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= ExpandMI(MBB, MBBI, NMBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool ARMExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
   STI = &MF.getSubtarget<ARMSubtarget>();
   TII = STI->getInstrInfo();
@@ -3248,7 +3233,7 @@ bool ARMExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
 
   bool Modified = false;
   for (MachineBasicBlock &MBB : MF)
-    Modified |= ExpandMBB(MBB);
+    Modified |= expandMBB(MBB);
   if (VerifyARMPseudo)
     MF.verify(this, "After expanding ARM pseudo instructions.");
 
diff --git a/llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp b/llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
index f257ccea6c50a91..2e683cb6f1c6e7d 100644
--- a/llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
@@ -17,7 +17,7 @@
 #include "AVRTargetMachine.h"
 #include "MCTargetDesc/AVRMCTargetDesc.h"
 
-#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/CodeGen/TargetRegisterInfo.h"
@@ -30,11 +30,11 @@ namespace {
 
 /// Expands "placeholder" instructions marked as pseudo into
 /// actual AVR instructions.
-class AVRExpandPseudo : public MachineFunctionPass {
+class AVRExpandPseudo : public ExpandPseudoInstsPass {
 public:
   static char ID;
 
-  AVRExpandPseudo() : MachineFunctionPass(ID) {
+  AVRExpandPseudo() : ExpandPseudoInstsPass(ID) {
     initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
   }
 
@@ -49,8 +49,7 @@ class AVRExpandPseudo : public MachineFunctionPass {
   const AVRRegisterInfo *TRI;
   const TargetInstrInfo *TII;
 
-  bool expandMBB(Block &MBB);
-  bool expandMI(Block &MBB, BlockIt MBBI);
+  bool expandMI(Block &MBB, BlockIt MBBI, BlockIt &NextMBBI) override;
   template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI);
 
   MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
@@ -107,19 +106,6 @@ class AVRExpandPseudo : public MachineFunctionPass {
 
 char AVRExpandPseudo::ID = 0;
 
-bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  BlockIt MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    BlockIt NMBBI = std::next(MBBI);
-    Modified |= expandMI(MBB, MBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
   bool Modified = false;
 
@@ -2559,7 +2545,7 @@ bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
   return true;
 }
 
-bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) {
+bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI, BlockIt &NextMBBI) {
   MachineInstr &MI = *MBBI;
   int Opcode = MBBI->getOpcode();
 
diff --git a/llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp b/llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp
index 18a532b55ee5a92..4fc3e8c048f8bce 100644
--- a/llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp
@@ -17,8 +17,8 @@
 #include "LoongArchInstrInfo.h"
 #include "LoongArchTargetMachine.h"
 
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/LivePhysRegs.h"
-#include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 
 using namespace llvm;
@@ -28,12 +28,12 @@ using namespace llvm;
 
 namespace {
 
-class LoongArchExpandAtomicPseudo : public MachineFunctionPass {
+class LoongArchExpandAtomicPseudo : public ExpandPseudoInstsPass {
 public:
   const LoongArchInstrInfo *TII;
   static char ID;
 
-  LoongArchExpandAtomicPseudo() : MachineFunctionPass(ID) {
+  LoongArchExpandAtomicPseudo() : ExpandPseudoInstsPass(ID) {
     initializeLoongArchExpandAtomicPseudoPass(*PassRegistry::getPassRegistry());
   }
 
@@ -44,9 +44,8 @@ class LoongArchExpandAtomicPseudo : public MachineFunctionPass {
   }
 
 private:
-  bool expandMBB(MachineBasicBlock &MBB);
   bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                MachineBasicBlock::iterator &NextMBBI);
+                MachineBasicBlock::iterator &NextMBBI) override;
   bool expandAtomicBinOp(MachineBasicBlock &MBB,
                          MachineBasicBlock::iterator MBBI, AtomicRMWInst::BinOp,
                          bool IsMasked, int Width,
@@ -71,19 +70,6 @@ bool LoongArchExpandAtomicPseudo::runOnMachineFunction(MachineFunction &MF) {
   return Modified;
 }
 
-bool LoongArchExpandAtomicPseudo::expandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= expandMI(MBB, MBBI, NMBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool LoongArchExpandAtomicPseudo::expandMI(
     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
     MachineBasicBlock::iterator &NextMBBI) {
diff --git a/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp b/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
index 72c1f1cec198347..f0a793ea2ebbe31 100644
--- a/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
@@ -16,8 +16,8 @@
 #include "LoongArchTargetMachine.h"
 #include "MCTargetDesc/LoongArchBaseInfo.h"
 #include "MCTargetDesc/LoongArchMCTargetDesc.h"
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/LivePhysRegs.h"
-#include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/CodeGen/Register.h"
@@ -34,12 +34,12 @@ using namespace llvm;
 
 namespace {
 
-class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
+class LoongArchPreRAExpandPseudo : public ExpandPseudoInstsPass {
 public:
   const LoongArchInstrInfo *TII;
   static char ID;
 
-  LoongArchPreRAExpandPseudo() : MachineFunctionPass(ID) {
+  LoongArchPreRAExpandPseudo() : ExpandPseudoInstsPass(ID) {
     initializeLoongArchPreRAExpandPseudoPass(*PassRegistry::getPassRegistry());
   }
 
@@ -54,9 +54,8 @@ class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
   }
 
 private:
-  bool expandMBB(MachineBasicBlock &MBB);
   bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                MachineBasicBlock::iterator &NextMBBI);
+                MachineBasicBlock::iterator &NextMBBI) override;
   bool expandPcalau12iInstPair(MachineBasicBlock &MBB,
                                MachineBasicBlock::iterator MBBI,
                                MachineBasicBlock::iterator &NextMBBI,
@@ -112,19 +111,6 @@ bool LoongArchPreRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
   return Modified;
 }
 
-bool LoongArchPreRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= expandMI(MBB, MBBI, NMBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool LoongArchPreRAExpandPseudo::expandMI(
     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
     MachineBasicBlock::iterator &NextMBBI) {
@@ -515,12 +501,12 @@ bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
   return true;
 }
 
-class LoongArchExpandPseudo : public MachineFunctionPass {
+class LoongArchExpandPseudo : public ExpandPseudoInstsPass {
 public:
   const LoongArchInstrInfo *TII;
   static char ID;
 
-  LoongArchExpandPseudo() : MachineFunctionPass(ID) {
+  LoongArchExpandPseudo() : ExpandPseudoInstsPass(ID) {
     initializeLoongArchExpandPseudoPass(*PassRegistry::getPassRegistry());
   }
 
@@ -531,9 +517,8 @@ class LoongArchExpandPseudo : public MachineFunctionPass {
   }
 
 private:
-  bool expandMBB(MachineBasicBlock &MBB);
   bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                MachineBasicBlock::iterator &NextMBBI);
+                MachineBasicBlock::iterator &NextMBBI) override;
   bool expandCopyCFR(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
                      MachineBasicBlock::iterator &NextMBBI);
 };
@@ -551,19 +536,6 @@ bool LoongArchExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
   return Modified;
 }
 
-bool LoongArchExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= expandMI(MBB, MBBI, NMBBI);
-    MBBI = NMBBI;
-  }
-
-  return Modified;
-}
-
 bool LoongArchExpandPseudo::expandMI(MachineBasicBlock &MBB,
                                      MachineBasicBlock::iterator MBBI,
                                      MachineBasicBlock::iterator &NextMBBI) {
diff --git a/llvm/lib/Target/M68k/M68kExpandPseudo.cpp b/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
index 13268d754a9dde6..7214fd5ef4b0e18 100644
--- a/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
+++ b/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
@@ -19,7 +19,7 @@
 #include "M68kMachineFunction.h"
 #include "M68kSubtarget.h"
 
-#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/ExpandPseudoInstsPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved.
@@ -32,10 +32,10 @@ using namespace llvm;
 #define PASS_NAME "M68k pseudo instruction expansion pass"
 
 namespace {
-class M68kExpandPseudo : public MachineFunctionPass {
+class M68kExpandPseudo : public ExpandPseudoInstsPass {
 public:
   static char ID;
-  M68kExpandPseudo() : MachineFunctionPass(ID) {}
+  M68kExpandPseudo() : ExpandPseudoInstsPass(ID) {}
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
@@ -58,8 +58,8 @@ class M68kExpandPseudo : public MachineFunctionPass {
   }
 
 private:
-  bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
-  bool ExpandMBB(MachineBasicBlock &MBB);
+  bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                MachineBasicBlock::iterator &NextMBBI) override;
 };
 char M68kExpandPseudo::ID = 0;
 } // End anonymous namespace.
@@ -69,8 +69,9 @@ INITIALIZE_PASS(M68kExpandPseudo, DEBUG_TYPE, PASS_NAME, false, false)
 /// If \p MBBI is a pseudo instruction, this method expands
 /// it to the corresponding (sequence of) actual instruction(s).
 /// \returns true if \p MBBI has been expanded.
-bool M68kExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
-                                MachineBasicBlock::iterator MBBI) {
+bool M68kExpandPseudo::expandMI(MachineBasicBlock &MBB,
+                                MachineBasicBlock::iterator MBBI,
+                                MachineBasicBlock::iterator &NextMBBI) {
   MachineInstr &MI = *MBBI;
   MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
   unsigned Opcode = MI.getOpcode();
@@ -284,22 +285,6 @@ bool M68kExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
   llvm_unreachable("Previous switch has a fallthrough?");
 }
 
-/// Expand all pseudo instructions contained in \p MBB.
-/// \returns true if any expansion occurred for \p MBB.
-bool M68kExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
-  bool Modified = false;
-
-  // MBBI may be invalidated by the expansion.
-  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
-  while (MBBI != E) {
-    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
-    Modified |= ExpandMI(MBB, MBBI);
-    MBBI = NMBBI;
-  }...
[truncated]

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 1b6b4d6a08321fb914127dadcd6677dcd9b1b222 129d5f0ce14fb91df3f9302790217a87301fb128 -- llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h llvm/lib/CodeGen/ExpandPseudoInstsPass.cpp llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp llvm/lib/Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp llvm/lib/Target/M68k/M68kExpandPseudo.cpp llvm/lib/Target/Mips/MipsExpandPseudo.cpp llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp llvm/lib/Target/X86/X86ExpandPseudo.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp b/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
index 2830ae03cc78..4c27e5622ba6 100644
--- a/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
@@ -38,81 +38,77 @@ VerifyARMPseudo("verify-arm-pseudo-expand", cl::Hidden,
 #define ARM_EXPAND_PSEUDO_NAME "ARM pseudo instruction expansion pass"
 
 namespace {
-  class ARMExpandPseudo : public ExpandPseudoInstsPass {
-  public:
-    static char ID;
-    ARMExpandPseudo() : ExpandPseudoInstsPass(ID) {}
+class ARMExpandPseudo : public ExpandPseudoInstsPass {
+public:
+  static char ID;
+  ARMExpandPseudo() : ExpandPseudoInstsPass(ID) {}
 
-    const ARMBaseInstrInfo *TII;
-    const TargetRegisterInfo *TRI;
-    const ARMSubtarget *STI;
-    ARMFunctionInfo *AFI;
+  const ARMBaseInstrInfo *TII;
+  const TargetRegisterInfo *TRI;
+  const ARMSubtarget *STI;
+  ARMFunctionInfo *AFI;
 
-    bool runOnMachineFunction(MachineFunction &Fn) override;
+  bool runOnMachineFunction(MachineFunction &Fn) override;
 
-    MachineFunctionProperties getRequiredProperties() const override {
-      return MachineFunctionProperties().set(
-          MachineFunctionProperties::Property::NoVRegs);
-    }
-
-    StringRef getPassName() const override {
-      return ARM_EXPAND_PSEUDO_NAME;
-    }
+  MachineFunctionProperties getRequiredProperties() const override {
+    return MachineFunctionProperties().set(
+        MachineFunctionProperties::Property::NoVRegs);
+  }
 
-  private:
-    bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                  MachineBasicBlock::iterator &NextMBBI) override;
-    void ExpandVLD(MachineBasicBlock::iterator &MBBI);
-    void ExpandVST(MachineBasicBlock::iterator &MBBI);
-    void ExpandLaneOp(MachineBasicBlock::iterator &MBBI);
-    void ExpandVTBL(MachineBasicBlock::iterator &MBBI,
-                    unsigned Opc, bool IsExt);
-    void ExpandMQQPRLoadStore(MachineBasicBlock::iterator &MBBI);
-    void ExpandTMOV32BitImm(MachineBasicBlock &MBB,
-                            MachineBasicBlock::iterator &MBBI);
-    void ExpandMOV32BitImm(MachineBasicBlock &MBB,
-                           MachineBasicBlock::iterator &MBBI);
-    void CMSEClearGPRegs(MachineBasicBlock &MBB,
-                         MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
-                         const SmallVectorImpl<unsigned> &ClearRegs,
-                         unsigned ClobberReg);
-    MachineBasicBlock &CMSEClearFPRegs(MachineBasicBlock &MBB,
-                                       MachineBasicBlock::iterator MBBI);
-    MachineBasicBlock &CMSEClearFPRegsV8(MachineBasicBlock &MBB,
-                                         MachineBasicBlock::iterator MBBI,
-                                         const BitVector &ClearRegs);
-    MachineBasicBlock &CMSEClearFPRegsV81(MachineBasicBlock &MBB,
-                                          MachineBasicBlock::iterator MBBI,
-                                          const BitVector &ClearRegs);
-    void CMSESaveClearFPRegs(MachineBasicBlock &MBB,
-                             MachineBasicBlock::iterator MBBI, DebugLoc &DL,
-                             const LivePhysRegs &LiveRegs,
-                             SmallVectorImpl<unsigned> &AvailableRegs);
-    void CMSESaveClearFPRegsV8(MachineBasicBlock &MBB,
-                               MachineBasicBlock::iterator MBBI, DebugLoc &DL,
-                               const LivePhysRegs &LiveRegs,
-                               SmallVectorImpl<unsigned> &ScratchRegs);
-    void CMSESaveClearFPRegsV81(MachineBasicBlock &MBB,
-                                MachineBasicBlock::iterator MBBI, DebugLoc &DL,
-                                const LivePhysRegs &LiveRegs);
-    void CMSERestoreFPRegs(MachineBasicBlock &MBB,
+  StringRef getPassName() const override { return ARM_EXPAND_PSEUDO_NAME; }
+
+private:
+  bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                MachineBasicBlock::iterator &NextMBBI) override;
+  void ExpandVLD(MachineBasicBlock::iterator &MBBI);
+  void ExpandVST(MachineBasicBlock::iterator &MBBI);
+  void ExpandLaneOp(MachineBasicBlock::iterator &MBBI);
+  void ExpandVTBL(MachineBasicBlock::iterator &MBBI, unsigned Opc, bool IsExt);
+  void ExpandMQQPRLoadStore(MachineBasicBlock::iterator &MBBI);
+  void ExpandTMOV32BitImm(MachineBasicBlock &MBB,
+                          MachineBasicBlock::iterator &MBBI);
+  void ExpandMOV32BitImm(MachineBasicBlock &MBB,
+                         MachineBasicBlock::iterator &MBBI);
+  void CMSEClearGPRegs(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                       const DebugLoc &DL,
+                       const SmallVectorImpl<unsigned> &ClearRegs,
+                       unsigned ClobberReg);
+  MachineBasicBlock &CMSEClearFPRegs(MachineBasicBlock &MBB,
+                                     MachineBasicBlock::iterator MBBI);
+  MachineBasicBlock &CMSEClearFPRegsV8(MachineBasicBlock &MBB,
+                                       MachineBasicBlock::iterator MBBI,
+                                       const BitVector &ClearRegs);
+  MachineBasicBlock &CMSEClearFPRegsV81(MachineBasicBlock &MBB,
+                                        MachineBasicBlock::iterator MBBI,
+                                        const BitVector &ClearRegs);
+  void CMSESaveClearFPRegs(MachineBasicBlock &MBB,
                            MachineBasicBlock::iterator MBBI, DebugLoc &DL,
+                           const LivePhysRegs &LiveRegs,
                            SmallVectorImpl<unsigned> &AvailableRegs);
-    void CMSERestoreFPRegsV8(MachineBasicBlock &MBB,
+  void CMSESaveClearFPRegsV8(MachineBasicBlock &MBB,
                              MachineBasicBlock::iterator MBBI, DebugLoc &DL,
-                             SmallVectorImpl<unsigned> &AvailableRegs);
-    void CMSERestoreFPRegsV81(MachineBasicBlock &MBB,
+                             const LivePhysRegs &LiveRegs,
+                             SmallVectorImpl<unsigned> &ScratchRegs);
+  void CMSESaveClearFPRegsV81(MachineBasicBlock &MBB,
                               MachineBasicBlock::iterator MBBI, DebugLoc &DL,
-                              SmallVectorImpl<unsigned> &AvailableRegs);
-    bool ExpandCMP_SWAP(MachineBasicBlock &MBB,
-                        MachineBasicBlock::iterator MBBI, unsigned LdrexOp,
-                        unsigned StrexOp, unsigned UxtOp,
-                        MachineBasicBlock::iterator &NextMBBI);
-
-    bool ExpandCMP_SWAP_64(MachineBasicBlock &MBB,
-                           MachineBasicBlock::iterator MBBI,
-                           MachineBasicBlock::iterator &NextMBBI);
-  };
+                              const LivePhysRegs &LiveRegs);
+  void CMSERestoreFPRegs(MachineBasicBlock &MBB,
+                         MachineBasicBlock::iterator MBBI, DebugLoc &DL,
+                         SmallVectorImpl<unsigned> &AvailableRegs);
+  void CMSERestoreFPRegsV8(MachineBasicBlock &MBB,
+                           MachineBasicBlock::iterator MBBI, DebugLoc &DL,
+                           SmallVectorImpl<unsigned> &AvailableRegs);
+  void CMSERestoreFPRegsV81(MachineBasicBlock &MBB,
+                            MachineBasicBlock::iterator MBBI, DebugLoc &DL,
+                            SmallVectorImpl<unsigned> &AvailableRegs);
+  bool ExpandCMP_SWAP(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                      unsigned LdrexOp, unsigned StrexOp, unsigned UxtOp,
+                      MachineBasicBlock::iterator &NextMBBI);
+
+  bool ExpandCMP_SWAP_64(MachineBasicBlock &MBB,
+                         MachineBasicBlock::iterator MBBI,
+                         MachineBasicBlock::iterator &NextMBBI);
+};
   char ARMExpandPseudo::ID = 0;
 }
 
diff --git a/llvm/lib/Target/Mips/MipsExpandPseudo.cpp b/llvm/lib/Target/Mips/MipsExpandPseudo.cpp
index 9e9d7ba33db0..8bbc3739672f 100644
--- a/llvm/lib/Target/Mips/MipsExpandPseudo.cpp
+++ b/llvm/lib/Target/Mips/MipsExpandPseudo.cpp
@@ -30,43 +30,42 @@ using namespace llvm;
 #define DEBUG_TYPE "mips-pseudo"
 
 namespace {
-  class MipsExpandPseudo : public ExpandPseudoInstsPass {
-  public:
-    static char ID;
-    MipsExpandPseudo() : ExpandPseudoInstsPass(ID) {}
+class MipsExpandPseudo : public ExpandPseudoInstsPass {
+public:
+  static char ID;
+  MipsExpandPseudo() : ExpandPseudoInstsPass(ID) {}
 
-    const MipsInstrInfo *TII;
-    const MipsSubtarget *STI;
+  const MipsInstrInfo *TII;
+  const MipsSubtarget *STI;
 
-    bool runOnMachineFunction(MachineFunction &Fn) override;
+  bool runOnMachineFunction(MachineFunction &Fn) override;
 
-    MachineFunctionProperties getRequiredProperties() const override {
-      return MachineFunctionProperties().set(
-          MachineFunctionProperties::Property::NoVRegs);
-    }
+  MachineFunctionProperties getRequiredProperties() const override {
+    return MachineFunctionProperties().set(
+        MachineFunctionProperties::Property::NoVRegs);
+  }
 
-    StringRef getPassName() const override {
-      return "Mips pseudo instruction expansion pass";
-    }
+  StringRef getPassName() const override {
+    return "Mips pseudo instruction expansion pass";
+  }
 
-  private:
-    bool expandAtomicCmpSwap(MachineBasicBlock &MBB,
-                             MachineBasicBlock::iterator MBBI,
-                             MachineBasicBlock::iterator &NextMBBI);
-    bool expandAtomicCmpSwapSubword(MachineBasicBlock &MBB,
-                                    MachineBasicBlock::iterator MBBI,
-                                    MachineBasicBlock::iterator &NextMBBI);
-
-    bool expandAtomicBinOp(MachineBasicBlock &BB,
-                           MachineBasicBlock::iterator I,
-                           MachineBasicBlock::iterator &NMBBI, unsigned Size);
-    bool expandAtomicBinOpSubword(MachineBasicBlock &BB,
-                                  MachineBasicBlock::iterator I,
-                                  MachineBasicBlock::iterator &NMBBI);
-
-    bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
-                  MachineBasicBlock::iterator &NMBB) override;
-   };
+private:
+  bool expandAtomicCmpSwap(MachineBasicBlock &MBB,
+                           MachineBasicBlock::iterator MBBI,
+                           MachineBasicBlock::iterator &NextMBBI);
+  bool expandAtomicCmpSwapSubword(MachineBasicBlock &MBB,
+                                  MachineBasicBlock::iterator MBBI,
+                                  MachineBasicBlock::iterator &NextMBBI);
+
+  bool expandAtomicBinOp(MachineBasicBlock &BB, MachineBasicBlock::iterator I,
+                         MachineBasicBlock::iterator &NMBBI, unsigned Size);
+  bool expandAtomicBinOpSubword(MachineBasicBlock &BB,
+                                MachineBasicBlock::iterator I,
+                                MachineBasicBlock::iterator &NMBBI);
+
+  bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+                MachineBasicBlock::iterator &NMBB) override;
+};
   char MipsExpandPseudo::ID = 0;
 }
 

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 29, 2023

I agree - I'm not sure if this patch is that useful, it avoids a little duplicate code but we have plenty of cases where targets duplicate code (either by design or due to one target copying another implementation).

@dtcxzyw dtcxzyw closed this Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants