Generalize IVUsers to track arbitrary expressions rather than expressions
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolutionExpander.h
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_SCALAREVOLUTION_EXPANDER_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
16
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 <set>
22
23 namespace llvm {
24   /// SCEVExpander - This class uses information about analyze scalars to
25   /// rewrite expressions in canonical form.
26   ///
27   /// Clients should create an instance of this class when rewriting is needed,
28   /// and destroy it when finished to allow the release of the associated
29   /// memory.
30   class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
31     ScalarEvolution &SE;
32     std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
33       InsertedExpressions;
34     std::set<Value*> InsertedValues;
35
36     /// PostIncLoops - Addrecs referring to any of the given loops are expanded
37     /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
38     /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
39     /// as opposed to a new phi starting at 1. This is only supported in
40     /// non-canonical mode.
41     PostIncLoopSet PostIncLoops;
42
43     /// IVIncInsertPos - When this is non-null, addrecs expanded in the
44     /// loop it indicates should be inserted with increments at
45     /// IVIncInsertPos.
46     const Loop *IVIncInsertLoop;
47
48     /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
49     /// insert the IV increment at this position.
50     Instruction *IVIncInsertPos;
51
52     /// CanonicalMode - When true, expressions are expanded in "canonical"
53     /// form. In particular, addrecs are expanded as arithmetic based on
54     /// a canonical induction variable. When false, expression are expanded
55     /// in a more literal form.
56     bool CanonicalMode;
57
58     typedef IRBuilder<true, TargetFolder> BuilderType;
59     BuilderType Builder;
60
61     friend struct SCEVVisitor<SCEVExpander, Value*>;
62
63   public:
64     /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
65     explicit SCEVExpander(ScalarEvolution &se)
66       : SE(se), IVIncInsertLoop(0), CanonicalMode(true),
67         Builder(se.getContext(), TargetFolder(se.TD)) {}
68
69     /// clear - Erase the contents of the InsertedExpressions map so that users
70     /// trying to expand the same expression into multiple BasicBlocks or
71     /// different places within the same BasicBlock can do so.
72     void clear() { InsertedExpressions.clear(); }
73
74     /// getOrInsertCanonicalInductionVariable - This method returns the
75     /// canonical induction variable of the specified type for the specified
76     /// loop (inserting one if there is none).  A canonical induction variable
77     /// starts at zero and steps by one on each iteration.
78     Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty);
79
80     /// expandCodeFor - Insert code to directly compute the specified SCEV
81     /// expression into the program.  The inserted code is inserted into the
82     /// specified block.
83     Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *I);
84
85     /// setIVIncInsertPos - Set the current IV increment loop and position.
86     void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
87       assert(!CanonicalMode &&
88              "IV increment positions are not supported in CanonicalMode");
89       IVIncInsertLoop = L;
90       IVIncInsertPos = Pos;
91     }
92
93     /// setPostInc - Enable post-inc expansion for addrecs referring to the
94     /// given loops. Post-inc expansion is only supported in non-canonical
95     /// mode.
96     void setPostInc(const PostIncLoopSet &L) {
97       assert(!CanonicalMode &&
98              "Post-inc expansion is not supported in CanonicalMode");
99       PostIncLoops = L;
100     }
101
102     /// clearPostInc - Disable all post-inc expansion.
103     void clearPostInc() {
104       PostIncLoops.clear();
105     }
106
107     /// disableCanonicalMode - Disable the behavior of expanding expressions in
108     /// canonical form rather than in a more literal form. Non-canonical mode
109     /// is useful for late optimization passes.
110     void disableCanonicalMode() { CanonicalMode = false; }
111
112     /// clearInsertPoint - Clear the current insertion point. This is useful
113     /// if the instruction that had been serving as the insertion point may
114     /// have been deleted.
115     void clearInsertPoint() {
116       Builder.ClearInsertionPoint();
117     }
118
119   private:
120     LLVMContext &getContext() const { return SE.getContext(); }
121
122     /// InsertBinop - Insert the specified binary operator, doing a small amount
123     /// of work to avoid inserting an obviously redundant operation.
124     Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
125
126     /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
127     /// which must be possible with a noop cast, doing what we can to
128     /// share the casts.
129     Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
130
131     /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
132     /// instead of using ptrtoint+arithmetic+inttoptr.
133     Value *expandAddToGEP(const SCEV *const *op_begin,
134                           const SCEV *const *op_end,
135                           const PointerType *PTy, const Type *Ty, Value *V);
136
137     Value *expand(const SCEV *S);
138
139     /// expandCodeFor - Insert code to directly compute the specified SCEV
140     /// expression into the program.  The inserted code is inserted into the
141     /// SCEVExpander's current insertion point. If a type is specified, the
142     /// result will be expanded to have that type, with a cast if necessary.
143     Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0);
144
145     /// isInsertedInstruction - Return true if the specified instruction was
146     /// inserted by the code rewriter.  If so, the client should not modify the
147     /// instruction.
148     bool isInsertedInstruction(Instruction *I) const {
149       return InsertedValues.count(I);
150     }
151
152     Value *visitConstant(const SCEVConstant *S) {
153       return S->getValue();
154     }
155
156     Value *visitTruncateExpr(const SCEVTruncateExpr *S);
157
158     Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
159
160     Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
161
162     Value *visitAddExpr(const SCEVAddExpr *S);
163
164     Value *visitMulExpr(const SCEVMulExpr *S);
165
166     Value *visitUDivExpr(const SCEVUDivExpr *S);
167
168     Value *visitAddRecExpr(const SCEVAddRecExpr *S);
169
170     Value *visitSMaxExpr(const SCEVSMaxExpr *S);
171
172     Value *visitUMaxExpr(const SCEVUMaxExpr *S);
173
174     Value *visitUnknown(const SCEVUnknown *S) {
175       return S->getValue();
176     }
177
178     void rememberInstruction(Value *I);
179
180     void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
181
182     Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
183     PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
184                                        const Loop *L,
185                                        const Type *ExpandTy,
186                                        const Type *IntTy);
187   };
188 }
189
190 #endif