LLVM 23.0.0git
IVUsers.h
Go to the documentation of this file.
1//===- llvm/Analysis/IVUsers.h - Induction Variable Users -------*- 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 implements bookkeeping for "interesting" users of expressions
10// computed from induction variables.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_IVUSERS_H
15#define LLVM_ANALYSIS_IVUSERS_H
16
20#include "llvm/IR/Instruction.h"
21#include "llvm/IR/ValueHandle.h"
22
23namespace llvm {
24
25class AssumptionCache;
26class DominatorTree;
27class ScalarEvolution;
28class SCEV;
29class IVUsers;
30
31/// IVStrideUse - Keep track of one use of a strided induction variable.
32/// The Expr member keeps track of the expression, User is the actual user
33/// instruction of the operand, and 'OperandValToReplace' is the operand of
34/// the User that is the use.
35class LLVM_ABI IVStrideUse final : public CallbackVH,
36 public ilist_node<IVStrideUse> {
37 friend class IVUsers;
38public:
40 : CallbackVH(U), Parent(P), OperandValToReplace(O) {
41 }
42
43 /// getUser - Return the user instruction for this use.
46 }
47
48 /// setUser - Assign a new user instruction for this use.
49 void setUser(Instruction *NewUser) {
50 setValPtr(NewUser);
51 }
52
53 /// getOperandValToReplace - Return the Value of the operand in the user
54 /// instruction that this IVStrideUse is representing.
56 return OperandValToReplace;
57 }
58
59 /// setOperandValToReplace - Assign a new Value as the operand value
60 /// to replace.
62 OperandValToReplace = Op;
63 }
64
65 /// getPostIncLoops - Return the set of loops for which the expression has
66 /// been adjusted to use post-inc mode.
68 return PostIncLoops;
69 }
70
71 /// transformToPostInc - Transform the expression to post-inc form for the
72 /// given loop.
73 void transformToPostInc(const Loop *L);
74
75private:
76 /// Parent - a pointer to the IVUsers that owns this IVStrideUse.
77 IVUsers *Parent;
78
79 /// OperandValToReplace - The Value of the operand in the user instruction
80 /// that this IVStrideUse is representing.
81 WeakTrackingVH OperandValToReplace;
82
83 /// PostIncLoops - The set of loops for which Expr has been adjusted to
84 /// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
85 PostIncLoopSet PostIncLoops;
86
87 /// Deleted - Implementation of CallbackVH virtual function to
88 /// receive notification when the User is deleted.
89 void deleted() override;
90};
91
92class IVUsers {
93 friend class IVStrideUse;
94 Loop *L;
96 LoopInfo *LI;
97 DominatorTree *DT;
100
101 /// IVUses - A list of all tracked IV uses of induction variable expressions
102 /// we are interested in.
103 ilist<IVStrideUse> IVUses;
104
105 // Ephemeral values used by @llvm.assume in this function.
107
108public:
111
113 : L(std::move(X.L)), AC(std::move(X.AC)), DT(std::move(X.DT)),
114 SE(std::move(X.SE)), Processed(std::move(X.Processed)),
115 IVUses(std::move(X.IVUses)), EphValues(std::move(X.EphValues)) {
116 for (IVStrideUse &U : IVUses)
117 U.Parent = this;
118 }
119 IVUsers(const IVUsers &) = delete;
120 IVUsers &operator=(IVUsers &&) = delete;
121 IVUsers &operator=(const IVUsers &) = delete;
122
123 Loop *getLoop() const { return L; }
124
125 /// AddUsersIfInteresting - Inspect the specified Instruction. If it is a
126 /// reducible SCEV, recursively add its users to the IVUsesByStride set and
127 /// return true. Otherwise, return false.
129
131
132 /// getReplacementExpr - Return a SCEV expression which computes the
133 /// value of the OperandValToReplace of the given IVStrideUse.
134 LLVM_ABI const SCEV *getReplacementExpr(const IVStrideUse &IU) const;
135
136 /// getExpr - Return the expression for the use. Returns nullptr if the result
137 /// is not invertible.
138 LLVM_ABI const SCEV *getExpr(const IVStrideUse &IU) const;
139
140 LLVM_ABI const SCEV *getStride(const IVStrideUse &IU, const Loop *L) const;
141
144 iterator begin() { return IVUses.begin(); }
145 iterator end() { return IVUses.end(); }
146 const_iterator begin() const { return IVUses.begin(); }
147 const_iterator end() const { return IVUses.end(); }
148 bool empty() const { return IVUses.empty(); }
149
150 bool isIVUserOrOperand(Instruction *Inst) const {
151 return Processed.count(Inst);
152 }
153
154 bool isEphemeral(const Value *V) const { return EphValues.count(V); }
155
156 LLVM_ABI void releaseMemory();
157
158 LLVM_ABI void print(raw_ostream &OS, const Module * = nullptr) const;
159
160 /// dump - This method is used for debugging.
161 LLVM_ABI void dump() const;
162};
163
165
167 std::unique_ptr<IVUsers> IU;
168
169public:
170 static char ID;
171
173
174 IVUsers &getIU() { return *IU; }
175 const IVUsers &getIU() const { return *IU; }
176
177 void getAnalysisUsage(AnalysisUsage &AU) const override;
178
179 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
180
181 void releaseMemory() override;
182
183 void print(raw_ostream &OS, const Module * = nullptr) const override;
184};
185
186/// Analysis pass that exposes the \c IVUsers for a loop.
187class IVUsersAnalysis : public AnalysisInfoMixin<IVUsersAnalysis> {
189 static AnalysisKey Key;
190
191public:
193
196};
197
198}
199
200#endif
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
#define LLVM_ABI
Definition Compiler.h:213
This header provides classes for managing per-loop analyses.
#define I(x, y, z)
Definition MD5.cpp:57
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:598
#define P(N)
Represent the analysis usage information of a pass.
A cache of @llvm.assume calls within a function.
CallbackVH(const CallbackVH &)=default
void setValPtr(Value *P)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
IVStrideUse - Keep track of one use of a strided induction variable.
Definition IVUsers.h:36
friend class IVUsers
Definition IVUsers.h:37
Instruction * getUser() const
getUser - Return the user instruction for this use.
Definition IVUsers.h:44
void setOperandValToReplace(Value *Op)
setOperandValToReplace - Assign a new Value as the operand value to replace.
Definition IVUsers.h:61
const PostIncLoopSet & getPostIncLoops() const
getPostIncLoops - Return the set of loops for which the expression has been adjusted to use post-inc ...
Definition IVUsers.h:67
Value * getOperandValToReplace() const
getOperandValToReplace - Return the Value of the operand in the user instruction that this IVStrideUs...
Definition IVUsers.h:55
IVStrideUse(IVUsers *P, Instruction *U, Value *O)
Definition IVUsers.h:39
void setUser(Instruction *NewUser)
setUser - Assign a new user instruction for this use.
Definition IVUsers.h:49
Analysis pass that exposes the IVUsers for a loop.
Definition IVUsers.h:187
LLVM_ABI IVUsers run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR)
Definition IVUsers.cpp:36
const IVUsers & getIU() const
Definition IVUsers.h:175
LLVM_ABI void dump() const
dump - This method is used for debugging.
Definition IVUsers.cpp:292
ilist< IVStrideUse >::const_iterator const_iterator
Definition IVUsers.h:143
bool isIVUserOrOperand(Instruction *Inst) const
Definition IVUsers.h:150
iterator end()
Definition IVUsers.h:145
LLVM_ABI IVStrideUse & AddUser(Instruction *User, Value *Operand)
Definition IVUsers.cpp:246
LLVM_ABI IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT, ScalarEvolution *SE)
Definition IVUsers.cpp:251
LLVM_ABI void releaseMemory()
Definition IVUsers.cpp:295
Loop * getLoop() const
Definition IVUsers.h:123
IVUsers & operator=(IVUsers &&)=delete
LLVM_ABI const SCEV * getStride(const IVStrideUse &IU, const Loop *L) const
Definition IVUsers.cpp:356
IVUsers(const IVUsers &)=delete
LLVM_ABI const SCEV * getReplacementExpr(const IVStrideUse &IU) const
getReplacementExpr - Return a SCEV expression which computes the value of the OperandValToReplace of ...
Definition IVUsers.cpp:329
bool isEphemeral(const Value *V) const
Definition IVUsers.h:154
IVUsers(IVUsers &&X)
Definition IVUsers.h:112
iterator begin()
Definition IVUsers.h:144
LLVM_ABI bool AddUsersIfInteresting(Instruction *I)
AddUsersIfInteresting - Inspect the specified Instruction.
Definition IVUsers.cpp:136
friend class IVStrideUse
Definition IVUsers.h:93
const_iterator end() const
Definition IVUsers.h:147
ilist< IVStrideUse >::iterator iterator
Definition IVUsers.h:142
const_iterator begin() const
Definition IVUsers.h:146
LLVM_ABI const SCEV * getExpr(const IVStrideUse &IU) const
getExpr - Return the expression for the use.
Definition IVUsers.cpp:334
IVUsers & operator=(const IVUsers &)=delete
LLVM_ABI void print(raw_ostream &OS, const Module *=nullptr) const
Definition IVUsers.cpp:265
bool empty() const
Definition IVUsers.h:148
LoopPass(char &pid)
Definition LoopPass.h:31
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This class represents an analyzed expression in the program.
The main scalar evolution driver.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Value * getValPtr() const
LLVM Value Representation.
Definition Value.h:75
Value handle that is nullable, but tries to track the Value.
typename base_list_type::const_iterator const_iterator
Definition ilist.h:122
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.
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
iplist< T, Options... > ilist
Definition ilist.h:344
DWARFExpression::Operation Op
LLVM_ABI Pass * createIVUsersPass()
Definition IVUsers.cpp:51
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
SmallPtrSet< const Loop *, 2 > PostIncLoopSet
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:861
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...