1 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- 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 defines the classes used to generate code from scalar expressions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
17 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
18 #include "llvm/Analysis/ScalarEvolutionNormalization.h"
19 #include "llvm/Support/IRBuilder.h"
20 #include "llvm/Support/TargetFolder.h"
21 #include "llvm/Support/ValueHandle.h"
25 /// SCEVExpander - This class uses information about analyze scalars to
26 /// rewrite expressions in canonical form.
28 /// Clients should create an instance of this class when rewriting is needed,
29 /// and destroy it when finished to allow the release of the associated
31 class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
34 // New instructions receive a name to identifies them with the current pass.
37 std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
39 std::set<AssertingVH<Value> > InsertedValues;
40 std::set<AssertingVH<Value> > InsertedPostIncValues;
42 /// RelevantLoops - A memoization of the "relevant" loop for a given SCEV.
43 DenseMap<const SCEV *, const Loop *> RelevantLoops;
45 /// PostIncLoops - Addrecs referring to any of the given loops are expanded
46 /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
47 /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
48 /// as opposed to a new phi starting at 1. This is only supported in
49 /// non-canonical mode.
50 PostIncLoopSet PostIncLoops;
52 /// IVIncInsertPos - When this is non-null, addrecs expanded in the
53 /// loop it indicates should be inserted with increments at
55 const Loop *IVIncInsertLoop;
57 /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
58 /// insert the IV increment at this position.
59 Instruction *IVIncInsertPos;
61 /// CanonicalMode - When true, expressions are expanded in "canonical"
62 /// form. In particular, addrecs are expanded as arithmetic based on
63 /// a canonical induction variable. When false, expression are expanded
64 /// in a more literal form.
67 /// When invoked from LSR, the expander is in "strength reduction" mode. The
68 /// only difference is that phi's are only reused if they are already in
72 typedef IRBuilder<true, TargetFolder> BuilderType;
76 const char *DebugType;
79 friend struct SCEVVisitor<SCEVExpander, Value*>;
82 /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
83 explicit SCEVExpander(ScalarEvolution &se, const char *name)
84 : SE(se), IVName(name), IVIncInsertLoop(0), IVIncInsertPos(0),
85 CanonicalMode(true), LSRMode(false),
86 Builder(se.getContext(), TargetFolder(se.TD)) {
93 void setDebugType(const char* s) { DebugType = s; }
96 /// clear - Erase the contents of the InsertedExpressions map so that users
97 /// trying to expand the same expression into multiple BasicBlocks or
98 /// different places within the same BasicBlock can do so.
100 InsertedExpressions.clear();
101 InsertedValues.clear();
102 InsertedPostIncValues.clear();
105 /// getOrInsertCanonicalInductionVariable - This method returns the
106 /// canonical induction variable of the specified type for the specified
107 /// loop (inserting one if there is none). A canonical induction variable
108 /// starts at zero and steps by one on each iteration.
109 PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
111 /// hoistStep - Utility for hoisting an IV increment.
112 static bool hoistStep(Instruction *IncV, Instruction *InsertPos,
113 const DominatorTree *DT);
115 /// replaceCongruentIVs - replace congruent phis with their most canonical
116 /// representative. Return the number of phis eliminated.
117 unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
118 SmallVectorImpl<WeakVH> &DeadInsts);
120 /// expandCodeFor - Insert code to directly compute the specified SCEV
121 /// expression into the program. The inserted code is inserted into the
123 Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
125 /// setIVIncInsertPos - Set the current IV increment loop and position.
126 void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
127 assert(!CanonicalMode &&
128 "IV increment positions are not supported in CanonicalMode");
130 IVIncInsertPos = Pos;
133 /// setPostInc - Enable post-inc expansion for addrecs referring to the
134 /// given loops. Post-inc expansion is only supported in non-canonical
136 void setPostInc(const PostIncLoopSet &L) {
137 assert(!CanonicalMode &&
138 "Post-inc expansion is not supported in CanonicalMode");
142 /// clearPostInc - Disable all post-inc expansion.
143 void clearPostInc() {
144 PostIncLoops.clear();
146 // When we change the post-inc loop set, cached expansions may no
148 InsertedPostIncValues.clear();
151 /// disableCanonicalMode - Disable the behavior of expanding expressions in
152 /// canonical form rather than in a more literal form. Non-canonical mode
153 /// is useful for late optimization passes.
154 void disableCanonicalMode() { CanonicalMode = false; }
156 void enableLSRMode() { LSRMode = true; }
158 /// clearInsertPoint - Clear the current insertion point. This is useful
159 /// if the instruction that had been serving as the insertion point may
160 /// have been deleted.
161 void clearInsertPoint() {
162 Builder.ClearInsertionPoint();
165 LLVMContext &getContext() const { return SE.getContext(); }
167 /// InsertBinop - Insert the specified binary operator, doing a small amount
168 /// of work to avoid inserting an obviously redundant operation.
169 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
171 /// ReuseOrCreateCast - Arange for there to be a cast of V to Ty at IP,
172 /// reusing an existing cast if a suitable one exists, moving an existing
173 /// cast if a suitable one exists but isn't in the right place, or
174 /// or creating a new one.
175 Value *ReuseOrCreateCast(Value *V, Type *Ty,
176 Instruction::CastOps Op,
177 BasicBlock::iterator IP);
179 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
180 /// which must be possible with a noop cast, doing what we can to
182 Value *InsertNoopCastOfTo(Value *V, Type *Ty);
184 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
185 /// instead of using ptrtoint+arithmetic+inttoptr.
186 Value *expandAddToGEP(const SCEV *const *op_begin,
187 const SCEV *const *op_end,
188 PointerType *PTy, Type *Ty, Value *V);
190 Value *expand(const SCEV *S);
192 /// expandCodeFor - Insert code to directly compute the specified SCEV
193 /// expression into the program. The inserted code is inserted into the
194 /// SCEVExpander's current insertion point. If a type is specified, the
195 /// result will be expanded to have that type, with a cast if necessary.
196 Value *expandCodeFor(const SCEV *SH, Type *Ty = 0);
198 /// isInsertedInstruction - Return true if the specified instruction was
199 /// inserted by the code rewriter. If so, the client should not modify the
201 bool isInsertedInstruction(Instruction *I) const {
202 return InsertedValues.count(I) || InsertedPostIncValues.count(I);
205 /// getRelevantLoop - Determine the most "relevant" loop for the given SCEV.
206 const Loop *getRelevantLoop(const SCEV *);
208 Value *visitConstant(const SCEVConstant *S) {
209 return S->getValue();
212 Value *visitTruncateExpr(const SCEVTruncateExpr *S);
214 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
216 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
218 Value *visitAddExpr(const SCEVAddExpr *S);
220 Value *visitMulExpr(const SCEVMulExpr *S);
222 Value *visitUDivExpr(const SCEVUDivExpr *S);
224 Value *visitAddRecExpr(const SCEVAddRecExpr *S);
226 Value *visitSMaxExpr(const SCEVSMaxExpr *S);
228 Value *visitUMaxExpr(const SCEVUMaxExpr *S);
230 Value *visitUnknown(const SCEVUnknown *S) {
231 return S->getValue();
234 void rememberInstruction(Value *I);
236 void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
238 bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
240 bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
242 Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
243 PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
247 Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
248 Type *ExpandTy, Type *IntTy, bool useSubtract);