10b57cec5SDimitry Andric //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the MachineLoopInfo class that is used to identify natural
100b57cec5SDimitry Andric // loops and determine the loop depth of various nodes of the CFG.  Note that
110b57cec5SDimitry Andric // the loops identified may actually be several natural loops that share the
120b57cec5SDimitry Andric // same header node... not just a single natural loop.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfoImpl.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
19af732203SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
21af732203SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
220b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
26af732203SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
300b57cec5SDimitry Andric template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
310b57cec5SDimitry Andric template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric char MachineLoopInfo::ID = 0;
MachineLoopInfo()34480093f4SDimitry Andric MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
35480093f4SDimitry Andric   initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
36480093f4SDimitry Andric }
370b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
380b57cec5SDimitry Andric                 "Machine Natural Loop Construction", true, true)
390b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
400b57cec5SDimitry Andric INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
410b57cec5SDimitry Andric                 "Machine Natural Loop Construction", true, true)
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
440b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction &)450b57cec5SDimitry Andric bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
46480093f4SDimitry Andric   calculate(getAnalysis<MachineDominatorTree>());
470b57cec5SDimitry Andric   return false;
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
calculate(MachineDominatorTree & MDT)50480093f4SDimitry Andric void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
51480093f4SDimitry Andric   releaseMemory();
52480093f4SDimitry Andric   LI.analyze(MDT.getBase());
53480093f4SDimitry Andric }
54480093f4SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const550b57cec5SDimitry Andric void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
560b57cec5SDimitry Andric   AU.setPreservesAll();
570b57cec5SDimitry Andric   AU.addRequired<MachineDominatorTree>();
580b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
getTopBlock()610b57cec5SDimitry Andric MachineBasicBlock *MachineLoop::getTopBlock() {
620b57cec5SDimitry Andric   MachineBasicBlock *TopMBB = getHeader();
630b57cec5SDimitry Andric   MachineFunction::iterator Begin = TopMBB->getParent()->begin();
640b57cec5SDimitry Andric   if (TopMBB->getIterator() != Begin) {
650b57cec5SDimitry Andric     MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
660b57cec5SDimitry Andric     while (contains(PriorMBB)) {
670b57cec5SDimitry Andric       TopMBB = PriorMBB;
680b57cec5SDimitry Andric       if (TopMBB->getIterator() == Begin)
690b57cec5SDimitry Andric         break;
700b57cec5SDimitry Andric       PriorMBB = &*std::prev(TopMBB->getIterator());
710b57cec5SDimitry Andric     }
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric   return TopMBB;
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
getBottomBlock()760b57cec5SDimitry Andric MachineBasicBlock *MachineLoop::getBottomBlock() {
770b57cec5SDimitry Andric   MachineBasicBlock *BotMBB = getHeader();
780b57cec5SDimitry Andric   MachineFunction::iterator End = BotMBB->getParent()->end();
790b57cec5SDimitry Andric   if (BotMBB->getIterator() != std::prev(End)) {
800b57cec5SDimitry Andric     MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
810b57cec5SDimitry Andric     while (contains(NextMBB)) {
820b57cec5SDimitry Andric       BotMBB = NextMBB;
830b57cec5SDimitry Andric       if (BotMBB == &*std::next(BotMBB->getIterator()))
840b57cec5SDimitry Andric         break;
850b57cec5SDimitry Andric       NextMBB = &*std::next(BotMBB->getIterator());
860b57cec5SDimitry Andric     }
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric   return BotMBB;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
findLoopControlBlock()910b57cec5SDimitry Andric MachineBasicBlock *MachineLoop::findLoopControlBlock() {
920b57cec5SDimitry Andric   if (MachineBasicBlock *Latch = getLoopLatch()) {
930b57cec5SDimitry Andric     if (isLoopExiting(Latch))
940b57cec5SDimitry Andric       return Latch;
950b57cec5SDimitry Andric     else
960b57cec5SDimitry Andric       return getExitingBlock();
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric   return nullptr;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
getStartLoc() const1010b57cec5SDimitry Andric DebugLoc MachineLoop::getStartLoc() const {
1020b57cec5SDimitry Andric   // Try the pre-header first.
1030b57cec5SDimitry Andric   if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
1040b57cec5SDimitry Andric     if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
1050b57cec5SDimitry Andric       if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
1060b57cec5SDimitry Andric         return DL;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // If we have no pre-header or there are no instructions with debug
1090b57cec5SDimitry Andric   // info in it, try the header.
1100b57cec5SDimitry Andric   if (MachineBasicBlock *HeadMBB = getHeader())
1110b57cec5SDimitry Andric     if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
1120b57cec5SDimitry Andric       return HeadBB->getTerminator()->getDebugLoc();
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   return DebugLoc();
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric MachineBasicBlock *
findLoopPreheader(MachineLoop * L,bool SpeculativePreheader,bool FindMultiLoopPreheader) const118*5f7ddb14SDimitry Andric MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
119*5f7ddb14SDimitry Andric                                    bool FindMultiLoopPreheader) const {
1200b57cec5SDimitry Andric   if (MachineBasicBlock *PB = L->getLoopPreheader())
1210b57cec5SDimitry Andric     return PB;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   if (!SpeculativePreheader)
1240b57cec5SDimitry Andric     return nullptr;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
1270b57cec5SDimitry Andric   if (HB->pred_size() != 2 || HB->hasAddressTaken())
1280b57cec5SDimitry Andric     return nullptr;
1290b57cec5SDimitry Andric   // Find the predecessor of the header that is not the latch block.
1300b57cec5SDimitry Andric   MachineBasicBlock *Preheader = nullptr;
1310b57cec5SDimitry Andric   for (MachineBasicBlock *P : HB->predecessors()) {
1320b57cec5SDimitry Andric     if (P == LB)
1330b57cec5SDimitry Andric       continue;
1340b57cec5SDimitry Andric     // Sanity.
1350b57cec5SDimitry Andric     if (Preheader)
1360b57cec5SDimitry Andric       return nullptr;
1370b57cec5SDimitry Andric     Preheader = P;
1380b57cec5SDimitry Andric   }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   // Check if the preheader candidate is a successor of any other loop
1410b57cec5SDimitry Andric   // headers. We want to avoid having two loop setups in the same block.
142*5f7ddb14SDimitry Andric   if (!FindMultiLoopPreheader) {
1430b57cec5SDimitry Andric     for (MachineBasicBlock *S : Preheader->successors()) {
1440b57cec5SDimitry Andric       if (S == HB)
1450b57cec5SDimitry Andric         continue;
1460b57cec5SDimitry Andric       MachineLoop *T = getLoopFor(S);
1470b57cec5SDimitry Andric       if (T && T->getHeader() == S)
1480b57cec5SDimitry Andric         return nullptr;
1490b57cec5SDimitry Andric     }
150*5f7ddb14SDimitry Andric   }
1510b57cec5SDimitry Andric   return Preheader;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
isLoopInvariant(MachineInstr & I) const154af732203SDimitry Andric bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
155af732203SDimitry Andric   MachineFunction *MF = I.getParent()->getParent();
156af732203SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
157af732203SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
158af732203SDimitry Andric 
159af732203SDimitry Andric   // The instruction is loop invariant if all of its operands are.
160af732203SDimitry Andric   for (const MachineOperand &MO : I.operands()) {
161af732203SDimitry Andric     if (!MO.isReg())
162af732203SDimitry Andric       continue;
163af732203SDimitry Andric 
164af732203SDimitry Andric     Register Reg = MO.getReg();
165af732203SDimitry Andric     if (Reg == 0) continue;
166af732203SDimitry Andric 
167af732203SDimitry Andric     // An instruction that uses or defines a physical register can't e.g. be
168af732203SDimitry Andric     // hoisted, so mark this as not invariant.
169af732203SDimitry Andric     if (Register::isPhysicalRegister(Reg)) {
170af732203SDimitry Andric       if (MO.isUse()) {
171af732203SDimitry Andric         // If the physreg has no defs anywhere, it's just an ambient register
172af732203SDimitry Andric         // and we can freely move its uses. Alternatively, if it's allocatable,
173af732203SDimitry Andric         // it could get allocated to something with a def during allocation.
174af732203SDimitry Andric         // However, if the physreg is known to always be caller saved/restored
175af732203SDimitry Andric         // then this use is safe to hoist.
176af732203SDimitry Andric         if (!MRI->isConstantPhysReg(Reg) &&
177af732203SDimitry Andric             !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())))
178af732203SDimitry Andric           return false;
179af732203SDimitry Andric         // Otherwise it's safe to move.
180af732203SDimitry Andric         continue;
181af732203SDimitry Andric       } else if (!MO.isDead()) {
182af732203SDimitry Andric         // A def that isn't dead can't be moved.
183af732203SDimitry Andric         return false;
184af732203SDimitry Andric       } else if (getHeader()->isLiveIn(Reg)) {
185af732203SDimitry Andric         // If the reg is live into the loop, we can't hoist an instruction
186af732203SDimitry Andric         // which would clobber it.
187af732203SDimitry Andric         return false;
188af732203SDimitry Andric       }
189af732203SDimitry Andric     }
190af732203SDimitry Andric 
191af732203SDimitry Andric     if (!MO.isUse())
192af732203SDimitry Andric       continue;
193af732203SDimitry Andric 
194af732203SDimitry Andric     assert(MRI->getVRegDef(Reg) &&
195af732203SDimitry Andric            "Machine instr not mapped for this vreg?!");
196af732203SDimitry Andric 
197af732203SDimitry Andric     // If the loop contains the definition of an operand, then the instruction
198af732203SDimitry Andric     // isn't loop invariant.
199af732203SDimitry Andric     if (contains(MRI->getVRegDef(Reg)))
200af732203SDimitry Andric       return false;
201af732203SDimitry Andric   }
202af732203SDimitry Andric 
203af732203SDimitry Andric   // If we got this far, the instruction is loop invariant!
204af732203SDimitry Andric   return true;
205af732203SDimitry Andric }
206af732203SDimitry Andric 
2070b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2080b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineLoop::dump() const {
2090b57cec5SDimitry Andric   print(dbgs());
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric #endif
212