LLVM 22.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"
39
40namespace llvm {
41
42class MachineDominatorTree;
43// Implementation in LoopInfoImpl.h
44class MachineLoop;
45extern template class LLVM_TEMPLATE_ABI
47
49public:
50 /// Return the "top" block in the loop, which is the first block in the linear
51 /// layout, ignoring any parts of the loop not contiguous with the part that
52 /// contains the header.
53 LLVM_ABI MachineBasicBlock *getTopBlock();
54
55 /// Return the "bottom" block in the loop, which is the last block in the
56 /// linear layout, ignoring any parts of the loop not contiguous with the part
57 /// that contains the header.
58 LLVM_ABI MachineBasicBlock *getBottomBlock();
59
60 /// Find the block that contains the loop control variable and the
61 /// loop test. This will return the latch block if it's one of the exiting
62 /// blocks. Otherwise, return the exiting block. Return 'null' when
63 /// multiple exiting blocks are present.
64 LLVM_ABI MachineBasicBlock *findLoopControlBlock() const;
65
66 /// Return the debug location of the start of this loop.
67 /// This looks for a BB terminating instruction with a known debug
68 /// location by looking at the preheader and header blocks. If it
69 /// cannot find a terminating instruction with location information,
70 /// it returns an unknown location.
71 LLVM_ABI DebugLoc getStartLoc() const;
72
73 /// Find the llvm.loop metadata for this loop.
74 /// If each branch to the header of this loop contains the same llvm.loop
75 /// metadata, then this metadata node is returned. Otherwise, if any
76 /// latch instruction does not contain the llvm.loop metadata or
77 /// multiple latch instructions contain different llvm.loop metadata nodes,
78 /// then null is returned.
79 LLVM_ABI MDNode *getLoopID() const;
80
81 /// Returns true if the instruction is loop invariant.
82 /// I.e., all virtual register operands are defined outside of the loop,
83 /// physical registers aren't accessed explicitly, and there are no side
84 /// effects that aren't captured by the operands or other flags.
85 /// ExcludeReg can be used to exclude the given register from the check
86 /// i.e. when we're considering hoisting it's definition but not hoisted it
87 /// yet
88 LLVM_ABI bool isLoopInvariant(MachineInstr &I,
89 const Register ExcludeReg = 0) const;
90
91 LLVM_ABI void dump() const;
92
93private:
95
96 /// Returns true if the given physreg has no defs inside the loop.
97 bool isLoopInvariantImplicitPhysReg(Register Reg) const;
98
101
102 MachineLoop() = default;
103};
104
105// Implementation in LoopInfoImpl.h
106extern template class LLVM_TEMPLATE_ABI
108
112
113public:
114 MachineLoopInfo() = default;
115 explicit MachineLoopInfo(MachineDominatorTree &MDT) { calculate(MDT); }
119
120 /// Handle invalidation explicitly.
121 LLVM_ABI bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
123
124 /// Find the block that either is the loop preheader, or could
125 /// speculatively be used as the preheader. This is e.g. useful to place
126 /// loop setup code. Code that cannot be speculated should not be placed
127 /// here. SpeculativePreheader is controlling whether it also tries to
128 /// find the speculative preheader if the regular preheader is not present.
129 /// With FindMultiLoopPreheader = false, nullptr will be returned if the found
130 /// preheader is the preheader of multiple loops.
132 findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false,
133 bool FindMultiLoopPreheader = false) const;
134
135 /// Calculate the natural loop information.
136 LLVM_ABI void calculate(MachineDominatorTree &MDT);
137};
138
139/// Analysis pass that exposes the \c MachineLoopInfo for a machine function.
142 LLVM_ABI static AnalysisKey Key;
143
144public:
148};
149
150/// Printer pass for the \c LoopAnalysis results.
153
154public:
158 static bool isRequired() { return true; }
159};
160
163
164public:
165 static char ID; // Pass identification, replacement for typeid
166
168
169 bool runOnMachineFunction(MachineFunction &F) override;
170
171 void releaseMemory() override { LI.releaseMemory(); }
172
173 void getAnalysisUsage(AnalysisUsage &AU) const override;
174
175 MachineLoopInfo &getLI() { return LI; }
176};
177
178// Allow clients to walk the list of nested loops...
180 using NodeRef = const MachineLoop *;
182
183 static NodeRef getEntryNode(const MachineLoop *L) { return L; }
184 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
185 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
186};
187
188template <> struct GraphTraits<MachineLoop*> {
191
192 static NodeRef getEntryNode(MachineLoop *L) { return L; }
193 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
194 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
195};
196
197} // end namespace llvm
198
199#endif // LLVM_CODEGEN_MACHINELOOPINFO_H
MachineBasicBlock & MBB
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_TEMPLATE_ABI
Definition: Compiler.h:214
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
Register Reg
raw_pwrite_stream & OS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
A debug info location.
Definition: DebugLoc.h:124
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:1077
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:72
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:112
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:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
template class LLVM_TEMPLATE_ABI LoopBase< MachineBasicBlock, MachineLoop >
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
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:70