@@ -433,6 +433,8 @@ class PeepholeOptimizer : private MachineFunction::Delegate {
433433 MachineDominatorTree *DT = nullptr ; // Machine dominator tree
434434 MachineLoopInfo *MLI = nullptr ;
435435
436+ bool NeedToInvalidateMLI = false ;
437+
436438public:
437439 PeepholeOptimizer (MachineDominatorTree *DT, MachineLoopInfo *MLI)
438440 : DT(DT), MLI(MLI) {}
@@ -444,6 +446,7 @@ class PeepholeOptimizer : private MachineFunction::Delegate {
444446 // / Sequence of instructions that formulate recurrence cycle.
445447 using RecurrenceCycle = SmallVector<RecurrenceInstr, 4 >;
446448
449+ bool needToInvalidateMLI () const { return NeedToInvalidateMLI; }
447450private:
448451 bool optimizeCmpInstr (MachineInstr &MI);
449452 bool optimizeExtInstr (MachineInstr &MI, MachineBasicBlock &MBB,
@@ -568,8 +571,10 @@ class PeepholeOptimizerLegacy : public MachineFunctionPass {
568571 void getAnalysisUsage (AnalysisUsage &AU) const override {
569572 MachineFunctionPass::getAnalysisUsage (AU);
570573 AU.addRequired <MachineLoopInfoWrapperPass>();
571- if (Aggressive)
574+ if (Aggressive) {
572575 AU.addRequired <MachineDominatorTreeWrapperPass>();
576+ AU.addPreserved <MachineDominatorTreeWrapperPass>();
577+ }
573578 }
574579
575580 MachineFunctionProperties getRequiredProperties () const override {
@@ -1654,7 +1659,13 @@ PeepholeOptimizerPass::run(MachineFunction &MF,
16541659 if (!Changed)
16551660 return PreservedAnalyses::all ();
16561661
1657- return getMachineFunctionPassPreservedAnalyses ();
1662+ auto PA = getMachineFunctionPassPreservedAnalyses ();
1663+ PA.preserve <MachineDominatorTreeAnalysis>();
1664+ if (!Impl.needToInvalidateMLI ()) {
1665+ PA.preserve <MachineLoopAnalysis>();
1666+ PA.preserveSet <CFGAnalyses>();
1667+ }
1668+ return PA;
16581669}
16591670
16601671bool PeepholeOptimizerLegacy::runOnMachineFunction (MachineFunction &MF) {
@@ -1783,17 +1794,21 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
17831794 }
17841795
17851796 if (MI->isConditionalBranch () && optimizeCondBranch (*MI)) {
1797+ NeedToInvalidateMLI = true ;
17861798 // optimizeCondBranch might have converted a conditional branch to
17871799 // an unconditional branch. If there is a branch instruction after it,
17881800 // delete it.
17891801 MachineInstr *NewBr = &*std::prev (MII);
17901802 if (NewBr->isUnconditionalBranch ()) {
17911803 if (MII != MBB.end ()) {
17921804 MachineInstr *Dead = &*MII;
1793- ++MII;
17941805 MachineBasicBlock *DeadDest = TII->getBranchDestBlock (*Dead);
1795- if (DT && TII->getBranchDestBlock (*NewBr) != DeadDest)
1806+ if (DT && TII->getBranchDestBlock (*NewBr) != DeadDest) {
17961807 DT->deleteEdge (&MBB, DeadDest);
1808+ MLI->calculate (*DT);
1809+ NeedToInvalidateMLI = false ;
1810+ }
1811+ ++MII;
17971812 Dead->eraseFromParent ();
17981813 }
17991814 }
0 commit comments