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