a887c830c954ce4f5057b56dc71cf110bdf72432
[oota-llvm.git] / include / llvm / Analysis / IVUsers.h
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/ScalarEvolutionNormalization.h"
20 #include "llvm/Support/ValueHandle.h"
21
22 namespace llvm {
23
24 class DominatorTree;
25 class Instruction;
26 class Value;
27 class IVUsers;
28 class ScalarEvolution;
29 class SCEV;
30 class IVUsers;
31
32 /// IVStrideUse - Keep track of one use of a strided induction variable.
33 /// The Expr member keeps track of the expression, User is the actual user
34 /// instruction of the operand, and 'OperandValToReplace' is the operand of
35 /// the User that is the use.
36 class IVStrideUse : public CallbackVH, public ilist_node<IVStrideUse> {
37   friend class IVUsers;
38 public:
39   IVStrideUse(IVUsers *P, const SCEV *E,
40               Instruction* U, Value *O)
41     : CallbackVH(U), Parent(P), Expr(E), OperandValToReplace(O) {
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 IVUsers that owns
55   /// this IVStrideUse.
56   IVUsers *getParent() const { return Parent; }
57
58   /// getExpr - Return the expression for the use.
59   const SCEV *getExpr() const { return Expr; }
60
61   /// setExpr - Assign a new expression to this use.
62   void setExpr(const SCEV *Val) {
63     Expr = Val;
64   }
65
66   const SCEV *getStride(const Loop *L) const;
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   /// getPostIncLoops - Return the set of loops for which the expression has
81   /// been adjusted to use post-inc mode.
82   const PostIncLoopSet &getPostIncLoops() const {
83     return PostIncLoops;
84   }
85
86   /// transformToPostInc - Transform the expression to post-inc form for the
87   /// given loop.
88   void transformToPostInc(const Loop *L);
89
90 private:
91   /// Parent - a pointer to the IVUsers that owns this IVStrideUse.
92   IVUsers *Parent;
93
94   /// Expr - The expression for this use.
95   const SCEV *Expr;
96
97   /// OperandValToReplace - The Value of the operand in the user instruction
98   /// that this IVStrideUse is representing.
99   WeakVH OperandValToReplace;
100
101   /// PostIncLoops - The set of loops for which Expr has been adjusted to
102   /// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
103   PostIncLoopSet PostIncLoops;
104
105   /// Deleted - Implementation of CallbackVH virtual function to
106   /// receive notification when the User is deleted.
107   virtual void deleted();
108 };
109
110 template<> struct ilist_traits<IVStrideUse>
111   : public ilist_default_traits<IVStrideUse> {
112   // createSentinel is used to get hold of a node that marks the end of
113   // the list...
114   // The sentinel is relative to this instance, so we use a non-static
115   // method.
116   IVStrideUse *createSentinel() const {
117     // since i(p)lists always publicly derive from the corresponding
118     // traits, placing a data member in this class will augment i(p)list.
119     // But since the NodeTy is expected to publicly derive from
120     // ilist_node<NodeTy>, there is a legal viable downcast from it
121     // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
122     // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
123     // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
124     // the superposition.
125     return static_cast<IVStrideUse*>(&Sentinel);
126   }
127   static void destroySentinel(IVStrideUse*) {}
128
129   IVStrideUse *provideInitialHead() const { return createSentinel(); }
130   IVStrideUse *ensureHead(IVStrideUse*) const { return createSentinel(); }
131   static void noteHead(IVStrideUse*, IVStrideUse*) {}
132
133 private:
134   mutable ilist_node<IVStrideUse> Sentinel;
135 };
136
137 class IVUsers : public LoopPass {
138   friend class IVStrideUse;
139   Loop *L;
140   LoopInfo *LI;
141   DominatorTree *DT;
142   ScalarEvolution *SE;
143   SmallPtrSet<Instruction*,16> Processed;
144
145   /// IVUses - A list of all tracked IV uses of induction variable expressions
146   /// we are interested in.
147   ilist<IVStrideUse> IVUses;
148
149   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
150
151   virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
152
153   virtual void releaseMemory();
154
155 public:
156   static char ID; // Pass ID, replacement for typeid
157   IVUsers();
158
159   /// AddUsersIfInteresting - Inspect the specified Instruction.  If it is a
160   /// reducible SCEV, recursively add its users to the IVUsesByStride set and
161   /// return true.  Otherwise, return false.
162   bool AddUsersIfInteresting(Instruction *I);
163
164   IVStrideUse &AddUser(const SCEV *Expr,
165                        Instruction *User, Value *Operand);
166
167   /// getReplacementExpr - Return a SCEV expression which computes the
168   /// value of the OperandValToReplace of the given IVStrideUse.
169   const SCEV *getReplacementExpr(const IVStrideUse &U) const;
170
171   typedef ilist<IVStrideUse>::iterator iterator;
172   typedef ilist<IVStrideUse>::const_iterator const_iterator;
173   iterator begin() { return IVUses.begin(); }
174   iterator end()   { return IVUses.end(); }
175   const_iterator begin() const { return IVUses.begin(); }
176   const_iterator end() const   { return IVUses.end(); }
177   bool empty() const { return IVUses.empty(); }
178
179   void print(raw_ostream &OS, const Module* = 0) const;
180
181   /// dump - This method is used for debugging.
182   void dump() const;
183 };
184
185 Pass *createIVUsersPass();
186
187 }
188
189 #endif