1 //===- llvm/Analysis/IVUsers.h - Induction Variable Users -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements bookkeeping for "interesting" users of expressions
11 // computed from induction variables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_IVUSERS_H
16 #define LLVM_ANALYSIS_IVUSERS_H
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/ADT/SmallVector.h"
28 struct IVUsersOfOneStride;
30 /// IVStrideUse - Keep track of one use of a strided induction variable, where
31 /// the stride is stored externally. The Offset member keeps track of the
32 /// offset from the IV, User is the actual user of the operand, and
33 /// 'OperandValToReplace' is the operand of the User that is the use.
34 class IVStrideUse : public CallbackVH, public ilist_node<IVStrideUse> {
36 IVStrideUse(IVUsersOfOneStride *parent,
38 Instruction* U, Value *O)
39 : CallbackVH(U), Parent(parent), Offset(offset),
40 OperandValToReplace(O),
41 IsUseOfPostIncrementedValue(false) {
44 /// getUser - Return the user instruction for this use.
45 Instruction *getUser() const {
46 return cast<Instruction>(getValPtr());
49 /// setUser - Assign a new user instruction for this use.
50 void setUser(Instruction *NewUser) {
54 /// getParent - Return a pointer to the IVUsersOfOneStride that owns
56 IVUsersOfOneStride *getParent() const { return Parent; }
58 /// getOffset - Return the offset to add to a theoeretical induction
59 /// variable that starts at zero and counts up by the stride to compute
60 /// the value for the use. This always has the same type as the stride.
61 const SCEV *getOffset() const { return Offset; }
63 /// setOffset - Assign a new offset to this use.
64 void setOffset(const SCEV *Val) {
68 /// getOperandValToReplace - Return the Value of the operand in the user
69 /// instruction that this IVStrideUse is representing.
70 Value *getOperandValToReplace() const {
71 return OperandValToReplace;
74 /// setOperandValToReplace - Assign a new Value as the operand value
76 void setOperandValToReplace(Value *Op) {
77 OperandValToReplace = Op;
80 /// isUseOfPostIncrementedValue - True if this should use the
81 /// post-incremented version of this IV, not the preincremented version.
82 /// This can only be set in special cases, such as the terminating setcc
83 /// instruction for a loop or uses dominated by the loop.
84 bool isUseOfPostIncrementedValue() const {
85 return IsUseOfPostIncrementedValue;
88 /// setIsUseOfPostIncrmentedValue - set the flag that indicates whether
89 /// this is a post-increment use.
90 void setIsUseOfPostIncrementedValue(bool Val) {
91 IsUseOfPostIncrementedValue = Val;
95 /// Parent - a pointer to the IVUsersOfOneStride that owns this IVStrideUse.
96 IVUsersOfOneStride *Parent;
98 /// Offset - The offset to add to the base induction expression.
101 /// OperandValToReplace - The Value of the operand in the user instruction
102 /// that this IVStrideUse is representing.
103 WeakVH OperandValToReplace;
105 /// IsUseOfPostIncrementedValue - True if this should use the
106 /// post-incremented version of this IV, not the preincremented version.
107 bool IsUseOfPostIncrementedValue;
109 /// Deleted - Implementation of CallbackVH virtual function to
110 /// recieve notification when the User is deleted.
111 virtual void deleted();
114 template<> struct ilist_traits<IVStrideUse>
115 : public ilist_default_traits<IVStrideUse> {
116 // createSentinel is used to get hold of a node that marks the end of
118 // The sentinel is relative to this instance, so we use a non-static
120 IVStrideUse *createSentinel() const {
121 // since i(p)lists always publicly derive from the corresponding
122 // traits, placing a data member in this class will augment i(p)list.
123 // But since the NodeTy is expected to publicly derive from
124 // ilist_node<NodeTy>, there is a legal viable downcast from it
125 // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
126 // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
127 // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
128 // the superposition.
129 return static_cast<IVStrideUse*>(&Sentinel);
131 static void destroySentinel(IVStrideUse*) {}
133 IVStrideUse *provideInitialHead() const { return createSentinel(); }
134 IVStrideUse *ensureHead(IVStrideUse*) const { return createSentinel(); }
135 static void noteHead(IVStrideUse*, IVStrideUse*) {}
138 mutable ilist_node<IVStrideUse> Sentinel;
141 /// IVUsersOfOneStride - This structure keeps track of all instructions that
142 /// have an operand that is based on the trip count multiplied by some stride.
143 struct IVUsersOfOneStride : public ilist_node<IVUsersOfOneStride> {
145 IVUsersOfOneStride(const IVUsersOfOneStride &I); // do not implement
146 void operator=(const IVUsersOfOneStride &I); // do not implement
149 IVUsersOfOneStride() : Stride(0) {}
151 explicit IVUsersOfOneStride(const SCEV *stride) : Stride(stride) {}
153 /// Stride - The stride for all the contained IVStrideUses. This is
154 /// a constant for affine strides.
157 /// Users - Keep track of all of the users of this stride as well as the
158 /// initial value and the operand that uses the IV.
159 ilist<IVStrideUse> Users;
161 void addUser(const SCEV *Offset, Instruction *User, Value *Operand) {
162 Users.push_back(new IVStrideUse(this, Offset, User, Operand));
165 void removeUser(IVStrideUse *User) {
170 class IVUsers : public LoopPass {
171 friend class IVStrideUserVH;
176 SmallPtrSet<Instruction*,16> Processed;
178 /// IVUses - A list of all tracked IV uses of induction variable expressions
179 /// we are interested in.
180 ilist<IVUsersOfOneStride> IVUses;
183 /// IVUsesByStride - A mapping from the strides in StrideOrder to the
185 std::map<const SCEV *, IVUsersOfOneStride*> IVUsesByStride;
187 /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
188 /// We use this to iterate over the IVUsesByStride collection without being
189 /// dependent on random ordering of pointers in the process.
190 SmallVector<const SCEV *, 16> StrideOrder;
193 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
195 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
197 virtual void releaseMemory();
200 static char ID; // Pass ID, replacement for typeid
203 /// AddUsersIfInteresting - Inspect the specified Instruction. If it is a
204 /// reducible SCEV, recursively add its users to the IVUsesByStride set and
205 /// return true. Otherwise, return false.
206 bool AddUsersIfInteresting(Instruction *I);
208 void AddUser(const SCEV *Stride, const SCEV *Offset,
209 Instruction *User, Value *Operand);
211 /// getReplacementExpr - Return a SCEV expression which computes the
212 /// value of the OperandValToReplace of the given IVStrideUse.
213 const SCEV *getReplacementExpr(const IVStrideUse &U) const;
215 /// getCanonicalExpr - Return a SCEV expression which computes the
216 /// value of the SCEV of the given IVStrideUse, ignoring the
217 /// isUseOfPostIncrementedValue flag.
218 const SCEV *getCanonicalExpr(const IVStrideUse &U) const;
220 void print(raw_ostream &OS, const Module* = 0) const;
222 /// dump - This method is used for debugging.
226 Pass *createIVUsersPass();