LLVM  3.7.0
ScalarEvolutionExpander.h
Go to the documentation of this file.
1 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- 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 defines the classes used to generate code from scalar expressions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
16 
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/ValueHandle.h"
22 #include <set>
23 
24 namespace llvm {
25  class TargetTransformInfo;
26 
27  /// Return true if the given expression is safe to expand in the sense that
28  /// all materialized values are safe to speculate.
29  bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE);
30 
31  /// This class uses information about analyze scalars to
32  /// rewrite expressions in canonical form.
33  ///
34  /// Clients should create an instance of this class when rewriting is needed,
35  /// and destroy it when finished to allow the release of the associated
36  /// memory.
37  class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
38  ScalarEvolution &SE;
39  const DataLayout &DL;
40 
41  // New instructions receive a name to identifies them with the current pass.
42  const char* IVName;
43 
44  // InsertedExpressions caches Values for reuse, so must track RAUW.
45  std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >
46  InsertedExpressions;
47  // InsertedValues only flags inserted instructions so needs no RAUW.
48  std::set<AssertingVH<Value> > InsertedValues;
49  std::set<AssertingVH<Value> > InsertedPostIncValues;
50 
51  /// A memoization of the "relevant" loop for a given SCEV.
53 
54  /// \brief Addrecs referring to any of the given loops are expanded
55  /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
56  /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
57  /// as opposed to a new phi starting at 1. This is only supported in
58  /// non-canonical mode.
59  PostIncLoopSet PostIncLoops;
60 
61  /// \brief When this is non-null, addrecs expanded in the loop it indicates
62  /// should be inserted with increments at IVIncInsertPos.
63  const Loop *IVIncInsertLoop;
64 
65  /// \brief When expanding addrecs in the IVIncInsertLoop loop, insert the IV
66  /// increment at this position.
67  Instruction *IVIncInsertPos;
68 
69  /// \brief Phis that complete an IV chain. Reuse
70  std::set<AssertingVH<PHINode> > ChainedPhis;
71 
72  /// \brief When true, expressions are expanded in "canonical" form. In
73  /// particular, addrecs are expanded as arithmetic based on a canonical
74  /// induction variable. When false, expression are expanded in a more
75  /// literal form.
76  bool CanonicalMode;
77 
78  /// \brief When invoked from LSR, the expander is in "strength reduction"
79  /// mode. The only difference is that phi's are only reused if they are
80  /// already in "expanded" form.
81  bool LSRMode;
82 
84  BuilderType Builder;
85 
86 #ifndef NDEBUG
87  const char *DebugType;
88 #endif
89 
90  friend struct SCEVVisitor<SCEVExpander, Value*>;
91 
92  public:
93  /// \brief Construct a SCEVExpander in "canonical" mode.
94  explicit SCEVExpander(ScalarEvolution &se, const DataLayout &DL,
95  const char *name)
96  : SE(se), DL(DL), IVName(name), IVIncInsertLoop(nullptr),
97  IVIncInsertPos(nullptr), CanonicalMode(true), LSRMode(false),
98  Builder(se.getContext(), TargetFolder(DL)) {
99 #ifndef NDEBUG
100  DebugType = "";
101 #endif
102  }
103 
104 #ifndef NDEBUG
105  void setDebugType(const char* s) { DebugType = s; }
106 #endif
107 
108  /// \brief Erase the contents of the InsertedExpressions map so that users
109  /// trying to expand the same expression into multiple BasicBlocks or
110  /// different places within the same BasicBlock can do so.
111  void clear() {
112  InsertedExpressions.clear();
113  InsertedValues.clear();
114  InsertedPostIncValues.clear();
115  ChainedPhis.clear();
116  }
117 
118  /// \brief Return true for expressions that may incur non-trivial cost to
119  /// evaluate at runtime.
120  bool isHighCostExpansion(const SCEV *Expr, Loop *L) {
122  return isHighCostExpansionHelper(Expr, L, Processed);
123  }
124 
125  /// \brief This method returns the canonical induction variable of the
126  /// specified type for the specified loop (inserting one if there is none).
127  /// A canonical induction variable starts at zero and steps by one on each
128  /// iteration.
130 
131  /// \brief Return the induction variable increment's IV operand.
133  bool allowScale);
134 
135  /// \brief Utility for hoisting an IV increment.
136  bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
137 
138  /// \brief replace congruent phis with their most canonical
139  /// representative. Return the number of phis eliminated.
140  unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
141  SmallVectorImpl<WeakVH> &DeadInsts,
142  const TargetTransformInfo *TTI = nullptr);
143 
144  /// \brief Insert code to directly compute the specified SCEV expression
145  /// into the program. The inserted code is inserted into the specified
146  /// block.
147  Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
148 
149  /// \brief Set the current IV increment loop and position.
150  void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
151  assert(!CanonicalMode &&
152  "IV increment positions are not supported in CanonicalMode");
153  IVIncInsertLoop = L;
154  IVIncInsertPos = Pos;
155  }
156 
157  /// \brief Enable post-inc expansion for addrecs referring to the given
158  /// loops. Post-inc expansion is only supported in non-canonical mode.
159  void setPostInc(const PostIncLoopSet &L) {
160  assert(!CanonicalMode &&
161  "Post-inc expansion is not supported in CanonicalMode");
162  PostIncLoops = L;
163  }
164 
165  /// \brief Disable all post-inc expansion.
166  void clearPostInc() {
167  PostIncLoops.clear();
168 
169  // When we change the post-inc loop set, cached expansions may no
170  // longer be valid.
171  InsertedPostIncValues.clear();
172  }
173 
174  /// \brief Disable the behavior of expanding expressions in canonical form
175  /// rather than in a more literal form. Non-canonical mode is useful for
176  /// late optimization passes.
177  void disableCanonicalMode() { CanonicalMode = false; }
178 
179  void enableLSRMode() { LSRMode = true; }
180 
181  /// \brief Clear the current insertion point. This is useful if the
182  /// instruction that had been serving as the insertion point may have been
183  /// deleted.
185  Builder.ClearInsertionPoint();
186  }
187 
188  /// \brief Return true if the specified instruction was inserted by the code
189  /// rewriter. If so, the client should not modify the instruction.
191  return InsertedValues.count(I) || InsertedPostIncValues.count(I);
192  }
193 
194  void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
195 
196  private:
197  LLVMContext &getContext() const { return SE.getContext(); }
198 
199  /// \brief Recursive helper function for isHighCostExpansion.
200  bool isHighCostExpansionHelper(const SCEV *S, Loop *L,
201  SmallPtrSetImpl<const SCEV *> &Processed);
202 
203  /// \brief Insert the specified binary operator, doing a small amount
204  /// of work to avoid inserting an obviously redundant operation.
205  Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
206 
207  /// \brief Arrange for there to be a cast of V to Ty at IP, reusing an
208  /// existing cast if a suitable one exists, moving an existing cast if a
209  /// suitable one exists but isn't in the right place, or or creating a new
210  /// one.
211  Value *ReuseOrCreateCast(Value *V, Type *Ty,
214 
215  /// \brief Insert a cast of V to the specified type, which must be possible
216  /// with a noop cast, doing what we can to share the casts.
217  Value *InsertNoopCastOfTo(Value *V, Type *Ty);
218 
219  /// \brief Expand a SCEVAddExpr with a pointer type into a GEP
220  /// instead of using ptrtoint+arithmetic+inttoptr.
221  Value *expandAddToGEP(const SCEV *const *op_begin,
222  const SCEV *const *op_end,
223  PointerType *PTy, Type *Ty, Value *V);
224 
225  Value *expand(const SCEV *S);
226 
227  /// \brief Insert code to directly compute the specified SCEV expression
228  /// into the program. The inserted code is inserted into the SCEVExpander's
229  /// current insertion point. If a type is specified, the result will be
230  /// expanded to have that type, with a cast if necessary.
231  Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr);
232 
233  /// \brief Determine the most "relevant" loop for the given SCEV.
234  const Loop *getRelevantLoop(const SCEV *);
235 
236  Value *visitConstant(const SCEVConstant *S) {
237  return S->getValue();
238  }
239 
240  Value *visitTruncateExpr(const SCEVTruncateExpr *S);
241 
242  Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
243 
244  Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
245 
246  Value *visitAddExpr(const SCEVAddExpr *S);
247 
248  Value *visitMulExpr(const SCEVMulExpr *S);
249 
250  Value *visitUDivExpr(const SCEVUDivExpr *S);
251 
252  Value *visitAddRecExpr(const SCEVAddRecExpr *S);
253 
254  Value *visitSMaxExpr(const SCEVSMaxExpr *S);
255 
256  Value *visitUMaxExpr(const SCEVUMaxExpr *S);
257 
258  Value *visitUnknown(const SCEVUnknown *S) {
259  return S->getValue();
260  }
261 
262  void rememberInstruction(Value *I);
263 
264  bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
265 
266  bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
267 
268  Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
269  PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
270  const Loop *L,
271  Type *ExpandTy,
272  Type *IntTy,
273  Type *&TruncTy,
274  bool &InvertStep);
275  Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
276  Type *ExpandTy, Type *IntTy, bool useSubtract);
277  };
278 }
279 
280 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
bool hoistIVInc(Instruction *IncV, Instruction *InsertPos)
Utility for hoisting an IV increment.
LLVMContext & getContext() const
ScalarEvolution - This class is the main scalar evolution driver.
void setDebugType(const char *s)
void clearPostInc()
Disable all post-inc expansion.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
#define false
Definition: ConvertUTF.c:65
void ClearInsertionPoint()
Clear the insertion point: created instructions will not be inserted into a block.
Definition: IRBuilder.h:74
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
void clearInsertPoint()
Clear the current insertion point.
void clear()
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression ...
#define true
Definition: ConvertUTF.c:66
SCEVVisitor - This class defines a simple visitor class that may be used for various SCEV analysis pu...
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:280
bool isHighCostExpansion(const SCEV *Expr, Loop *L)
Return true for expressions that may incur non-trivial cost to evaluate at runtime.
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
SCEVExpander(ScalarEvolution &se, const DataLayout &DL, const char *name)
Construct a SCEVExpander in "canonical" mode.
void setChainedPhi(PHINode *PN)
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
PHINode * getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty)
This method returns the canonical induction variable of the specified type for the specified loop (in...
void setIVIncInsertPos(const Loop *L, Instruction *Pos)
Set the current IV increment loop and position.
This class uses information about analyze scalars to rewrite expressions in canonical form...
SCEV - This class represents an analyzed expression in the program.
#define I(x, y, z)
Definition: MD5.cpp:54
Instruction * getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale)
Return the induction variable increment's IV operand.
LLVM Value Representation.
Definition: Value.h:69
static const char * name
bool isInsertedInstruction(Instruction *I) const
Return true if the specified instruction was inserted by the code rewriter.
void disableCanonicalMode()
Disable the behavior of expanding expressions in canonical form rather than in a more literal form...
void setPostInc(const PostIncLoopSet &L)
Enable post-inc expansion for addrecs referring to the given loops.
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:93