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