65
65
// C = copy A <-- same-bank copy
66
66
// ===----------------------------------------------------------------------===//
67
67
68
+ #include " llvm/CodeGen/PeepholeOptimizer.h"
68
69
#include " llvm/ADT/DenseMap.h"
69
70
#include " llvm/ADT/SmallPtrSet.h"
70
71
#include " llvm/ADT/SmallSet.h"
78
79
#include " llvm/CodeGen/MachineInstrBuilder.h"
79
80
#include " llvm/CodeGen/MachineLoopInfo.h"
80
81
#include " llvm/CodeGen/MachineOperand.h"
82
+ #include " llvm/CodeGen/MachinePassManager.h"
81
83
#include " llvm/CodeGen/MachineRegisterInfo.h"
82
84
#include " llvm/CodeGen/TargetInstrInfo.h"
83
85
#include " llvm/CodeGen/TargetOpcodes.h"
@@ -147,39 +149,18 @@ namespace {
147
149
class ValueTrackerResult ;
148
150
class RecurrenceInstr ;
149
151
150
- class PeepholeOptimizer : public MachineFunctionPass ,
151
- private MachineFunction::Delegate {
152
+ class PeepholeOptimizer : private MachineFunction ::Delegate {
152
153
const TargetInstrInfo *TII = nullptr ;
153
154
const TargetRegisterInfo *TRI = nullptr ;
154
155
MachineRegisterInfo *MRI = nullptr ;
155
156
MachineDominatorTree *DT = nullptr ; // Machine dominator tree
156
157
MachineLoopInfo *MLI = nullptr ;
157
158
158
159
public:
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) {}
182
162
163
+ bool run (MachineFunction &MF);
183
164
// / Track Def -> Use info used for rewriting copies.
184
165
using RewriteMapTy = SmallDenseMap<RegSubRegPair, ValueTrackerResult>;
185
166
@@ -294,6 +275,33 @@ class PeepholeOptimizer : public MachineFunctionPass,
294
275
}
295
276
};
296
277
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
+
297
305
// / Helper class to hold instructions that are inside recurrence cycles.
298
306
// / The recurrence cycle is formulated around 1) a def operand and its
299
307
// / tied use operand, or 2) a def operand and a use operand that is commutable
@@ -469,16 +477,16 @@ class ValueTracker {
469
477
470
478
} // end anonymous namespace
471
479
472
- char PeepholeOptimizer ::ID = 0 ;
480
+ char PeepholeOptimizerLegacy ::ID = 0 ;
473
481
474
- char &llvm::PeepholeOptimizerID = PeepholeOptimizer ::ID;
482
+ char &llvm::PeepholeOptimizerLegacyID = PeepholeOptimizerLegacy ::ID;
475
483
476
- INITIALIZE_PASS_BEGIN (PeepholeOptimizer , DEBUG_TYPE, " Peephole Optimizations " ,
477
- false , false )
484
+ INITIALIZE_PASS_BEGIN (PeepholeOptimizerLegacy , DEBUG_TYPE,
485
+ " Peephole Optimizations " , false , false )
478
486
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
479
487
INITIALIZE_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 )
482
490
483
491
// / If instruction is a copy-like instruction, i.e. it reads a single register
484
492
// / and writes a single register and it does not modify the source, and if the
@@ -1644,9 +1652,37 @@ bool PeepholeOptimizer::optimizeRecurrence(MachineInstr &PHI) {
1644
1652
return Changed;
1645
1653
}
1646
1654
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) {
1648
1675
if (skipFunction (MF.getFunction ()))
1649
1676
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) {
1650
1686
1651
1687
LLVM_DEBUG (dbgs () << " ********** PEEPHOLE OPTIMIZER **********\n " );
1652
1688
LLVM_DEBUG (dbgs () << " ********** Function: " << MF.getName () << ' \n ' );
@@ -1657,9 +1693,6 @@ bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
1657
1693
TII = MF.getSubtarget ().getInstrInfo ();
1658
1694
TRI = MF.getSubtarget ().getRegisterInfo ();
1659
1695
MRI = &MF.getRegInfo ();
1660
- DT = Aggressive ? &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ()
1661
- : nullptr ;
1662
- MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
1663
1696
MF.setDelegate (this );
1664
1697
1665
1698
bool Changed = false ;
0 commit comments