Push LLVMContext _back_ through IRBuilder.
[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/Support/IRBuilder.h"
19 #include "llvm/Support/TargetFolder.h"
20
21 namespace llvm {
22   /// SCEVExpander - This class uses information about analyze scalars to
23   /// rewrite expressions in canonical form.
24   ///
25   /// Clients should create an instance of this class when rewriting is needed,
26   /// and destroy it when finished to allow the release of the associated 
27   /// memory.
28   struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
29     ScalarEvolution &SE;
30     std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
31       InsertedExpressions;
32     std::set<Value*> InsertedValues;
33
34     typedef IRBuilder<true, TargetFolder> BuilderType;
35     BuilderType Builder;
36
37     friend struct SCEVVisitor<SCEVExpander, Value*>;
38   public:
39     explicit SCEVExpander(ScalarEvolution &se)
40       : SE(se), Builder(*se.getContext(),
41                         TargetFolder(se.TD, se.getContext())) {}
42
43     /// clear - Erase the contents of the InsertedExpressions map so that users
44     /// trying to expand the same expression into multiple BasicBlocks or
45     /// different places within the same BasicBlock can do so.
46     void clear() { InsertedExpressions.clear(); }
47
48     /// getOrInsertCanonicalInductionVariable - This method returns the
49     /// canonical induction variable of the specified type for the specified
50     /// loop (inserting one if there is none).  A canonical induction variable
51     /// starts at zero and steps by one on each iteration.
52     Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty);
53
54     /// expandCodeFor - Insert code to directly compute the specified SCEV
55     /// expression into the program.  The inserted code is inserted into the
56     /// specified block.
57     Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *IP) {
58       Builder.SetInsertPoint(IP->getParent(), IP);
59       return expandCodeFor(SH, Ty);
60     }
61
62   private:
63     LLVMContext *getContext() const { return SE.getContext(); }
64     
65     /// InsertBinop - Insert the specified binary operator, doing a small amount
66     /// of work to avoid inserting an obviously redundant operation.
67     Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
68
69     /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
70     /// which must be possible with a noop cast, doing what we can to
71     /// share the casts.
72     Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
73
74     /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
75     /// instead of using ptrtoint+arithmetic+inttoptr.
76     Value *expandAddToGEP(const SCEV *const *op_begin,
77                           const SCEV *const *op_end,
78                           const PointerType *PTy, const Type *Ty, Value *V);
79
80     Value *expand(const SCEV *S);
81
82     /// expandCodeFor - Insert code to directly compute the specified SCEV
83     /// expression into the program.  The inserted code is inserted into the
84     /// SCEVExpander's current insertion point. If a type is specified, the
85     /// result will be expanded to have that type, with a cast if necessary.
86     Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0);
87
88     /// isInsertedInstruction - Return true if the specified instruction was
89     /// inserted by the code rewriter.  If so, the client should not modify the
90     /// instruction.
91     bool isInsertedInstruction(Instruction *I) const {
92       return InsertedValues.count(I);
93     }
94
95     Value *visitConstant(const SCEVConstant *S) {
96       return S->getValue();
97     }
98
99     Value *visitTruncateExpr(const SCEVTruncateExpr *S);
100
101     Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
102
103     Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
104
105     Value *visitAddExpr(const SCEVAddExpr *S);
106
107     Value *visitMulExpr(const SCEVMulExpr *S);
108
109     Value *visitUDivExpr(const SCEVUDivExpr *S);
110
111     Value *visitAddRecExpr(const SCEVAddRecExpr *S);
112
113     Value *visitSMaxExpr(const SCEVSMaxExpr *S);
114
115     Value *visitUMaxExpr(const SCEVUMaxExpr *S);
116
117     Value *visitUnknown(const SCEVUnknown *S) {
118       return S->getValue();
119     }
120   };
121 }
122
123 #endif
124