IVUsers.h revision bafbbdde38dedf05b53b731cbc083b2c483ae64e
1//===- llvm/Analysis/IVUsers.h - Induction Variable Users -------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements bookkeeping for "interesting" users of expressions
11// computed from induction variables.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_IVUSERS_H
16#define LLVM_ANALYSIS_IVUSERS_H
17
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/Analysis/ScalarEvolution.h"
20#include "llvm/ADT/SmallVector.h"
21#include <map>
22
23namespace llvm {
24
25class DominatorTree;
26class Instruction;
27class Value;
28struct IVUsersOfOneStride;
29
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.
34class IVStrideUse : public CallbackVH, public ilist_node<IVStrideUse> {
35public:
36  IVStrideUse(IVUsersOfOneStride *parent,
37              const SCEV *offset,
38              Instruction* U, Value *O)
39    : CallbackVH(U), Parent(parent), Offset(offset),
40      OperandValToReplace(O),
41      IsUseOfPostIncrementedValue(false) {
42  }
43
44  /// getUser - Return the user instruction for this use.
45  Instruction *getUser() const {
46    return cast<Instruction>(getValPtr());
47  }
48
49  /// setUser - Assign a new user instruction for this use.
50  void setUser(Instruction *NewUser) {
51    setValPtr(NewUser);
52  }
53
54  /// getParent - Return a pointer to the IVUsersOfOneStride that owns
55  /// this IVStrideUse.
56  IVUsersOfOneStride *getParent() const { return Parent; }
57
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; }
62
63  /// setOffset - Assign a new offset to this use.
64  void setOffset(const SCEV *Val) {
65    Offset = Val;
66  }
67
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;
72  }
73
74  /// setOperandValToReplace - Assign a new Value as the operand value
75  /// to replace.
76  void setOperandValToReplace(Value *Op) {
77    OperandValToReplace = Op;
78  }
79
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;
86  }
87
88  /// setIsUseOfPostIncrmentedValue - set the flag that indicates whether
89  /// this is a post-increment use.
90  void setIsUseOfPostIncrementedValue(bool Val) {
91    IsUseOfPostIncrementedValue = Val;
92  }
93
94private:
95  /// Parent - a pointer to the IVUsersOfOneStride that owns this IVStrideUse.
96  IVUsersOfOneStride *Parent;
97
98  /// Offset - The offset to add to the base induction expression.
99  const SCEV *Offset;
100
101  /// OperandValToReplace - The Value of the operand in the user instruction
102  /// that this IVStrideUse is representing.
103  WeakVH OperandValToReplace;
104
105  /// IsUseOfPostIncrementedValue - True if this should use the
106  /// post-incremented version of this IV, not the preincremented version.
107  bool IsUseOfPostIncrementedValue;
108
109  /// Deleted - Implementation of CallbackVH virtual function to
110  /// recieve notification when the User is deleted.
111  virtual void deleted();
112};
113
114template<> 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
117  // the list...
118  // The sentinel is relative to this instance, so we use a non-static
119  // method.
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);
130  }
131  static void destroySentinel(IVStrideUse*) {}
132
133  IVStrideUse *provideInitialHead() const { return createSentinel(); }
134  IVStrideUse *ensureHead(IVStrideUse*) const { return createSentinel(); }
135  static void noteHead(IVStrideUse*, IVStrideUse*) {}
136
137private:
138  mutable ilist_node<IVStrideUse> Sentinel;
139};
140
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.
143struct IVUsersOfOneStride : public ilist_node<IVUsersOfOneStride> {
144private:
145  IVUsersOfOneStride(const IVUsersOfOneStride &I); // do not implement
146  void operator=(const IVUsersOfOneStride &I);     // do not implement
147
148public:
149  IVUsersOfOneStride() : Stride(0) {}
150
151  explicit IVUsersOfOneStride(const SCEV *stride) : Stride(stride) {}
152
153  /// Stride - The stride for all the contained IVStrideUses. This is
154  /// a constant for affine strides.
155  const SCEV *Stride;
156
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;
160
161  void addUser(const SCEV *Offset, Instruction *User, Value *Operand) {
162    Users.push_back(new IVStrideUse(this, Offset, User, Operand));
163  }
164
165  void removeUser(IVStrideUse *User) {
166    Users.erase(User);
167  }
168};
169
170class IVUsers : public LoopPass {
171  friend class IVStrideUserVH;
172  Loop *L;
173  LoopInfo *LI;
174  DominatorTree *DT;
175  ScalarEvolution *SE;
176  SmallPtrSet<Instruction*,16> Processed;
177
178  /// IVUses - A list of all tracked IV uses of induction variable expressions
179  /// we are interested in.
180  ilist<IVUsersOfOneStride> IVUses;
181
182public:
183  /// IVUsesByStride - A mapping from the strides in StrideOrder to the
184  /// uses in IVUses.
185  std::map<const SCEV *, IVUsersOfOneStride*> IVUsesByStride;
186
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;
191
192private:
193  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
194
195  virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
196
197  virtual void releaseMemory();
198
199public:
200  static char ID; // Pass ID, replacement for typeid
201  IVUsers();
202
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);
207
208  void AddUser(const SCEV *Stride, const SCEV *Offset,
209               Instruction *User, Value *Operand);
210
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;
214
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;
219
220  void print(raw_ostream &OS, const Module* = 0) const;
221
222  /// dump - This method is used for debugging.
223  void dump() const;
224};
225
226Pass *createIVUsersPass();
227
228}
229
230#endif
231