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/CodeGen/MachineDominators.h"
18e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
19349cc55cSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
20e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
210b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
22480093f4SDimitry Andric #include "llvm/InitializePasses.h"
2381ad6265SDimitry Andric #include "llvm/Pass.h"
2481ad6265SDimitry Andric #include "llvm/PassRegistry.h"
25fe013be4SDimitry Andric #include "llvm/Support/GenericLoopInfoImpl.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;
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() const91*c9157d92SDimitry Andric MachineBasicBlock *MachineLoop::findLoopControlBlock() const {
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) const118fe6060f1SDimitry 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
getLoopID() const154*c9157d92SDimitry Andric MDNode *MachineLoop::getLoopID() const {
155*c9157d92SDimitry Andric MDNode *LoopID = nullptr;
156*c9157d92SDimitry Andric if (const auto *MBB = findLoopControlBlock()) {
157*c9157d92SDimitry Andric // If there is a single latch block, then the metadata
158*c9157d92SDimitry Andric // node is attached to its terminating instruction.
159*c9157d92SDimitry Andric const auto *BB = MBB->getBasicBlock();
160*c9157d92SDimitry Andric if (!BB)
161*c9157d92SDimitry Andric return nullptr;
162*c9157d92SDimitry Andric if (const auto *TI = BB->getTerminator())
163*c9157d92SDimitry Andric LoopID = TI->getMetadata(LLVMContext::MD_loop);
164*c9157d92SDimitry Andric } else if (const auto *MBB = getHeader()) {
165*c9157d92SDimitry Andric // There seem to be multiple latch blocks, so we have to
166*c9157d92SDimitry Andric // visit all predecessors of the loop header and check
167*c9157d92SDimitry Andric // their terminating instructions for the metadata.
168*c9157d92SDimitry Andric if (const auto *Header = MBB->getBasicBlock()) {
169*c9157d92SDimitry Andric // Walk over all blocks in the loop.
170*c9157d92SDimitry Andric for (const auto *MBB : this->blocks()) {
171*c9157d92SDimitry Andric const auto *BB = MBB->getBasicBlock();
172*c9157d92SDimitry Andric if (!BB)
173*c9157d92SDimitry Andric return nullptr;
174*c9157d92SDimitry Andric const auto *TI = BB->getTerminator();
175*c9157d92SDimitry Andric if (!TI)
176*c9157d92SDimitry Andric return nullptr;
177*c9157d92SDimitry Andric MDNode *MD = nullptr;
178*c9157d92SDimitry Andric // Check if this terminating instruction jumps to the loop header.
179*c9157d92SDimitry Andric for (const auto *Succ : successors(TI)) {
180*c9157d92SDimitry Andric if (Succ == Header) {
181*c9157d92SDimitry Andric // This is a jump to the header - gather the metadata from it.
182*c9157d92SDimitry Andric MD = TI->getMetadata(LLVMContext::MD_loop);
183*c9157d92SDimitry Andric break;
184*c9157d92SDimitry Andric }
185*c9157d92SDimitry Andric }
186*c9157d92SDimitry Andric if (!MD)
187*c9157d92SDimitry Andric return nullptr;
188*c9157d92SDimitry Andric if (!LoopID)
189*c9157d92SDimitry Andric LoopID = MD;
190*c9157d92SDimitry Andric else if (MD != LoopID)
191*c9157d92SDimitry Andric return nullptr;
192*c9157d92SDimitry Andric }
193*c9157d92SDimitry Andric }
194*c9157d92SDimitry Andric }
195*c9157d92SDimitry Andric if (LoopID &&
196*c9157d92SDimitry Andric (LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID))
197*c9157d92SDimitry Andric LoopID = nullptr;
198*c9157d92SDimitry Andric return LoopID;
199*c9157d92SDimitry Andric }
200*c9157d92SDimitry Andric
isLoopInvariant(MachineInstr & I) const201e8d8bef9SDimitry Andric bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
202e8d8bef9SDimitry Andric MachineFunction *MF = I.getParent()->getParent();
203e8d8bef9SDimitry Andric MachineRegisterInfo *MRI = &MF->getRegInfo();
204349cc55cSDimitry Andric const TargetSubtargetInfo &ST = MF->getSubtarget();
205349cc55cSDimitry Andric const TargetRegisterInfo *TRI = ST.getRegisterInfo();
206349cc55cSDimitry Andric const TargetInstrInfo *TII = ST.getInstrInfo();
207e8d8bef9SDimitry Andric
208e8d8bef9SDimitry Andric // The instruction is loop invariant if all of its operands are.
209e8d8bef9SDimitry Andric for (const MachineOperand &MO : I.operands()) {
210e8d8bef9SDimitry Andric if (!MO.isReg())
211e8d8bef9SDimitry Andric continue;
212e8d8bef9SDimitry Andric
213e8d8bef9SDimitry Andric Register Reg = MO.getReg();
214e8d8bef9SDimitry Andric if (Reg == 0) continue;
215e8d8bef9SDimitry Andric
216e8d8bef9SDimitry Andric // An instruction that uses or defines a physical register can't e.g. be
217e8d8bef9SDimitry Andric // hoisted, so mark this as not invariant.
218bdd1243dSDimitry Andric if (Reg.isPhysical()) {
219e8d8bef9SDimitry Andric if (MO.isUse()) {
220e8d8bef9SDimitry Andric // If the physreg has no defs anywhere, it's just an ambient register
221e8d8bef9SDimitry Andric // and we can freely move its uses. Alternatively, if it's allocatable,
222e8d8bef9SDimitry Andric // it could get allocated to something with a def during allocation.
223e8d8bef9SDimitry Andric // However, if the physreg is known to always be caller saved/restored
224e8d8bef9SDimitry Andric // then this use is safe to hoist.
225e8d8bef9SDimitry Andric if (!MRI->isConstantPhysReg(Reg) &&
226349cc55cSDimitry Andric !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
227349cc55cSDimitry Andric !TII->isIgnorableUse(MO))
228e8d8bef9SDimitry Andric return false;
229e8d8bef9SDimitry Andric // Otherwise it's safe to move.
230e8d8bef9SDimitry Andric continue;
231e8d8bef9SDimitry Andric } else if (!MO.isDead()) {
232e8d8bef9SDimitry Andric // A def that isn't dead can't be moved.
233e8d8bef9SDimitry Andric return false;
234e8d8bef9SDimitry Andric } else if (getHeader()->isLiveIn(Reg)) {
235e8d8bef9SDimitry Andric // If the reg is live into the loop, we can't hoist an instruction
236e8d8bef9SDimitry Andric // which would clobber it.
237e8d8bef9SDimitry Andric return false;
238e8d8bef9SDimitry Andric }
239e8d8bef9SDimitry Andric }
240e8d8bef9SDimitry Andric
241e8d8bef9SDimitry Andric if (!MO.isUse())
242e8d8bef9SDimitry Andric continue;
243e8d8bef9SDimitry Andric
244e8d8bef9SDimitry Andric assert(MRI->getVRegDef(Reg) &&
245e8d8bef9SDimitry Andric "Machine instr not mapped for this vreg?!");
246e8d8bef9SDimitry Andric
247e8d8bef9SDimitry Andric // If the loop contains the definition of an operand, then the instruction
248e8d8bef9SDimitry Andric // isn't loop invariant.
249e8d8bef9SDimitry Andric if (contains(MRI->getVRegDef(Reg)))
250e8d8bef9SDimitry Andric return false;
251e8d8bef9SDimitry Andric }
252e8d8bef9SDimitry Andric
253e8d8bef9SDimitry Andric // If we got this far, the instruction is loop invariant!
254e8d8bef9SDimitry Andric return true;
255e8d8bef9SDimitry Andric }
256e8d8bef9SDimitry Andric
2570b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2580b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineLoop::dump() const {
2590b57cec5SDimitry Andric print(dbgs());
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric #endif
262