diff --git a/llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h b/llvm/include/llvm/CodeGen/ExpandPseudoInstsPass.h new file mode 100644 index 0000000000000..b7c47dc1a410d --- /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 389c70d04f17b..aa8e0c4455d3a 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 0000000000000..f64c82bd8bca3 --- /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 42bc390a67b2b..e20a3c9d5c9b9 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(MF.getSubtarget().getInstrInfo()); diff --git a/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp b/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp index 2f9236bb977fc..2830ae03cc786 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(); 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 f257ccea6c50a..2e683cb6f1c6e 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 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(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 18a532b55ee5a..4fc3e8c048f8b 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 72c1f1cec1983..f0a793ea2ebbe 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 13268d754a9dd..7214fd5ef4b0e 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; - } - - return Modified; -} - bool M68kExpandPseudo::runOnMachineFunction(MachineFunction &MF) { STI = &MF.getSubtarget(); TII = STI->getInstrInfo(); @@ -309,7 +294,7 @@ bool M68kExpandPseudo::runOnMachineFunction(MachineFunction &MF) { bool Modified = false; for (MachineBasicBlock &MBB : MF) - Modified |= ExpandMBB(MBB); + Modified |= expandMBB(MBB); return Modified; } diff --git a/llvm/lib/Target/Mips/MipsExpandPseudo.cpp b/llvm/lib/Target/Mips/MipsExpandPseudo.cpp index 2c2554b5b4bc3..9e9d7ba33db04 100644 --- a/llvm/lib/Target/Mips/MipsExpandPseudo.cpp +++ b/llvm/lib/Target/Mips/MipsExpandPseudo.cpp @@ -21,8 +21,8 @@ #include "Mips.h" #include "MipsInstrInfo.h" #include "MipsSubtarget.h" +#include "llvm/CodeGen/ExpandPseudoInstsPass.h" #include "llvm/CodeGen/LivePhysRegs.h" -#include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" using namespace llvm; @@ -30,10 +30,10 @@ using namespace llvm; #define DEBUG_TYPE "mips-pseudo" namespace { - class MipsExpandPseudo : public MachineFunctionPass { + class MipsExpandPseudo : public ExpandPseudoInstsPass { public: static char ID; - MipsExpandPseudo() : MachineFunctionPass(ID) {} + MipsExpandPseudo() : ExpandPseudoInstsPass(ID) {} const MipsInstrInfo *TII; const MipsSubtarget *STI; @@ -65,8 +65,7 @@ namespace { MachineBasicBlock::iterator &NMBBI); bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, - MachineBasicBlock::iterator &NMBB); - bool expandMBB(MachineBasicBlock &MBB); + MachineBasicBlock::iterator &NMBB) override; }; char MipsExpandPseudo::ID = 0; } @@ -878,19 +877,6 @@ bool MipsExpandPseudo::expandMI(MachineBasicBlock &MBB, } } -bool MipsExpandPseudo::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 MipsExpandPseudo::runOnMachineFunction(MachineFunction &MF) { STI = &MF.getSubtarget(); TII = STI->getInstrInfo(); diff --git a/llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp b/llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp index bb772fc5da922..80cd98ae1aeb6 100644 --- a/llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp +++ b/llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp @@ -17,8 +17,8 @@ #include "RISCVInstrInfo.h" #include "RISCVTargetMachine.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,13 +28,13 @@ using namespace llvm; namespace { -class RISCVExpandAtomicPseudo : public MachineFunctionPass { +class RISCVExpandAtomicPseudo : public ExpandPseudoInstsPass { public: const RISCVSubtarget *STI; const RISCVInstrInfo *TII; static char ID; - RISCVExpandAtomicPseudo() : MachineFunctionPass(ID) { + RISCVExpandAtomicPseudo() : ExpandPseudoInstsPass(ID) { initializeRISCVExpandAtomicPseudoPass(*PassRegistry::getPassRegistry()); } @@ -45,9 +45,8 @@ class RISCVExpandAtomicPseudo : 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, @@ -91,19 +90,6 @@ bool RISCVExpandAtomicPseudo::runOnMachineFunction(MachineFunction &MF) { return Modified; } -bool RISCVExpandAtomicPseudo::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 RISCVExpandAtomicPseudo::expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI) { diff --git a/llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp b/llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp index ff35a8fc9c5a1..cd27a741d1530 100644 --- a/llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp +++ b/llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp @@ -16,8 +16,8 @@ #include "RISCVInstrInfo.h" #include "RISCVTargetMachine.h" +#include "llvm/CodeGen/ExpandPseudoInstsPass.h" #include "llvm/CodeGen/LivePhysRegs.h" -#include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/MC/MCContext.h" @@ -28,13 +28,13 @@ using namespace llvm; namespace { -class RISCVExpandPseudo : public MachineFunctionPass { +class RISCVExpandPseudo : public ExpandPseudoInstsPass { public: const RISCVSubtarget *STI; const RISCVInstrInfo *TII; static char ID; - RISCVExpandPseudo() : MachineFunctionPass(ID) { + RISCVExpandPseudo() : ExpandPseudoInstsPass(ID) { initializeRISCVExpandPseudoPass(*PassRegistry::getPassRegistry()); } @@ -43,9 +43,8 @@ class RISCVExpandPseudo : public MachineFunctionPass { StringRef getPassName() const override { return RISCV_EXPAND_PSEUDO_NAME; } private: - bool expandMBB(MachineBasicBlock &MBB); bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, - MachineBasicBlock::iterator &NextMBBI); + MachineBasicBlock::iterator &NextMBBI) override; bool expandCCOp(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI); bool expandVSetVL(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); @@ -87,19 +86,6 @@ bool RISCVExpandPseudo::runOnMachineFunction(MachineFunction &MF) { return Modified; } -bool RISCVExpandPseudo::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 RISCVExpandPseudo::expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI) { @@ -375,13 +361,13 @@ bool RISCVExpandPseudo::expandRV32ZdinxLoad(MachineBasicBlock &MBB, return true; } -class RISCVPreRAExpandPseudo : public MachineFunctionPass { +class RISCVPreRAExpandPseudo : public ExpandPseudoInstsPass { public: const RISCVSubtarget *STI; const RISCVInstrInfo *TII; static char ID; - RISCVPreRAExpandPseudo() : MachineFunctionPass(ID) { + RISCVPreRAExpandPseudo() : ExpandPseudoInstsPass(ID) { initializeRISCVPreRAExpandPseudoPass(*PassRegistry::getPassRegistry()); } @@ -396,9 +382,8 @@ class RISCVPreRAExpandPseudo : public MachineFunctionPass { } private: - bool expandMBB(MachineBasicBlock &MBB); bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, - MachineBasicBlock::iterator &NextMBBI); + MachineBasicBlock::iterator &NextMBBI) override; bool expandAuipcInstPair(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, @@ -447,19 +432,6 @@ bool RISCVPreRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) { return Modified; } -bool RISCVPreRAExpandPseudo::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 RISCVPreRAExpandPseudo::expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI) { diff --git a/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp b/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp index 407e7cfd6fef8..2ea9e83ed7352 100644 --- a/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp +++ b/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp @@ -16,7 +16,7 @@ #include "RISCV.h" #include "RISCVInstrInfo.h" #include "RISCVTargetMachine.h" -#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/ExpandPseudoInstsPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" using namespace llvm; @@ -26,12 +26,12 @@ using namespace llvm; namespace { -class RISCVPostRAExpandPseudo : public MachineFunctionPass { +class RISCVPostRAExpandPseudo : public ExpandPseudoInstsPass { public: const RISCVInstrInfo *TII; static char ID; - RISCVPostRAExpandPseudo() : MachineFunctionPass(ID) { + RISCVPostRAExpandPseudo() : ExpandPseudoInstsPass(ID) { initializeRISCVPostRAExpandPseudoPass(*PassRegistry::getPassRegistry()); } @@ -42,9 +42,8 @@ class RISCVPostRAExpandPseudo : public MachineFunctionPass { } private: - bool expandMBB(MachineBasicBlock &MBB); bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, - MachineBasicBlock::iterator &NextMBBI); + MachineBasicBlock::iterator &NextMBBI) override; bool expandMovImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); }; @@ -58,19 +57,6 @@ bool RISCVPostRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) { return Modified; } -bool RISCVPostRAExpandPseudo::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 RISCVPostRAExpandPseudo::expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI) { diff --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp index 085fa9280b0ea..33935dd745336 100644 --- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp +++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp @@ -18,8 +18,8 @@ #include "X86InstrInfo.h" #include "X86MachineFunctionInfo.h" #include "X86Subtarget.h" +#include "llvm/CodeGen/ExpandPseudoInstsPass.h" #include "llvm/CodeGen/LivePhysRegs.h" -#include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved. #include "llvm/IR/EHPersonalities.h" @@ -31,10 +31,10 @@ using namespace llvm; #define X86_EXPAND_PSEUDO_NAME "X86 pseudo instruction expansion pass" namespace { -class X86ExpandPseudo : public MachineFunctionPass { +class X86ExpandPseudo : public ExpandPseudoInstsPass { public: static char ID; - X86ExpandPseudo() : MachineFunctionPass(ID) {} + X86ExpandPseudo() : ExpandPseudoInstsPass(ID) {} void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); @@ -65,12 +65,12 @@ class X86ExpandPseudo : public MachineFunctionPass { MachineBasicBlock::iterator MBBI); void expandCALL_RVMARKER(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); - bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); - bool ExpandMBB(MachineBasicBlock &MBB); + bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, + MachineBasicBlock::iterator &NextMBBI) override; /// This function expands pseudos which affects control flow. /// It is done in separate pass to simplify blocks navigation in main - /// pass(calling ExpandMBB). + /// pass(calling expandMBB). bool ExpandPseudosWhichAffectControlFlow(MachineFunction &MF); /// Expand X86::VASTART_SAVE_XMM_REGS into set of xmm copying instructions, @@ -259,8 +259,9 @@ void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB, /// 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 X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MBBI) { +bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, + MachineBasicBlock::iterator &NextMBBI) { MachineInstr &MI = *MBBI; unsigned Opcode = MI.getOpcode(); const DebugLoc &DL = MBBI->getDebugLoc(); @@ -707,22 +708,6 @@ void X86ExpandPseudo::ExpandVastartSaveXmmRegs( VAStartPseudoInstr->eraseFromParent(); } -/// Expand all pseudo instructions contained in \p MBB. -/// \returns true if any expansion occurred for \p MBB. -bool X86ExpandPseudo::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; - } - - return Modified; -} - bool X86ExpandPseudo::ExpandPseudosWhichAffectControlFlow(MachineFunction &MF) { // Currently pseudo which affects control flow is only // X86::VASTART_SAVE_XMM_REGS which is located in Entry block. @@ -747,7 +732,7 @@ bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) { bool Modified = ExpandPseudosWhichAffectControlFlow(MF); for (MachineBasicBlock &MBB : MF) - Modified |= ExpandMBB(MBB); + Modified |= expandMBB(MBB); return Modified; }