LLVM 20.0.0git
MachineLoopInfo.h
Go to the documentation of this file.
1//===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the MachineLoopInfo class that is used to identify natural
10// loops and determine the loop depth of various nodes of the CFG. Note that
11// natural loops may actually be several loops that share the same header node.
12//
13// This analysis calculates the nesting structure of loops in a function. For
14// each natural loop identified, this analysis identifies natural loops
15// contained entirely within the loop and the basic blocks the make up the loop.
16//
17// It can calculate on the fly various bits of information, for example:
18//
19// * whether there is a preheader for the loop
20// * the number of back edges to the header
21// * whether or not a particular block branches out of the loop
22// * the successor blocks of the loop
23// * the loop depth
24// * the trip count
25// * etc...
26//
27//===----------------------------------------------------------------------===//
28
29#ifndef LLVM_CODEGEN_MACHINELOOPINFO_H
30#define LLVM_CODEGEN_MACHINELOOPINFO_H
31
35#include "llvm/IR/CFG.h"
36#include "llvm/IR/DebugLoc.h"
38
39namespace llvm {
40
41class MachineDominatorTree;
42// Implementation in LoopInfoImpl.h
43class MachineLoop;
44extern template class LoopBase<MachineBasicBlock, MachineLoop>;
45
47public:
48 /// Return the "top" block in the loop, which is the first block in the linear
49 /// layout, ignoring any parts of the loop not contiguous with the part that
50 /// contains the header.
51 MachineBasicBlock *getTopBlock();
52
53 /// Return the "bottom" block in the loop, which is the last block in the
54 /// linear layout, ignoring any parts of the loop not contiguous with the part
55 /// that contains the header.
56 MachineBasicBlock *getBottomBlock();
57
58 /// Find the block that contains the loop control variable and the
59 /// loop test. This will return the latch block if it's one of the exiting
60 /// blocks. Otherwise, return the exiting block. Return 'null' when
61 /// multiple exiting blocks are present.
62 MachineBasicBlock *findLoopControlBlock() const;
63
64 /// Return the debug location of the start of this loop.
65 /// This looks for a BB terminating instruction with a known debug
66 /// location by looking at the preheader and header blocks. If it
67 /// cannot find a terminating instruction with location information,
68 /// it returns an unknown location.
69 DebugLoc getStartLoc() const;
70
71 /// Find the llvm.loop metadata for this loop.
72 /// If each branch to the header of this loop contains the same llvm.loop
73 /// metadata, then this metadata node is returned. Otherwise, if any
74 /// latch instruction does not contain the llvm.loop metadata or
75 /// multiple latch instructions contain different llvm.loop metadata nodes,
76 /// then null is returned.
77 MDNode *getLoopID() const;
78
79 /// Returns true if the instruction is loop invariant.
80 /// I.e., all virtual register operands are defined outside of the loop,
81 /// physical registers aren't accessed explicitly, and there are no side
82 /// effects that aren't captured by the operands or other flags.
83 /// ExcludeReg can be used to exclude the given register from the check
84 /// i.e. when we're considering hoisting it's definition but not hoisted it
85 /// yet
86 bool isLoopInvariant(MachineInstr &I, const Register ExcludeReg = 0) const;
87
88 void dump() const;
89
90private:
92
93 /// Returns true if the given physreg has no defs inside the loop.
94 bool isLoopInvariantImplicitPhysReg(Register Reg) const;
95
98
99 MachineLoop() = default;
100};
101
102// Implementation in LoopInfoImpl.h
104
108
109public:
110 MachineLoopInfo() = default;
111 explicit MachineLoopInfo(MachineDominatorTree &MDT) { calculate(MDT); }
115
116 /// Handle invalidation explicitly.
117 bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
119
120 /// Find the block that either is the loop preheader, or could
121 /// speculatively be used as the preheader. This is e.g. useful to place
122 /// loop setup code. Code that cannot be speculated should not be placed
123 /// here. SpeculativePreheader is controlling whether it also tries to
124 /// find the speculative preheader if the regular preheader is not present.
125 /// With FindMultiLoopPreheader = false, nullptr will be returned if the found
126 /// preheader is the preheader of multiple loops.
128 findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false,
129 bool FindMultiLoopPreheader = false) const;
130
131 /// Calculate the natural loop information.
132 void calculate(MachineDominatorTree &MDT);
133};
134
135/// Analysis pass that exposes the \c MachineLoopInfo for a machine function.
138 static AnalysisKey Key;
139
140public:
143};
144
145/// Printer pass for the \c LoopAnalysis results.
148
149public:
153 static bool isRequired() { return true; }
154};
155
158
159public:
160 static char ID; // Pass identification, replacement for typeid
161
163
164 bool runOnMachineFunction(MachineFunction &F) override;
165
166 void releaseMemory() override { LI.releaseMemory(); }
167
168 void getAnalysisUsage(AnalysisUsage &AU) const override;
169
170 MachineLoopInfo &getLI() { return LI; }
171};
172
173// Allow clients to walk the list of nested loops...
175 using NodeRef = const MachineLoop *;
177
178 static NodeRef getEntryNode(const MachineLoop *L) { return L; }
179 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
180 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
181};
182
183template <> struct GraphTraits<MachineLoop*> {
186
187 static NodeRef getEntryNode(MachineLoop *L) { return L; }
188 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
189 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
190};
191
192} // end namespace llvm
193
194#endif // LLVM_CODEGEN_MACHINELOOPINFO_H
MachineBasicBlock & MBB
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned Reg
raw_pwrite_stream & OS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
A debug info location.
Definition: DebugLoc.h:33
Instances of this class are used to represent loops that are detected in the flow graph.
This class builds and contains all of the top-level loop structures in the specified function.
std::vector< MachineLoop * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Metadata node.
Definition: Metadata.h:1069
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Representation of each machine instruction.
Definition: MachineInstr.h:69
Analysis pass that exposes the MachineLoopInfo for a machine function.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
MachineLoopInfo(MachineLoopInfo &&)=default
MachineLoopInfo(const MachineLoopInfo &)=delete
friend class MachineLoopInfoWrapperPass
MachineLoopInfo & operator=(const MachineLoopInfo &)=delete
MachineLoopInfo(MachineDominatorTree &MDT)
Printer pass for the LoopAnalysis results.
MachineLoopPrinterPass(raw_ostream &OS)
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
MachineLoopInfo::iterator ChildIteratorType
static ChildIteratorType child_end(NodeRef N)
static NodeRef getEntryNode(MachineLoop *L)
static ChildIteratorType child_begin(NodeRef N)
MachineLoopInfo::iterator ChildIteratorType
static ChildIteratorType child_begin(NodeRef N)
static NodeRef getEntryNode(const MachineLoop *L)
static ChildIteratorType child_end(NodeRef N)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69