xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/CodeGen/MachineLoopInfo.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
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 // natural loops may actually be several loops that share the same header node.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // This analysis calculates the nesting structure of loops in a function.  For
140b57cec5SDimitry Andric // each natural loop identified, this analysis identifies natural loops
150b57cec5SDimitry Andric // contained entirely within the loop and the basic blocks the make up the loop.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric // It can calculate on the fly various bits of information, for example:
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric //  * whether there is a preheader for the loop
200b57cec5SDimitry Andric //  * the number of back edges to the header
210b57cec5SDimitry Andric //  * whether or not a particular block branches out of the loop
220b57cec5SDimitry Andric //  * the successor blocks of the loop
230b57cec5SDimitry Andric //  * the loop depth
240b57cec5SDimitry Andric //  * the trip count
250b57cec5SDimitry Andric //  * etc...
260b57cec5SDimitry Andric //
270b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #ifndef LLVM_CODEGEN_MACHINELOOPINFO_H
300b57cec5SDimitry Andric #define LLVM_CODEGEN_MACHINELOOPINFO_H
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
34*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachinePassManager.h"
355f757f3fSDimitry Andric #include "llvm/IR/CFG.h"
360b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
3706c3fb27SDimitry Andric #include "llvm/Support/GenericLoopInfo.h"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace llvm {
400b57cec5SDimitry Andric 
41480093f4SDimitry Andric class MachineDominatorTree;
420b57cec5SDimitry Andric // Implementation in LoopInfoImpl.h
430b57cec5SDimitry Andric class MachineLoop;
440b57cec5SDimitry Andric extern template class LoopBase<MachineBasicBlock, MachineLoop>;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
470b57cec5SDimitry Andric public:
480b57cec5SDimitry Andric   /// Return the "top" block in the loop, which is the first block in the linear
490b57cec5SDimitry Andric   /// layout, ignoring any parts of the loop not contiguous with the part that
500b57cec5SDimitry Andric   /// contains the header.
510b57cec5SDimitry Andric   MachineBasicBlock *getTopBlock();
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   /// Return the "bottom" block in the loop, which is the last block in the
540b57cec5SDimitry Andric   /// linear layout, ignoring any parts of the loop not contiguous with the part
550b57cec5SDimitry Andric   /// that contains the header.
560b57cec5SDimitry Andric   MachineBasicBlock *getBottomBlock();
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   /// Find the block that contains the loop control variable and the
590b57cec5SDimitry Andric   /// loop test. This will return the latch block if it's one of the exiting
600b57cec5SDimitry Andric   /// blocks. Otherwise, return the exiting block. Return 'null' when
610b57cec5SDimitry Andric   /// multiple exiting blocks are present.
625f757f3fSDimitry Andric   MachineBasicBlock *findLoopControlBlock() const;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   /// Return the debug location of the start of this loop.
650b57cec5SDimitry Andric   /// This looks for a BB terminating instruction with a known debug
660b57cec5SDimitry Andric   /// location by looking at the preheader and header blocks. If it
670b57cec5SDimitry Andric   /// cannot find a terminating instruction with location information,
680b57cec5SDimitry Andric   /// it returns an unknown location.
690b57cec5SDimitry Andric   DebugLoc getStartLoc() const;
700b57cec5SDimitry Andric 
715f757f3fSDimitry Andric   /// Find the llvm.loop metadata for this loop.
725f757f3fSDimitry Andric   /// If each branch to the header of this loop contains the same llvm.loop
735f757f3fSDimitry Andric   /// metadata, then this metadata node is returned. Otherwise, if any
745f757f3fSDimitry Andric   /// latch instruction does not contain the llvm.loop metadata or
755f757f3fSDimitry Andric   /// multiple latch instructions contain different llvm.loop metadata nodes,
765f757f3fSDimitry Andric   /// then null is returned.
775f757f3fSDimitry Andric   MDNode *getLoopID() const;
785f757f3fSDimitry Andric 
79e8d8bef9SDimitry Andric   /// Returns true if the instruction is loop invariant.
80e8d8bef9SDimitry Andric   /// I.e., all virtual register operands are defined outside of the loop,
81e8d8bef9SDimitry Andric   /// physical registers aren't accessed explicitly, and there are no side
82e8d8bef9SDimitry Andric   /// effects that aren't captured by the operands or other flags.
83*0fca6ea1SDimitry Andric   /// ExcludeReg can be used to exclude the given register from the check
84*0fca6ea1SDimitry Andric   /// i.e. when we're considering hoisting it's definition but not hoisted it
85*0fca6ea1SDimitry Andric   /// yet
86*0fca6ea1SDimitry Andric   bool isLoopInvariant(MachineInstr &I, const Register ExcludeReg = 0) const;
87e8d8bef9SDimitry Andric 
880b57cec5SDimitry Andric   void dump() const;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric private:
910b57cec5SDimitry Andric   friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
920b57cec5SDimitry Andric 
93*0fca6ea1SDimitry Andric   /// Returns true if the given physreg has no defs inside the loop.
94*0fca6ea1SDimitry Andric   bool isLoopInvariantImplicitPhysReg(Register Reg) const;
95*0fca6ea1SDimitry Andric 
960b57cec5SDimitry Andric   explicit MachineLoop(MachineBasicBlock *MBB)
970b57cec5SDimitry Andric     : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   MachineLoop() = default;
1000b57cec5SDimitry Andric };
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric // Implementation in LoopInfoImpl.h
1030b57cec5SDimitry Andric extern template class LoopInfoBase<MachineBasicBlock, MachineLoop>;
1040b57cec5SDimitry Andric 
105*0fca6ea1SDimitry Andric class MachineLoopInfo : public LoopInfoBase<MachineBasicBlock, MachineLoop> {
1060b57cec5SDimitry Andric   friend class LoopBase<MachineBasicBlock, MachineLoop>;
107*0fca6ea1SDimitry Andric   friend class MachineLoopInfoWrapperPass;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric public:
110*0fca6ea1SDimitry Andric   MachineLoopInfo() = default;
111*0fca6ea1SDimitry Andric   explicit MachineLoopInfo(MachineDominatorTree &MDT) { calculate(MDT); }
112*0fca6ea1SDimitry Andric   MachineLoopInfo(MachineLoopInfo &&) = default;
1130b57cec5SDimitry Andric   MachineLoopInfo(const MachineLoopInfo &) = delete;
1140b57cec5SDimitry Andric   MachineLoopInfo &operator=(const MachineLoopInfo &) = delete;
1150b57cec5SDimitry Andric 
116*0fca6ea1SDimitry Andric   /// Handle invalidation explicitly.
117*0fca6ea1SDimitry Andric   bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
118*0fca6ea1SDimitry Andric                   MachineFunctionAnalysisManager::Invalidator &);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   /// Find the block that either is the loop preheader, or could
1210b57cec5SDimitry Andric   /// speculatively be used as the preheader. This is e.g. useful to place
1220b57cec5SDimitry Andric   /// loop setup code. Code that cannot be speculated should not be placed
1230b57cec5SDimitry Andric   /// here. SpeculativePreheader is controlling whether it also tries to
1240b57cec5SDimitry Andric   /// find the speculative preheader if the regular preheader is not present.
125fe6060f1SDimitry Andric   /// With FindMultiLoopPreheader = false, nullptr will be returned if the found
126fe6060f1SDimitry Andric   /// preheader is the preheader of multiple loops.
127fe6060f1SDimitry Andric   MachineBasicBlock *
128fe6060f1SDimitry Andric   findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false,
129fe6060f1SDimitry Andric                     bool FindMultiLoopPreheader = false) const;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   /// Calculate the natural loop information.
132480093f4SDimitry Andric   void calculate(MachineDominatorTree &MDT);
133*0fca6ea1SDimitry Andric };
134*0fca6ea1SDimitry Andric 
135*0fca6ea1SDimitry Andric /// Analysis pass that exposes the \c MachineLoopInfo for a machine function.
136*0fca6ea1SDimitry Andric class MachineLoopAnalysis : public AnalysisInfoMixin<MachineLoopAnalysis> {
137*0fca6ea1SDimitry Andric   friend AnalysisInfoMixin<MachineLoopAnalysis>;
138*0fca6ea1SDimitry Andric   static AnalysisKey Key;
139*0fca6ea1SDimitry Andric 
140*0fca6ea1SDimitry Andric public:
141*0fca6ea1SDimitry Andric   using Result = MachineLoopInfo;
142*0fca6ea1SDimitry Andric   Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
143*0fca6ea1SDimitry Andric };
144*0fca6ea1SDimitry Andric 
145*0fca6ea1SDimitry Andric /// Printer pass for the \c LoopAnalysis results.
146*0fca6ea1SDimitry Andric class MachineLoopPrinterPass : public PassInfoMixin<MachineLoopPrinterPass> {
147*0fca6ea1SDimitry Andric   raw_ostream &OS;
148*0fca6ea1SDimitry Andric 
149*0fca6ea1SDimitry Andric public:
150*0fca6ea1SDimitry Andric   explicit MachineLoopPrinterPass(raw_ostream &OS) : OS(OS) {}
151*0fca6ea1SDimitry Andric   PreservedAnalyses run(MachineFunction &MF,
152*0fca6ea1SDimitry Andric                         MachineFunctionAnalysisManager &MFAM);
153*0fca6ea1SDimitry Andric   static bool isRequired() { return true; }
154*0fca6ea1SDimitry Andric };
155*0fca6ea1SDimitry Andric 
156*0fca6ea1SDimitry Andric class MachineLoopInfoWrapperPass : public MachineFunctionPass {
157*0fca6ea1SDimitry Andric   MachineLoopInfo LI;
158*0fca6ea1SDimitry Andric 
159*0fca6ea1SDimitry Andric public:
160*0fca6ea1SDimitry Andric   static char ID; // Pass identification, replacement for typeid
161*0fca6ea1SDimitry Andric 
162*0fca6ea1SDimitry Andric   MachineLoopInfoWrapperPass();
163*0fca6ea1SDimitry Andric 
164*0fca6ea1SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   void releaseMemory() override { LI.releaseMemory(); }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
1690b57cec5SDimitry Andric 
170*0fca6ea1SDimitry Andric   MachineLoopInfo &getLI() { return LI; }
1710b57cec5SDimitry Andric };
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric // Allow clients to walk the list of nested loops...
1740b57cec5SDimitry Andric template <> struct GraphTraits<const MachineLoop*> {
1750b57cec5SDimitry Andric   using NodeRef = const MachineLoop *;
1760b57cec5SDimitry Andric   using ChildIteratorType = MachineLoopInfo::iterator;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   static NodeRef getEntryNode(const MachineLoop *L) { return L; }
1790b57cec5SDimitry Andric   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1800b57cec5SDimitry Andric   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1810b57cec5SDimitry Andric };
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric template <> struct GraphTraits<MachineLoop*> {
1840b57cec5SDimitry Andric   using NodeRef = MachineLoop *;
1850b57cec5SDimitry Andric   using ChildIteratorType = MachineLoopInfo::iterator;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   static NodeRef getEntryNode(MachineLoop *L) { return L; }
1880b57cec5SDimitry Andric   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1890b57cec5SDimitry Andric   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1900b57cec5SDimitry Andric };
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric } // end namespace llvm
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric #endif // LLVM_CODEGEN_MACHINELOOPINFO_H
195