xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineLoopInfo.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MachineLoopInfo class that is used to identify natural
10 // loops and determine the loop depth of various nodes of the CFG.  Note that
11 // the loops identified may actually be several natural loops that share the
12 // same header node... not just a single natural loop.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/CodeGen/MachineLoopInfo.h"
17 #include "llvm/Analysis/LoopInfoImpl.h"
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25 
26 // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
27 template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
28 template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
29 
30 char MachineLoopInfo::ID = 0;
31 MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
32   initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
33 }
34 INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
35                 "Machine Natural Loop Construction", true, true)
36 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
37 INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
38                 "Machine Natural Loop Construction", true, true)
39 
40 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
41 
42 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
43   calculate(getAnalysis<MachineDominatorTree>());
44   return false;
45 }
46 
47 void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
48   releaseMemory();
49   LI.analyze(MDT.getBase());
50 }
51 
52 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
53   AU.setPreservesAll();
54   AU.addRequired<MachineDominatorTree>();
55   MachineFunctionPass::getAnalysisUsage(AU);
56 }
57 
58 MachineBasicBlock *MachineLoop::getTopBlock() {
59   MachineBasicBlock *TopMBB = getHeader();
60   MachineFunction::iterator Begin = TopMBB->getParent()->begin();
61   if (TopMBB->getIterator() != Begin) {
62     MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
63     while (contains(PriorMBB)) {
64       TopMBB = PriorMBB;
65       if (TopMBB->getIterator() == Begin)
66         break;
67       PriorMBB = &*std::prev(TopMBB->getIterator());
68     }
69   }
70   return TopMBB;
71 }
72 
73 MachineBasicBlock *MachineLoop::getBottomBlock() {
74   MachineBasicBlock *BotMBB = getHeader();
75   MachineFunction::iterator End = BotMBB->getParent()->end();
76   if (BotMBB->getIterator() != std::prev(End)) {
77     MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
78     while (contains(NextMBB)) {
79       BotMBB = NextMBB;
80       if (BotMBB == &*std::next(BotMBB->getIterator()))
81         break;
82       NextMBB = &*std::next(BotMBB->getIterator());
83     }
84   }
85   return BotMBB;
86 }
87 
88 MachineBasicBlock *MachineLoop::findLoopControlBlock() {
89   if (MachineBasicBlock *Latch = getLoopLatch()) {
90     if (isLoopExiting(Latch))
91       return Latch;
92     else
93       return getExitingBlock();
94   }
95   return nullptr;
96 }
97 
98 DebugLoc MachineLoop::getStartLoc() const {
99   // Try the pre-header first.
100   if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
101     if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
102       if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
103         return DL;
104 
105   // If we have no pre-header or there are no instructions with debug
106   // info in it, try the header.
107   if (MachineBasicBlock *HeadMBB = getHeader())
108     if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
109       return HeadBB->getTerminator()->getDebugLoc();
110 
111   return DebugLoc();
112 }
113 
114 MachineBasicBlock *
115 MachineLoopInfo::findLoopPreheader(MachineLoop *L,
116                                    bool SpeculativePreheader) const {
117   if (MachineBasicBlock *PB = L->getLoopPreheader())
118     return PB;
119 
120   if (!SpeculativePreheader)
121     return nullptr;
122 
123   MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
124   if (HB->pred_size() != 2 || HB->hasAddressTaken())
125     return nullptr;
126   // Find the predecessor of the header that is not the latch block.
127   MachineBasicBlock *Preheader = nullptr;
128   for (MachineBasicBlock *P : HB->predecessors()) {
129     if (P == LB)
130       continue;
131     // Sanity.
132     if (Preheader)
133       return nullptr;
134     Preheader = P;
135   }
136 
137   // Check if the preheader candidate is a successor of any other loop
138   // headers. We want to avoid having two loop setups in the same block.
139   for (MachineBasicBlock *S : Preheader->successors()) {
140     if (S == HB)
141       continue;
142     MachineLoop *T = getLoopFor(S);
143     if (T && T->getHeader() == S)
144       return nullptr;
145   }
146   return Preheader;
147 }
148 
149 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
150 LLVM_DUMP_METHOD void MachineLoop::dump() const {
151   print(dbgs());
152 }
153 #endif
154