xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/CodeGen/MachineLoopInfo.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines the MachineLoopInfo class that is used to identify natural
107330f729Sjoerg // loops and determine the loop depth of various nodes of the CFG.  Note that
117330f729Sjoerg // the loops identified may actually be several natural loops that share the
127330f729Sjoerg // same header node... not just a single natural loop.
137330f729Sjoerg //
147330f729Sjoerg //===----------------------------------------------------------------------===//
157330f729Sjoerg 
167330f729Sjoerg #include "llvm/CodeGen/MachineLoopInfo.h"
177330f729Sjoerg #include "llvm/Analysis/LoopInfoImpl.h"
187330f729Sjoerg #include "llvm/CodeGen/MachineDominators.h"
19*82d56013Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
207330f729Sjoerg #include "llvm/CodeGen/Passes.h"
21*82d56013Sjoerg #include "llvm/CodeGen/TargetSubtargetInfo.h"
227330f729Sjoerg #include "llvm/Config/llvm-config.h"
23*82d56013Sjoerg #include "llvm/InitializePasses.h"
247330f729Sjoerg #include "llvm/Support/Debug.h"
257330f729Sjoerg #include "llvm/Support/raw_ostream.h"
26*82d56013Sjoerg 
277330f729Sjoerg using namespace llvm;
287330f729Sjoerg 
297330f729Sjoerg // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
307330f729Sjoerg template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
317330f729Sjoerg template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
327330f729Sjoerg 
337330f729Sjoerg char MachineLoopInfo::ID = 0;
MachineLoopInfo()34*82d56013Sjoerg MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
35*82d56013Sjoerg   initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
36*82d56013Sjoerg }
377330f729Sjoerg INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
387330f729Sjoerg                 "Machine Natural Loop Construction", true, true)
397330f729Sjoerg INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
407330f729Sjoerg INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
417330f729Sjoerg                 "Machine Natural Loop Construction", true, true)
427330f729Sjoerg 
437330f729Sjoerg char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
447330f729Sjoerg 
runOnMachineFunction(MachineFunction &)457330f729Sjoerg bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
467330f729Sjoerg   calculate(getAnalysis<MachineDominatorTree>());
477330f729Sjoerg   return false;
487330f729Sjoerg }
497330f729Sjoerg 
calculate(MachineDominatorTree & MDT)507330f729Sjoerg void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
517330f729Sjoerg   releaseMemory();
527330f729Sjoerg   LI.analyze(MDT.getBase());
537330f729Sjoerg }
547330f729Sjoerg 
getAnalysisUsage(AnalysisUsage & AU) const557330f729Sjoerg void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
567330f729Sjoerg   AU.setPreservesAll();
577330f729Sjoerg   AU.addRequired<MachineDominatorTree>();
587330f729Sjoerg   MachineFunctionPass::getAnalysisUsage(AU);
597330f729Sjoerg }
607330f729Sjoerg 
getTopBlock()617330f729Sjoerg MachineBasicBlock *MachineLoop::getTopBlock() {
627330f729Sjoerg   MachineBasicBlock *TopMBB = getHeader();
637330f729Sjoerg   MachineFunction::iterator Begin = TopMBB->getParent()->begin();
647330f729Sjoerg   if (TopMBB->getIterator() != Begin) {
657330f729Sjoerg     MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
667330f729Sjoerg     while (contains(PriorMBB)) {
677330f729Sjoerg       TopMBB = PriorMBB;
687330f729Sjoerg       if (TopMBB->getIterator() == Begin)
697330f729Sjoerg         break;
707330f729Sjoerg       PriorMBB = &*std::prev(TopMBB->getIterator());
717330f729Sjoerg     }
727330f729Sjoerg   }
737330f729Sjoerg   return TopMBB;
747330f729Sjoerg }
757330f729Sjoerg 
getBottomBlock()767330f729Sjoerg MachineBasicBlock *MachineLoop::getBottomBlock() {
777330f729Sjoerg   MachineBasicBlock *BotMBB = getHeader();
787330f729Sjoerg   MachineFunction::iterator End = BotMBB->getParent()->end();
797330f729Sjoerg   if (BotMBB->getIterator() != std::prev(End)) {
807330f729Sjoerg     MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
817330f729Sjoerg     while (contains(NextMBB)) {
827330f729Sjoerg       BotMBB = NextMBB;
837330f729Sjoerg       if (BotMBB == &*std::next(BotMBB->getIterator()))
847330f729Sjoerg         break;
857330f729Sjoerg       NextMBB = &*std::next(BotMBB->getIterator());
867330f729Sjoerg     }
877330f729Sjoerg   }
887330f729Sjoerg   return BotMBB;
897330f729Sjoerg }
907330f729Sjoerg 
findLoopControlBlock()917330f729Sjoerg MachineBasicBlock *MachineLoop::findLoopControlBlock() {
927330f729Sjoerg   if (MachineBasicBlock *Latch = getLoopLatch()) {
937330f729Sjoerg     if (isLoopExiting(Latch))
947330f729Sjoerg       return Latch;
957330f729Sjoerg     else
967330f729Sjoerg       return getExitingBlock();
977330f729Sjoerg   }
987330f729Sjoerg   return nullptr;
997330f729Sjoerg }
1007330f729Sjoerg 
getStartLoc() const1017330f729Sjoerg DebugLoc MachineLoop::getStartLoc() const {
1027330f729Sjoerg   // Try the pre-header first.
1037330f729Sjoerg   if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
1047330f729Sjoerg     if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
1057330f729Sjoerg       if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
1067330f729Sjoerg         return DL;
1077330f729Sjoerg 
1087330f729Sjoerg   // If we have no pre-header or there are no instructions with debug
1097330f729Sjoerg   // info in it, try the header.
1107330f729Sjoerg   if (MachineBasicBlock *HeadMBB = getHeader())
1117330f729Sjoerg     if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
1127330f729Sjoerg       return HeadBB->getTerminator()->getDebugLoc();
1137330f729Sjoerg 
1147330f729Sjoerg   return DebugLoc();
1157330f729Sjoerg }
1167330f729Sjoerg 
1177330f729Sjoerg MachineBasicBlock *
findLoopPreheader(MachineLoop * L,bool SpeculativePreheader) const1187330f729Sjoerg MachineLoopInfo::findLoopPreheader(MachineLoop *L,
1197330f729Sjoerg                                    bool SpeculativePreheader) const {
1207330f729Sjoerg   if (MachineBasicBlock *PB = L->getLoopPreheader())
1217330f729Sjoerg     return PB;
1227330f729Sjoerg 
1237330f729Sjoerg   if (!SpeculativePreheader)
1247330f729Sjoerg     return nullptr;
1257330f729Sjoerg 
1267330f729Sjoerg   MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
1277330f729Sjoerg   if (HB->pred_size() != 2 || HB->hasAddressTaken())
1287330f729Sjoerg     return nullptr;
1297330f729Sjoerg   // Find the predecessor of the header that is not the latch block.
1307330f729Sjoerg   MachineBasicBlock *Preheader = nullptr;
1317330f729Sjoerg   for (MachineBasicBlock *P : HB->predecessors()) {
1327330f729Sjoerg     if (P == LB)
1337330f729Sjoerg       continue;
1347330f729Sjoerg     // Sanity.
1357330f729Sjoerg     if (Preheader)
1367330f729Sjoerg       return nullptr;
1377330f729Sjoerg     Preheader = P;
1387330f729Sjoerg   }
1397330f729Sjoerg 
1407330f729Sjoerg   // Check if the preheader candidate is a successor of any other loop
1417330f729Sjoerg   // headers. We want to avoid having two loop setups in the same block.
1427330f729Sjoerg   for (MachineBasicBlock *S : Preheader->successors()) {
1437330f729Sjoerg     if (S == HB)
1447330f729Sjoerg       continue;
1457330f729Sjoerg     MachineLoop *T = getLoopFor(S);
1467330f729Sjoerg     if (T && T->getHeader() == S)
1477330f729Sjoerg       return nullptr;
1487330f729Sjoerg   }
1497330f729Sjoerg   return Preheader;
1507330f729Sjoerg }
1517330f729Sjoerg 
isLoopInvariant(MachineInstr & I) const152*82d56013Sjoerg bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
153*82d56013Sjoerg   MachineFunction *MF = I.getParent()->getParent();
154*82d56013Sjoerg   MachineRegisterInfo *MRI = &MF->getRegInfo();
155*82d56013Sjoerg   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
156*82d56013Sjoerg 
157*82d56013Sjoerg   // The instruction is loop invariant if all of its operands are.
158*82d56013Sjoerg   for (const MachineOperand &MO : I.operands()) {
159*82d56013Sjoerg     if (!MO.isReg())
160*82d56013Sjoerg       continue;
161*82d56013Sjoerg 
162*82d56013Sjoerg     Register Reg = MO.getReg();
163*82d56013Sjoerg     if (Reg == 0) continue;
164*82d56013Sjoerg 
165*82d56013Sjoerg     // An instruction that uses or defines a physical register can't e.g. be
166*82d56013Sjoerg     // hoisted, so mark this as not invariant.
167*82d56013Sjoerg     if (Register::isPhysicalRegister(Reg)) {
168*82d56013Sjoerg       if (MO.isUse()) {
169*82d56013Sjoerg         // If the physreg has no defs anywhere, it's just an ambient register
170*82d56013Sjoerg         // and we can freely move its uses. Alternatively, if it's allocatable,
171*82d56013Sjoerg         // it could get allocated to something with a def during allocation.
172*82d56013Sjoerg         // However, if the physreg is known to always be caller saved/restored
173*82d56013Sjoerg         // then this use is safe to hoist.
174*82d56013Sjoerg         if (!MRI->isConstantPhysReg(Reg) &&
175*82d56013Sjoerg             !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())))
176*82d56013Sjoerg           return false;
177*82d56013Sjoerg         // Otherwise it's safe to move.
178*82d56013Sjoerg         continue;
179*82d56013Sjoerg       } else if (!MO.isDead()) {
180*82d56013Sjoerg         // A def that isn't dead can't be moved.
181*82d56013Sjoerg         return false;
182*82d56013Sjoerg       } else if (getHeader()->isLiveIn(Reg)) {
183*82d56013Sjoerg         // If the reg is live into the loop, we can't hoist an instruction
184*82d56013Sjoerg         // which would clobber it.
185*82d56013Sjoerg         return false;
186*82d56013Sjoerg       }
187*82d56013Sjoerg     }
188*82d56013Sjoerg 
189*82d56013Sjoerg     if (!MO.isUse())
190*82d56013Sjoerg       continue;
191*82d56013Sjoerg 
192*82d56013Sjoerg     assert(MRI->getVRegDef(Reg) &&
193*82d56013Sjoerg            "Machine instr not mapped for this vreg?!");
194*82d56013Sjoerg 
195*82d56013Sjoerg     // If the loop contains the definition of an operand, then the instruction
196*82d56013Sjoerg     // isn't loop invariant.
197*82d56013Sjoerg     if (contains(MRI->getVRegDef(Reg)))
198*82d56013Sjoerg       return false;
199*82d56013Sjoerg   }
200*82d56013Sjoerg 
201*82d56013Sjoerg   // If we got this far, the instruction is loop invariant!
202*82d56013Sjoerg   return true;
203*82d56013Sjoerg }
204*82d56013Sjoerg 
2057330f729Sjoerg #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2067330f729Sjoerg LLVM_DUMP_METHOD void MachineLoop::dump() const {
2077330f729Sjoerg   print(dbgs());
2087330f729Sjoerg }
2097330f729Sjoerg #endif
210