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