LLVM  3.7.0
IVUsers.h
Go to the documentation of this file.
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"
20 #include "llvm/IR/ValueHandle.h"
21 
22 namespace llvm {
23 
24 class AssumptionCache;
25 class DominatorTree;
26 class Instruction;
27 class Value;
28 class ScalarEvolution;
29 class SCEV;
30 class IVUsers;
31 class DataLayout;
32 
33 /// IVStrideUse - Keep track of one use of a strided induction variable.
34 /// The Expr member keeps track of the expression, User is the actual user
35 /// instruction of the operand, and 'OperandValToReplace' is the operand of
36 /// the User that is the use.
37 class IVStrideUse : public CallbackVH, public ilist_node<IVStrideUse> {
38  friend class IVUsers;
39 public:
41  : CallbackVH(U), Parent(P), 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  /// getOperandValToReplace - Return the Value of the operand in the user
55  /// instruction that this IVStrideUse is representing.
57  return OperandValToReplace;
58  }
59 
60  /// setOperandValToReplace - Assign a new Value as the operand value
61  /// to replace.
63  OperandValToReplace = Op;
64  }
65 
66  /// getPostIncLoops - Return the set of loops for which the expression has
67  /// been adjusted to use post-inc mode.
69  return PostIncLoops;
70  }
71 
72  /// transformToPostInc - Transform the expression to post-inc form for the
73  /// given loop.
74  void transformToPostInc(const Loop *L);
75 
76 private:
77  /// Parent - a pointer to the IVUsers that owns this IVStrideUse.
78  IVUsers *Parent;
79 
80  /// OperandValToReplace - The Value of the operand in the user instruction
81  /// that this IVStrideUse is representing.
82  WeakVH OperandValToReplace;
83 
84  /// PostIncLoops - The set of loops for which Expr has been adjusted to
85  /// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
86  PostIncLoopSet PostIncLoops;
87 
88  /// Deleted - Implementation of CallbackVH virtual function to
89  /// receive notification when the User is deleted.
90  void deleted() override;
91 };
92 
93 template<> struct ilist_traits<IVStrideUse>
94  : public ilist_default_traits<IVStrideUse> {
95  // createSentinel is used to get hold of a node that marks the end of
96  // the list...
97  // The sentinel is relative to this instance, so we use a non-static
98  // method.
100  // since i(p)lists always publicly derive from the corresponding
101  // traits, placing a data member in this class will augment i(p)list.
102  // But since the NodeTy is expected to publicly derive from
103  // ilist_node<NodeTy>, there is a legal viable downcast from it
104  // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
105  // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
106  // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
107  // the superposition.
108  return static_cast<IVStrideUse*>(&Sentinel);
109  }
110  static void destroySentinel(IVStrideUse*) {}
111 
114  static void noteHead(IVStrideUse*, IVStrideUse*) {}
115 
116 private:
117  mutable ilist_node<IVStrideUse> Sentinel;
118 };
119 
120 class IVUsers : public LoopPass {
121  friend class IVStrideUse;
122  Loop *L;
123  AssumptionCache *AC;
124  LoopInfo *LI;
125  DominatorTree *DT;
126  ScalarEvolution *SE;
128 
129  /// IVUses - A list of all tracked IV uses of induction variable expressions
130  /// we are interested in.
131  ilist<IVStrideUse> IVUses;
132 
133  // Ephemeral values used by @llvm.assume in this function.
135 
136  void getAnalysisUsage(AnalysisUsage &AU) const override;
137 
138  bool runOnLoop(Loop *L, LPPassManager &LPM) override;
139 
140  void releaseMemory() override;
141 
142 public:
143  static char ID; // Pass ID, replacement for typeid
144  IVUsers();
145 
146  Loop *getLoop() const { return L; }
147 
148  /// AddUsersIfInteresting - Inspect the specified Instruction. If it is a
149  /// reducible SCEV, recursively add its users to the IVUsesByStride set and
150  /// return true. Otherwise, return false.
152 
154 
155  /// getReplacementExpr - Return a SCEV expression which computes the
156  /// value of the OperandValToReplace of the given IVStrideUse.
157  const SCEV *getReplacementExpr(const IVStrideUse &IU) const;
158 
159  /// getExpr - Return the expression for the use.
160  const SCEV *getExpr(const IVStrideUse &IU) const;
161 
162  const SCEV *getStride(const IVStrideUse &IU, const Loop *L) const;
163 
166  iterator begin() { return IVUses.begin(); }
167  iterator end() { return IVUses.end(); }
168  const_iterator begin() const { return IVUses.begin(); }
169  const_iterator end() const { return IVUses.end(); }
170  bool empty() const { return IVUses.empty(); }
171 
172  bool isIVUserOrOperand(Instruction *Inst) const {
173  return Processed.count(Inst);
174  }
175 
176  void print(raw_ostream &OS, const Module* = nullptr) const override;
177 
178  /// dump - This method is used for debugging.
179  void dump() const;
180 protected:
181  bool AddUsersImpl(Instruction *I, SmallPtrSetImpl<Loop*> &SimpleLoopNests);
182 };
183 
185 
186 }
187 
188 #endif
const SCEV * getExpr(const IVStrideUse &IU) const
getExpr - Return the expression for the use.
Definition: IVUsers.cpp:332
iterator begin()
Definition: IVUsers.h:166
const PostIncLoopSet & getPostIncLoops() const
getPostIncLoops - Return the set of loops for which the expression has been adjusted to use post-inc ...
Definition: IVUsers.h:68
IVStrideUse * createSentinel() const
Definition: IVUsers.h:99
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
Pass * createIVUsersPass()
Definition: IVUsers.cpp:46
ScalarEvolution - This class is the main scalar evolution driver.
void setOperandValToReplace(Value *Op)
setOperandValToReplace - Assign a new Value as the operand value to replace.
Definition: IVUsers.h:62
Loop * getLoop() const
Definition: IVUsers.h:146
A cache of .assume calls within a function.
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
Definition: IVUsers.cpp:284
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
bool AddUsersIfInteresting(Instruction *I)
AddUsersIfInteresting - Inspect the specified Instruction.
Definition: IVUsers.cpp:235
void setValPtr(Value *P)
Definition: ValueHandle.h:352
const SCEV * getStride(const IVStrideUse &IU, const Loop *L) const
Definition: IVUsers.cpp:358
IVStrideUse * provideInitialHead() const
Definition: IVUsers.h:112
const_iterator begin() const
Definition: IVUsers.h:168
ilist_default_traits - Default template traits for intrusive list.
Definition: ilist.h:127
static void noteHead(IVStrideUse *, IVStrideUse *)
Definition: IVUsers.h:114
IVStrideUse * ensureHead(IVStrideUse *) const
Definition: IVUsers.h:113
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:141
const_iterator end() const
Definition: IVUsers.h:169
Instruction * getUser() const
getUser - Return the user instruction for this use.
Definition: IVUsers.h:45
iterator end()
Definition: IVUsers.h:167
ilist< IVStrideUse >::const_iterator const_iterator
Definition: IVUsers.h:165
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
#define P(N)
void setUser(Instruction *NewUser)
setUser - Assign a new user instruction for this use.
Definition: IVUsers.h:50
bool AddUsersImpl(Instruction *I, SmallPtrSetImpl< Loop * > &SimpleLoopNests)
AddUsersImpl - Inspect the specified instruction.
Definition: IVUsers.cpp:118
Value * getValPtr() const
Definition: ValueHandle.h:100
const SCEV * getReplacementExpr(const IVStrideUse &IU) const
getReplacementExpr - Return a SCEV expression which computes the value of the OperandValToReplace of ...
Definition: IVUsers.cpp:327
Represent the analysis usage information of a pass.
ilist< IVStrideUse >::iterator iterator
Definition: IVUsers.h:164
aarch64 type AArch64 Type Promotion Pass
bool isIVUserOrOperand(Instruction *Inst) const
Definition: IVUsers.h:172
Value * getOperandValToReplace() const
getOperandValToReplace - Return the Value of the operand in the user instruction that this IVStrideUs...
Definition: IVUsers.h:56
static char ID
Definition: IVUsers.h:143
void dump() const
dump - This method is used for debugging.
Definition: IVUsers.cpp:315
IVStrideUse(IVUsers *P, Instruction *U, Value *O)
Definition: IVUsers.h:40
SCEV - This class represents an analyzed expression in the program.
ilist_node - Base class that provides next/prev services for nodes that use ilist_nextprev_traits or ...
Definition: ilist_node.h:43
#define I(x, y, z)
Definition: MD5.cpp:54
void transformToPostInc(const Loop *L)
transformToPostInc - Transform the expression to post-inc form for the given loop.
Definition: IVUsers.cpp:364
static NodeTy * createSentinel()
createSentinel - create the dynamic sentinel
Definition: ilist.h:78
LLVM Value Representation.
Definition: Value.h:69
static void destroySentinel(IVStrideUse *)
Definition: IVUsers.h:110
IVStrideUse - Keep track of one use of a strided induction variable.
Definition: IVUsers.h:37
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:344
IVStrideUse & AddUser(Instruction *User, Value *Operand)
Definition: IVUsers.cpp:244
bool empty() const
Definition: IVUsers.h:170