Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the MachineLoopInfo class that is used to identify natural
0010 // loops and determine the loop depth of various nodes of the CFG.  Note that
0011 // natural loops may actually be several loops that share the same header node.
0012 //
0013 // This analysis calculates the nesting structure of loops in a function.  For
0014 // each natural loop identified, this analysis identifies natural loops
0015 // contained entirely within the loop and the basic blocks the make up the loop.
0016 //
0017 // It can calculate on the fly various bits of information, for example:
0018 //
0019 //  * whether there is a preheader for the loop
0020 //  * the number of back edges to the header
0021 //  * whether or not a particular block branches out of the loop
0022 //  * the successor blocks of the loop
0023 //  * the loop depth
0024 //  * the trip count
0025 //  * etc...
0026 //
0027 //===----------------------------------------------------------------------===//
0028 
0029 #ifndef LLVM_CODEGEN_MACHINELOOPINFO_H
0030 #define LLVM_CODEGEN_MACHINELOOPINFO_H
0031 
0032 #include "llvm/CodeGen/MachineBasicBlock.h"
0033 #include "llvm/CodeGen/MachineFunctionPass.h"
0034 #include "llvm/CodeGen/MachinePassManager.h"
0035 #include "llvm/IR/CFG.h"
0036 #include "llvm/IR/DebugLoc.h"
0037 #include "llvm/Support/GenericLoopInfo.h"
0038 
0039 namespace llvm {
0040 
0041 class MachineDominatorTree;
0042 // Implementation in LoopInfoImpl.h
0043 class MachineLoop;
0044 extern template class LoopBase<MachineBasicBlock, MachineLoop>;
0045 
0046 class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
0047 public:
0048   /// Return the "top" block in the loop, which is the first block in the linear
0049   /// layout, ignoring any parts of the loop not contiguous with the part that
0050   /// contains the header.
0051   MachineBasicBlock *getTopBlock();
0052 
0053   /// Return the "bottom" block in the loop, which is the last block in the
0054   /// linear layout, ignoring any parts of the loop not contiguous with the part
0055   /// that contains the header.
0056   MachineBasicBlock *getBottomBlock();
0057 
0058   /// Find the block that contains the loop control variable and the
0059   /// loop test. This will return the latch block if it's one of the exiting
0060   /// blocks. Otherwise, return the exiting block. Return 'null' when
0061   /// multiple exiting blocks are present.
0062   MachineBasicBlock *findLoopControlBlock() const;
0063 
0064   /// Return the debug location of the start of this loop.
0065   /// This looks for a BB terminating instruction with a known debug
0066   /// location by looking at the preheader and header blocks. If it
0067   /// cannot find a terminating instruction with location information,
0068   /// it returns an unknown location.
0069   DebugLoc getStartLoc() const;
0070 
0071   /// Find the llvm.loop metadata for this loop.
0072   /// If each branch to the header of this loop contains the same llvm.loop
0073   /// metadata, then this metadata node is returned. Otherwise, if any
0074   /// latch instruction does not contain the llvm.loop metadata or
0075   /// multiple latch instructions contain different llvm.loop metadata nodes,
0076   /// then null is returned.
0077   MDNode *getLoopID() const;
0078 
0079   /// Returns true if the instruction is loop invariant.
0080   /// I.e., all virtual register operands are defined outside of the loop,
0081   /// physical registers aren't accessed explicitly, and there are no side
0082   /// effects that aren't captured by the operands or other flags.
0083   /// ExcludeReg can be used to exclude the given register from the check
0084   /// i.e. when we're considering hoisting it's definition but not hoisted it
0085   /// yet
0086   bool isLoopInvariant(MachineInstr &I, const Register ExcludeReg = 0) const;
0087 
0088   void dump() const;
0089 
0090 private:
0091   friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
0092 
0093   /// Returns true if the given physreg has no defs inside the loop.
0094   bool isLoopInvariantImplicitPhysReg(Register Reg) const;
0095 
0096   explicit MachineLoop(MachineBasicBlock *MBB)
0097     : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
0098 
0099   MachineLoop() = default;
0100 };
0101 
0102 // Implementation in LoopInfoImpl.h
0103 extern template class LoopInfoBase<MachineBasicBlock, MachineLoop>;
0104 
0105 class MachineLoopInfo : public LoopInfoBase<MachineBasicBlock, MachineLoop> {
0106   friend class LoopBase<MachineBasicBlock, MachineLoop>;
0107   friend class MachineLoopInfoWrapperPass;
0108 
0109 public:
0110   MachineLoopInfo() = default;
0111   explicit MachineLoopInfo(MachineDominatorTree &MDT) { calculate(MDT); }
0112   MachineLoopInfo(MachineLoopInfo &&) = default;
0113   MachineLoopInfo(const MachineLoopInfo &) = delete;
0114   MachineLoopInfo &operator=(const MachineLoopInfo &) = delete;
0115 
0116   /// Handle invalidation explicitly.
0117   bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
0118                   MachineFunctionAnalysisManager::Invalidator &);
0119 
0120   /// Find the block that either is the loop preheader, or could
0121   /// speculatively be used as the preheader. This is e.g. useful to place
0122   /// loop setup code. Code that cannot be speculated should not be placed
0123   /// here. SpeculativePreheader is controlling whether it also tries to
0124   /// find the speculative preheader if the regular preheader is not present.
0125   /// With FindMultiLoopPreheader = false, nullptr will be returned if the found
0126   /// preheader is the preheader of multiple loops.
0127   MachineBasicBlock *
0128   findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false,
0129                     bool FindMultiLoopPreheader = false) const;
0130 
0131   /// Calculate the natural loop information.
0132   void calculate(MachineDominatorTree &MDT);
0133 };
0134 
0135 /// Analysis pass that exposes the \c MachineLoopInfo for a machine function.
0136 class MachineLoopAnalysis : public AnalysisInfoMixin<MachineLoopAnalysis> {
0137   friend AnalysisInfoMixin<MachineLoopAnalysis>;
0138   static AnalysisKey Key;
0139 
0140 public:
0141   using Result = MachineLoopInfo;
0142   Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
0143 };
0144 
0145 /// Printer pass for the \c LoopAnalysis results.
0146 class MachineLoopPrinterPass : public PassInfoMixin<MachineLoopPrinterPass> {
0147   raw_ostream &OS;
0148 
0149 public:
0150   explicit MachineLoopPrinterPass(raw_ostream &OS) : OS(OS) {}
0151   PreservedAnalyses run(MachineFunction &MF,
0152                         MachineFunctionAnalysisManager &MFAM);
0153   static bool isRequired() { return true; }
0154 };
0155 
0156 class MachineLoopInfoWrapperPass : public MachineFunctionPass {
0157   MachineLoopInfo LI;
0158 
0159 public:
0160   static char ID; // Pass identification, replacement for typeid
0161 
0162   MachineLoopInfoWrapperPass();
0163 
0164   bool runOnMachineFunction(MachineFunction &F) override;
0165 
0166   void releaseMemory() override { LI.releaseMemory(); }
0167 
0168   void getAnalysisUsage(AnalysisUsage &AU) const override;
0169 
0170   MachineLoopInfo &getLI() { return LI; }
0171 };
0172 
0173 // Allow clients to walk the list of nested loops...
0174 template <> struct GraphTraits<const MachineLoop*> {
0175   using NodeRef = const MachineLoop *;
0176   using ChildIteratorType = MachineLoopInfo::iterator;
0177 
0178   static NodeRef getEntryNode(const MachineLoop *L) { return L; }
0179   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
0180   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
0181 };
0182 
0183 template <> struct GraphTraits<MachineLoop*> {
0184   using NodeRef = MachineLoop *;
0185   using ChildIteratorType = MachineLoopInfo::iterator;
0186 
0187   static NodeRef getEntryNode(MachineLoop *L) { return L; }
0188   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
0189   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
0190 };
0191 
0192 } // end namespace llvm
0193 
0194 #endif // LLVM_CODEGEN_MACHINELOOPINFO_H