xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineLoopInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
19e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
20349cc55cSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
21e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
220b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
2481ad6265SDimitry Andric #include "llvm/Pass.h"
2581ad6265SDimitry Andric #include "llvm/PassRegistry.h"
26e8d8bef9SDimitry 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;
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 
450b57cec5SDimitry Andric bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
46480093f4SDimitry Andric   calculate(getAnalysis<MachineDominatorTree>());
470b57cec5SDimitry Andric   return false;
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
50480093f4SDimitry Andric void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
51480093f4SDimitry Andric   releaseMemory();
52480093f4SDimitry Andric   LI.analyze(MDT.getBase());
53480093f4SDimitry Andric }
54480093f4SDimitry Andric 
550b57cec5SDimitry 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 
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 
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 
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 
1010b57cec5SDimitry 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 *
118fe6060f1SDimitry Andric MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
119fe6060f1SDimitry 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.
142fe6060f1SDimitry 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     }
150fe6060f1SDimitry Andric   }
1510b57cec5SDimitry Andric   return Preheader;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
154e8d8bef9SDimitry Andric bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
155e8d8bef9SDimitry Andric   MachineFunction *MF = I.getParent()->getParent();
156e8d8bef9SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
157349cc55cSDimitry Andric   const TargetSubtargetInfo &ST = MF->getSubtarget();
158349cc55cSDimitry Andric   const TargetRegisterInfo *TRI = ST.getRegisterInfo();
159349cc55cSDimitry Andric   const TargetInstrInfo *TII = ST.getInstrInfo();
160e8d8bef9SDimitry Andric 
161e8d8bef9SDimitry Andric   // The instruction is loop invariant if all of its operands are.
162e8d8bef9SDimitry Andric   for (const MachineOperand &MO : I.operands()) {
163e8d8bef9SDimitry Andric     if (!MO.isReg())
164e8d8bef9SDimitry Andric       continue;
165e8d8bef9SDimitry Andric 
166e8d8bef9SDimitry Andric     Register Reg = MO.getReg();
167e8d8bef9SDimitry Andric     if (Reg == 0) continue;
168e8d8bef9SDimitry Andric 
169e8d8bef9SDimitry Andric     // An instruction that uses or defines a physical register can't e.g. be
170e8d8bef9SDimitry Andric     // hoisted, so mark this as not invariant.
171*bdd1243dSDimitry Andric     if (Reg.isPhysical()) {
172e8d8bef9SDimitry Andric       if (MO.isUse()) {
173e8d8bef9SDimitry Andric         // If the physreg has no defs anywhere, it's just an ambient register
174e8d8bef9SDimitry Andric         // and we can freely move its uses. Alternatively, if it's allocatable,
175e8d8bef9SDimitry Andric         // it could get allocated to something with a def during allocation.
176e8d8bef9SDimitry Andric         // However, if the physreg is known to always be caller saved/restored
177e8d8bef9SDimitry Andric         // then this use is safe to hoist.
178e8d8bef9SDimitry Andric         if (!MRI->isConstantPhysReg(Reg) &&
179349cc55cSDimitry Andric             !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
180349cc55cSDimitry Andric             !TII->isIgnorableUse(MO))
181e8d8bef9SDimitry Andric           return false;
182e8d8bef9SDimitry Andric         // Otherwise it's safe to move.
183e8d8bef9SDimitry Andric         continue;
184e8d8bef9SDimitry Andric       } else if (!MO.isDead()) {
185e8d8bef9SDimitry Andric         // A def that isn't dead can't be moved.
186e8d8bef9SDimitry Andric         return false;
187e8d8bef9SDimitry Andric       } else if (getHeader()->isLiveIn(Reg)) {
188e8d8bef9SDimitry Andric         // If the reg is live into the loop, we can't hoist an instruction
189e8d8bef9SDimitry Andric         // which would clobber it.
190e8d8bef9SDimitry Andric         return false;
191e8d8bef9SDimitry Andric       }
192e8d8bef9SDimitry Andric     }
193e8d8bef9SDimitry Andric 
194e8d8bef9SDimitry Andric     if (!MO.isUse())
195e8d8bef9SDimitry Andric       continue;
196e8d8bef9SDimitry Andric 
197e8d8bef9SDimitry Andric     assert(MRI->getVRegDef(Reg) &&
198e8d8bef9SDimitry Andric            "Machine instr not mapped for this vreg?!");
199e8d8bef9SDimitry Andric 
200e8d8bef9SDimitry Andric     // If the loop contains the definition of an operand, then the instruction
201e8d8bef9SDimitry Andric     // isn't loop invariant.
202e8d8bef9SDimitry Andric     if (contains(MRI->getVRegDef(Reg)))
203e8d8bef9SDimitry Andric       return false;
204e8d8bef9SDimitry Andric   }
205e8d8bef9SDimitry Andric 
206e8d8bef9SDimitry Andric   // If we got this far, the instruction is loop invariant!
207e8d8bef9SDimitry Andric   return true;
208e8d8bef9SDimitry Andric }
209e8d8bef9SDimitry Andric 
2100b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2110b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineLoop::dump() const {
2120b57cec5SDimitry Andric   print(dbgs());
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric #endif
215