6565// C = copy A <-- same-bank copy
6666// ===----------------------------------------------------------------------===//
6767
68+ #include " llvm/CodeGen/PeepholeOptimizer.h"
6869#include " llvm/ADT/DenseMap.h"
6970#include " llvm/ADT/SmallPtrSet.h"
7071#include " llvm/ADT/SmallSet.h"
7879#include " llvm/CodeGen/MachineInstrBuilder.h"
7980#include " llvm/CodeGen/MachineLoopInfo.h"
8081#include " llvm/CodeGen/MachineOperand.h"
82+ #include " llvm/CodeGen/MachinePassManager.h"
8183#include " llvm/CodeGen/MachineRegisterInfo.h"
8284#include " llvm/CodeGen/TargetInstrInfo.h"
8385#include " llvm/CodeGen/TargetOpcodes.h"
@@ -147,39 +149,18 @@ namespace {
147149class ValueTrackerResult ;
148150class RecurrenceInstr ;
149151
150- class PeepholeOptimizer : public MachineFunctionPass ,
151- private MachineFunction::Delegate {
152+ class PeepholeOptimizer : private MachineFunction ::Delegate {
152153 const TargetInstrInfo *TII = nullptr ;
153154 const TargetRegisterInfo *TRI = nullptr ;
154155 MachineRegisterInfo *MRI = nullptr ;
155156 MachineDominatorTree *DT = nullptr ; // Machine dominator tree
156157 MachineLoopInfo *MLI = nullptr ;
157158
158159public:
159- static char ID; // Pass identification
160-
161- PeepholeOptimizer () : MachineFunctionPass(ID) {
162- initializePeepholeOptimizerPass (*PassRegistry::getPassRegistry ());
163- }
164-
165- bool runOnMachineFunction (MachineFunction &MF) override ;
166-
167- void getAnalysisUsage (AnalysisUsage &AU) const override {
168- AU.setPreservesCFG ();
169- MachineFunctionPass::getAnalysisUsage (AU);
170- AU.addRequired <MachineLoopInfoWrapperPass>();
171- AU.addPreserved <MachineLoopInfoWrapperPass>();
172- if (Aggressive) {
173- AU.addRequired <MachineDominatorTreeWrapperPass>();
174- AU.addPreserved <MachineDominatorTreeWrapperPass>();
175- }
176- }
177-
178- MachineFunctionProperties getRequiredProperties () const override {
179- return MachineFunctionProperties ().set (
180- MachineFunctionProperties::Property::IsSSA);
181- }
160+ PeepholeOptimizer (MachineDominatorTree *DT, MachineLoopInfo *MLI)
161+ : DT(DT), MLI(MLI) {}
182162
163+ bool run (MachineFunction &MF);
183164 // / Track Def -> Use info used for rewriting copies.
184165 using RewriteMapTy = SmallDenseMap<RegSubRegPair, ValueTrackerResult>;
185166
@@ -294,6 +275,33 @@ class PeepholeOptimizer : public MachineFunctionPass,
294275 }
295276};
296277
278+ class PeepholeOptimizerLegacy : public MachineFunctionPass {
279+ public:
280+ static char ID; // Pass identification
281+
282+ PeepholeOptimizerLegacy () : MachineFunctionPass(ID) {
283+ initializePeepholeOptimizerLegacyPass (*PassRegistry::getPassRegistry ());
284+ }
285+
286+ bool runOnMachineFunction (MachineFunction &MF) override ;
287+
288+ void getAnalysisUsage (AnalysisUsage &AU) const override {
289+ AU.setPreservesCFG ();
290+ MachineFunctionPass::getAnalysisUsage (AU);
291+ AU.addRequired <MachineLoopInfoWrapperPass>();
292+ AU.addPreserved <MachineLoopInfoWrapperPass>();
293+ if (Aggressive) {
294+ AU.addRequired <MachineDominatorTreeWrapperPass>();
295+ AU.addPreserved <MachineDominatorTreeWrapperPass>();
296+ }
297+ }
298+
299+ MachineFunctionProperties getRequiredProperties () const override {
300+ return MachineFunctionProperties ().set (
301+ MachineFunctionProperties::Property::IsSSA);
302+ }
303+ };
304+
297305// / Helper class to hold instructions that are inside recurrence cycles.
298306// / The recurrence cycle is formulated around 1) a def operand and its
299307// / tied use operand, or 2) a def operand and a use operand that is commutable
@@ -469,16 +477,16 @@ class ValueTracker {
469477
470478} // end anonymous namespace
471479
472- char PeepholeOptimizer ::ID = 0 ;
480+ char PeepholeOptimizerLegacy ::ID = 0 ;
473481
474- char &llvm::PeepholeOptimizerID = PeepholeOptimizer ::ID;
482+ char &llvm::PeepholeOptimizerLegacyID = PeepholeOptimizerLegacy ::ID;
475483
476- INITIALIZE_PASS_BEGIN (PeepholeOptimizer , DEBUG_TYPE, " Peephole Optimizations " ,
477- false , false )
484+ INITIALIZE_PASS_BEGIN (PeepholeOptimizerLegacy , DEBUG_TYPE,
485+ " Peephole Optimizations " , false , false )
478486INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
479487INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
480- INITIALIZE_PASS_END(PeepholeOptimizer , DEBUG_TYPE, " Peephole Optimizations " ,
481- false , false )
488+ INITIALIZE_PASS_END(PeepholeOptimizerLegacy , DEBUG_TYPE,
489+ " Peephole Optimizations " , false , false )
482490
483491// / If instruction is a copy-like instruction, i.e. it reads a single register
484492// / and writes a single register and it does not modify the source, and if the
@@ -1644,9 +1652,37 @@ bool PeepholeOptimizer::optimizeRecurrence(MachineInstr &PHI) {
16441652 return Changed;
16451653}
16461654
1647- bool PeepholeOptimizer::runOnMachineFunction (MachineFunction &MF) {
1655+ PreservedAnalyses
1656+ PeepholeOptimizerPass::run (MachineFunction &MF,
1657+ MachineFunctionAnalysisManager &MFAM) {
1658+ MFPropsModifier _ (*this , MF);
1659+ auto *DT =
1660+ Aggressive ? &MFAM.getResult <MachineDominatorTreeAnalysis>(MF) : nullptr ;
1661+ auto *MLI = &MFAM.getResult <MachineLoopAnalysis>(MF);
1662+ PeepholeOptimizer Impl (DT, MLI);
1663+ bool Changed = Impl.run (MF);
1664+ if (!Changed)
1665+ return PreservedAnalyses::all ();
1666+
1667+ auto PA = getMachineFunctionPassPreservedAnalyses ();
1668+ PA.preserve <MachineDominatorTreeAnalysis>();
1669+ PA.preserve <MachineLoopAnalysis>();
1670+ PA.preserveSet <CFGAnalyses>();
1671+ return PA;
1672+ }
1673+
1674+ bool PeepholeOptimizerLegacy::runOnMachineFunction (MachineFunction &MF) {
16481675 if (skipFunction (MF.getFunction ()))
16491676 return false ;
1677+ auto *DT = Aggressive
1678+ ? &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ()
1679+ : nullptr ;
1680+ auto *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
1681+ PeepholeOptimizer Impl (DT, MLI);
1682+ return Impl.run (MF);
1683+ }
1684+
1685+ bool PeepholeOptimizer::run (MachineFunction &MF) {
16501686
16511687 LLVM_DEBUG (dbgs () << " ********** PEEPHOLE OPTIMIZER **********\n " );
16521688 LLVM_DEBUG (dbgs () << " ********** Function: " << MF.getName () << ' \n ' );
@@ -1657,9 +1693,6 @@ bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
16571693 TII = MF.getSubtarget ().getInstrInfo ();
16581694 TRI = MF.getSubtarget ().getRegisterInfo ();
16591695 MRI = &MF.getRegInfo ();
1660- DT = Aggressive ? &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ()
1661- : nullptr ;
1662- MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
16631696 MF.setDelegate (this );
16641697
16651698 bool Changed = false ;
0 commit comments