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