diff --git a/llvm/include/llvm/CodeGen/MachineCopyPropagation.h b/llvm/include/llvm/CodeGen/MachineCopyPropagation.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/CodeGen/MachineCopyPropagation.h @@ -0,0 +1,57 @@ +//===- MachineCopyPropagation.h - Machine Copy Propagation Pass ---------===// +// +// 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 is an extremely simple MachineInstr-level copy propagation pass. +// +// This pass forwards the source of COPYs to the users of their destinations +// when doing so is legal. For example: +// +// %reg1 = COPY %reg0 +// ... +// ... = OP %reg1 +// +// If +// - %reg0 has not been clobbered by the time of the use of %reg1 +// - the register class constraints are satisfied +// - the COPY def is the only value that reaches OP +// then this pass replaces the above with: +// +// %reg1 = COPY %reg0 +// ... +// ... = OP %reg0 +// +// This pass also removes some redundant COPYs. For example: +// +// %R1 = COPY %R0 +// ... // No clobber of %R1 +// %R0 = COPY %R1 <<< Removed +// +// or +// +// %R1 = COPY %R0 +// ... // No clobber of %R0 +// %R1 = COPY %R0 <<< Removed +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_MACHINE_COPY_PROPAGATION_H +#define LLVM_CODEGEN_MACHINE_COPY_PROPAGATION_H + +#include "llvm/CodeGen/PassManager.h" +#include "llvm/IR/PassManager.h" + +namespace llvm { + +// The Machine Copy Propagation pass. +struct MachineCopyPropagationPass : PassInfoMixin { + PreservedAnalyses run(MachineFunction &MF, + MachineFunctionAnalysisManager &AM); +}; +} // namespace llvm + +#endif // LLVM_CODEGEN_MACHINE_COPY_PROPAGATION_H diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -257,7 +257,7 @@ void initializeMachineBranchProbabilityInfoPass(PassRegistry&); void initializeMachineCSEPass(PassRegistry &); void initializeMachineCombinerPass(PassRegistry&); -void initializeMachineCopyPropagationPass(PassRegistry &); +void initializeMachineCopyPropagationLegacyPassPass(PassRegistry &); void initializeMachineDominanceFrontierPass(PassRegistry&); void initializeMachineDominatorTreePass(PassRegistry &); void initializeMachineFunctionPrinterPassPass(PassRegistry&); diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp --- a/llvm/lib/CodeGen/CodeGen.cpp +++ b/llvm/lib/CodeGen/CodeGen.cpp @@ -58,8 +58,8 @@ initializeMachineBlockPlacementStatsPass(Registry); initializeMachineCSEPass(Registry); initializeMachineCombinerPass(Registry); - initializeMachineCopyPropagationPass(Registry); initializeMachineDominatorTreePass(Registry); + initializeMachineCopyPropagationLegacyPassPass(Registry); initializeMachineFunctionPrinterPassPass(Registry); initializeMachineLICMPass(Registry); initializeMachineLoopInfoPass(Registry); diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp --- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp +++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp @@ -39,6 +39,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/CodeGen/MachineCopyPropagation.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" @@ -180,16 +181,12 @@ } }; -class MachineCopyPropagation : public MachineFunctionPass { - const TargetRegisterInfo *TRI; - const TargetInstrInfo *TII; - const MachineRegisterInfo *MRI; - +class MachineCopyPropagationLegacyPass : public MachineFunctionPass { public: static char ID; // Pass identification, replacement for typeid - - MachineCopyPropagation() : MachineFunctionPass(ID) { - initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); + MachineCopyPropagationLegacyPass() : MachineFunctionPass(ID) { + initializeMachineCopyPropagationLegacyPassPass( + *PassRegistry::getPassRegistry()); } void getAnalysisUsage(AnalysisUsage &AU) const override { @@ -203,11 +200,24 @@ return MachineFunctionProperties().set( MachineFunctionProperties::Property::NoVRegs); } +}; + +class MachineCopyPropagation { + const TargetRegisterInfo *TRI; + const TargetInstrInfo *TII; + const MachineRegisterInfo *MRI; + +public: + bool Changed; + MachineCopyPropagation(const TargetRegisterInfo *TRI, + const TargetInstrInfo *TII, + const MachineRegisterInfo *MRI) + : TRI(TRI), TII(TII), MRI(MRI), Changed(false) {} + void CopyPropagateBlock(MachineBasicBlock &MBB); private: void ClobberRegister(unsigned Reg); void ReadRegister(unsigned Reg); - void CopyPropagateBlock(MachineBasicBlock &MBB); bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def); void forwardUses(MachineInstr &MI); bool isForwardableRegClassCopy(const MachineInstr &Copy, @@ -218,17 +228,15 @@ SmallSetVector MaybeDeadCopies; CopyTracker Tracker; - - bool Changed; }; } // end anonymous namespace -char MachineCopyPropagation::ID = 0; +char MachineCopyPropagationLegacyPass::ID = 0; -char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; +char &llvm::MachineCopyPropagationID = MachineCopyPropagationLegacyPass::ID; -INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, +INITIALIZE_PASS(MachineCopyPropagationLegacyPass, DEBUG_TYPE, "Machine Copy Propagation Pass", false, false) void MachineCopyPropagation::ReadRegister(unsigned Reg) { @@ -623,18 +631,27 @@ Tracker.clear(); } -bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { - if (skipFunction(MF.getFunction())) - return false; +MachineCopyPropagation copyPropagate(MachineFunction &MF) { + MachineCopyPropagation info{MF.getSubtarget().getRegisterInfo(), + MF.getSubtarget().getInstrInfo(), + &MF.getRegInfo()}; - Changed = false; + for (MachineBasicBlock &MBB : MF) + info.CopyPropagateBlock(MBB); - TRI = MF.getSubtarget().getRegisterInfo(); - TII = MF.getSubtarget().getInstrInfo(); - MRI = &MF.getRegInfo(); + return info; +} - for (MachineBasicBlock &MBB : MF) - CopyPropagateBlock(MBB); +PreservedAnalyses +MachineCopyPropagationPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &AM) { + return copyPropagate(MF).Changed ? PreservedAnalyses::all() + : PreservedAnalyses::none(); +} - return Changed; +bool MachineCopyPropagationLegacyPass::runOnMachineFunction( + MachineFunction &MF) { + if (skipFunction(MF.getFunction())) + return false; + return copyPropagate(MF).Changed; } diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -312,3 +312,8 @@ }, parseLoopUnswitchOptions) #undef LOOP_PASS_WITH_PARAMS +#ifndef MACHINE_FUNCTION_PASS +#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS) +#endif +MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass()) +#undef MACHINE_FUNCTION_PASS