109467b48Spatrick //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick // 909467b48Spatrick // This file defines the MachineLoopInfo class that is used to identify natural 1009467b48Spatrick // loops and determine the loop depth of various nodes of the CFG. Note that 1109467b48Spatrick // natural loops may actually be several loops that share the same header node. 1209467b48Spatrick // 1309467b48Spatrick // This analysis calculates the nesting structure of loops in a function. For 1409467b48Spatrick // each natural loop identified, this analysis identifies natural loops 1509467b48Spatrick // contained entirely within the loop and the basic blocks the make up the loop. 1609467b48Spatrick // 1709467b48Spatrick // It can calculate on the fly various bits of information, for example: 1809467b48Spatrick // 1909467b48Spatrick // * whether there is a preheader for the loop 2009467b48Spatrick // * the number of back edges to the header 2109467b48Spatrick // * whether or not a particular block branches out of the loop 2209467b48Spatrick // * the successor blocks of the loop 2309467b48Spatrick // * the loop depth 2409467b48Spatrick // * the trip count 2509467b48Spatrick // * etc... 2609467b48Spatrick // 2709467b48Spatrick //===----------------------------------------------------------------------===// 2809467b48Spatrick 2909467b48Spatrick #ifndef LLVM_CODEGEN_MACHINELOOPINFO_H 3009467b48Spatrick #define LLVM_CODEGEN_MACHINELOOPINFO_H 3109467b48Spatrick 3209467b48Spatrick #include "llvm/Analysis/LoopInfo.h" 3309467b48Spatrick #include "llvm/CodeGen/MachineBasicBlock.h" 3409467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h" 3509467b48Spatrick #include "llvm/IR/DebugLoc.h" 3609467b48Spatrick 3709467b48Spatrick namespace llvm { 3809467b48Spatrick 3909467b48Spatrick class MachineDominatorTree; 4009467b48Spatrick // Implementation in LoopInfoImpl.h 4109467b48Spatrick class MachineLoop; 4209467b48Spatrick extern template class LoopBase<MachineBasicBlock, MachineLoop>; 4309467b48Spatrick 4409467b48Spatrick class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> { 4509467b48Spatrick public: 4609467b48Spatrick /// Return the "top" block in the loop, which is the first block in the linear 4709467b48Spatrick /// layout, ignoring any parts of the loop not contiguous with the part that 4809467b48Spatrick /// contains the header. 4909467b48Spatrick MachineBasicBlock *getTopBlock(); 5009467b48Spatrick 5109467b48Spatrick /// Return the "bottom" block in the loop, which is the last block in the 5209467b48Spatrick /// linear layout, ignoring any parts of the loop not contiguous with the part 5309467b48Spatrick /// that contains the header. 5409467b48Spatrick MachineBasicBlock *getBottomBlock(); 5509467b48Spatrick 5609467b48Spatrick /// Find the block that contains the loop control variable and the 5709467b48Spatrick /// loop test. This will return the latch block if it's one of the exiting 5809467b48Spatrick /// blocks. Otherwise, return the exiting block. Return 'null' when 5909467b48Spatrick /// multiple exiting blocks are present. 6009467b48Spatrick MachineBasicBlock *findLoopControlBlock(); 6109467b48Spatrick 6209467b48Spatrick /// Return the debug location of the start of this loop. 6309467b48Spatrick /// This looks for a BB terminating instruction with a known debug 6409467b48Spatrick /// location by looking at the preheader and header blocks. If it 6509467b48Spatrick /// cannot find a terminating instruction with location information, 6609467b48Spatrick /// it returns an unknown location. 6709467b48Spatrick DebugLoc getStartLoc() const; 6809467b48Spatrick 69*73471bf0Spatrick /// Returns true if the instruction is loop invariant. 70*73471bf0Spatrick /// I.e., all virtual register operands are defined outside of the loop, 71*73471bf0Spatrick /// physical registers aren't accessed explicitly, and there are no side 72*73471bf0Spatrick /// effects that aren't captured by the operands or other flags. 73*73471bf0Spatrick bool isLoopInvariant(MachineInstr &I) const; 74*73471bf0Spatrick 7509467b48Spatrick void dump() const; 7609467b48Spatrick 7709467b48Spatrick private: 7809467b48Spatrick friend class LoopInfoBase<MachineBasicBlock, MachineLoop>; 7909467b48Spatrick MachineLoop(MachineBasicBlock * MBB)8009467b48Spatrick explicit MachineLoop(MachineBasicBlock *MBB) 8109467b48Spatrick : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {} 8209467b48Spatrick 8309467b48Spatrick MachineLoop() = default; 8409467b48Spatrick }; 8509467b48Spatrick 8609467b48Spatrick // Implementation in LoopInfoImpl.h 8709467b48Spatrick extern template class LoopInfoBase<MachineBasicBlock, MachineLoop>; 8809467b48Spatrick 8909467b48Spatrick class MachineLoopInfo : public MachineFunctionPass { 9009467b48Spatrick friend class LoopBase<MachineBasicBlock, MachineLoop>; 9109467b48Spatrick 9209467b48Spatrick LoopInfoBase<MachineBasicBlock, MachineLoop> LI; 9309467b48Spatrick 9409467b48Spatrick public: 9509467b48Spatrick static char ID; // Pass identification, replacement for typeid 9609467b48Spatrick 9709467b48Spatrick MachineLoopInfo(); MachineLoopInfo(MachineDominatorTree & MDT)9809467b48Spatrick explicit MachineLoopInfo(MachineDominatorTree &MDT) 9909467b48Spatrick : MachineFunctionPass(ID) { 10009467b48Spatrick calculate(MDT); 10109467b48Spatrick } 10209467b48Spatrick MachineLoopInfo(const MachineLoopInfo &) = delete; 10309467b48Spatrick MachineLoopInfo &operator=(const MachineLoopInfo &) = delete; 10409467b48Spatrick getBase()10509467b48Spatrick LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; } 10609467b48Spatrick 10709467b48Spatrick /// Find the block that either is the loop preheader, or could 10809467b48Spatrick /// speculatively be used as the preheader. This is e.g. useful to place 10909467b48Spatrick /// loop setup code. Code that cannot be speculated should not be placed 11009467b48Spatrick /// here. SpeculativePreheader is controlling whether it also tries to 11109467b48Spatrick /// find the speculative preheader if the regular preheader is not present. 112*73471bf0Spatrick /// With FindMultiLoopPreheader = false, nullptr will be returned if the found 113*73471bf0Spatrick /// preheader is the preheader of multiple loops. 114*73471bf0Spatrick MachineBasicBlock * 115*73471bf0Spatrick findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false, 116*73471bf0Spatrick bool FindMultiLoopPreheader = false) const; 11709467b48Spatrick 11809467b48Spatrick /// The iterator interface to the top-level loops in the current function. 11909467b48Spatrick using iterator = LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator; begin()12009467b48Spatrick inline iterator begin() const { return LI.begin(); } end()12109467b48Spatrick inline iterator end() const { return LI.end(); } empty()12209467b48Spatrick bool empty() const { return LI.empty(); } 12309467b48Spatrick 12409467b48Spatrick /// Return the innermost loop that BB lives in. If a basic block is in no loop 12509467b48Spatrick /// (for example the entry node), null is returned. getLoopFor(const MachineBasicBlock * BB)12609467b48Spatrick inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const { 12709467b48Spatrick return LI.getLoopFor(BB); 12809467b48Spatrick } 12909467b48Spatrick 13009467b48Spatrick /// Same as getLoopFor. 13109467b48Spatrick inline const MachineLoop *operator[](const MachineBasicBlock *BB) const { 13209467b48Spatrick return LI.getLoopFor(BB); 13309467b48Spatrick } 13409467b48Spatrick 13509467b48Spatrick /// Return the loop nesting level of the specified block. getLoopDepth(const MachineBasicBlock * BB)13609467b48Spatrick inline unsigned getLoopDepth(const MachineBasicBlock *BB) const { 13709467b48Spatrick return LI.getLoopDepth(BB); 13809467b48Spatrick } 13909467b48Spatrick 14009467b48Spatrick /// True if the block is a loop header node. isLoopHeader(const MachineBasicBlock * BB)14109467b48Spatrick inline bool isLoopHeader(const MachineBasicBlock *BB) const { 14209467b48Spatrick return LI.isLoopHeader(BB); 14309467b48Spatrick } 14409467b48Spatrick 14509467b48Spatrick /// Calculate the natural loop information. 14609467b48Spatrick bool runOnMachineFunction(MachineFunction &F) override; 14709467b48Spatrick void calculate(MachineDominatorTree &MDT); 14809467b48Spatrick releaseMemory()14909467b48Spatrick void releaseMemory() override { LI.releaseMemory(); } 15009467b48Spatrick 15109467b48Spatrick void getAnalysisUsage(AnalysisUsage &AU) const override; 15209467b48Spatrick 15309467b48Spatrick /// This removes the specified top-level loop from this loop info object. The 15409467b48Spatrick /// loop is not deleted, as it will presumably be inserted into another loop. removeLoop(iterator I)15509467b48Spatrick inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); } 15609467b48Spatrick 15709467b48Spatrick /// Change the top-level loop that contains BB to the specified loop. This 15809467b48Spatrick /// should be used by transformations that restructure the loop hierarchy 15909467b48Spatrick /// tree. changeLoopFor(MachineBasicBlock * BB,MachineLoop * L)16009467b48Spatrick inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) { 16109467b48Spatrick LI.changeLoopFor(BB, L); 16209467b48Spatrick } 16309467b48Spatrick 16409467b48Spatrick /// Replace the specified loop in the top-level loops list with the indicated 16509467b48Spatrick /// loop. changeTopLevelLoop(MachineLoop * OldLoop,MachineLoop * NewLoop)16609467b48Spatrick inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) { 16709467b48Spatrick LI.changeTopLevelLoop(OldLoop, NewLoop); 16809467b48Spatrick } 16909467b48Spatrick 17009467b48Spatrick /// This adds the specified loop to the collection of top-level loops. addTopLevelLoop(MachineLoop * New)17109467b48Spatrick inline void addTopLevelLoop(MachineLoop *New) { 17209467b48Spatrick LI.addTopLevelLoop(New); 17309467b48Spatrick } 17409467b48Spatrick 17509467b48Spatrick /// This method completely removes BB from all data structures, including all 17609467b48Spatrick /// of the Loop objects it is nested in and our mapping from 17709467b48Spatrick /// MachineBasicBlocks to loops. removeBlock(MachineBasicBlock * BB)17809467b48Spatrick void removeBlock(MachineBasicBlock *BB) { 17909467b48Spatrick LI.removeBlock(BB); 18009467b48Spatrick } 18109467b48Spatrick }; 18209467b48Spatrick 18309467b48Spatrick // Allow clients to walk the list of nested loops... 18409467b48Spatrick template <> struct GraphTraits<const MachineLoop*> { 18509467b48Spatrick using NodeRef = const MachineLoop *; 18609467b48Spatrick using ChildIteratorType = MachineLoopInfo::iterator; 18709467b48Spatrick 18809467b48Spatrick static NodeRef getEntryNode(const MachineLoop *L) { return L; } 18909467b48Spatrick static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } 19009467b48Spatrick static ChildIteratorType child_end(NodeRef N) { return N->end(); } 19109467b48Spatrick }; 19209467b48Spatrick 19309467b48Spatrick template <> struct GraphTraits<MachineLoop*> { 19409467b48Spatrick using NodeRef = MachineLoop *; 19509467b48Spatrick using ChildIteratorType = MachineLoopInfo::iterator; 19609467b48Spatrick 19709467b48Spatrick static NodeRef getEntryNode(MachineLoop *L) { return L; } 19809467b48Spatrick static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } 19909467b48Spatrick static ChildIteratorType child_end(NodeRef N) { return N->end(); } 20009467b48Spatrick }; 20109467b48Spatrick 20209467b48Spatrick } // end namespace llvm 20309467b48Spatrick 20409467b48Spatrick #endif // LLVM_CODEGEN_MACHINELOOPINFO_H 205