Prune and tidy #includes.
[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/Instructions.h"
18 #include "llvm/Type.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
21
22 namespace llvm {
23   /// SCEVExpander - This class uses information about analyze scalars to
24   /// rewrite expressions in canonical form.
25   ///
26   /// Clients should create an instance of this class when rewriting is needed,
27   /// and destroy it when finished to allow the release of the associated 
28   /// memory.
29   struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
30     ScalarEvolution &SE;
31     LoopInfo &LI;
32     std::map<SCEVHandle, Value*> InsertedExpressions;
33     std::set<Instruction*> InsertedInstructions;
34
35     Instruction *InsertPt;
36
37     friend struct SCEVVisitor<SCEVExpander, Value*>;
38   public:
39     SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
40
41     LoopInfo &getLoopInfo() const { return LI; }
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     /// isInsertedInstruction - Return true if the specified instruction was
49     /// inserted by the code rewriter.  If so, the client should not modify the
50     /// instruction.
51     bool isInsertedInstruction(Instruction *I) const {
52       return InsertedInstructions.count(I);
53     }
54
55     /// getOrInsertCanonicalInductionVariable - This method returns the
56     /// canonical induction variable of the specified type for the specified
57     /// loop (inserting one if there is none).  A canonical induction variable
58     /// starts at zero and steps by one on each iteration.
59     Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
60       assert(Ty->isInteger() && "Can only insert integer induction variables!");
61       SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
62                                       SE.getIntegerSCEV(1, Ty), L);
63       return expand(H);
64     }
65
66     /// addInsertedValue - Remember the specified instruction as being the
67     /// canonical form for the specified SCEV.
68     void addInsertedValue(Instruction *I, SCEV *S) {
69       InsertedExpressions[S] = (Value*)I;
70       InsertedInstructions.insert(I);
71     }
72
73     Instruction *getInsertionPoint() const { return InsertPt; }
74     
75     /// expandCodeFor - Insert code to directly compute the specified SCEV
76     /// expression into the program.  The inserted code is inserted into the
77     /// specified block.
78     Value *expandCodeFor(SCEVHandle SH, Instruction *IP) {
79       // Expand the code for this SCEV.
80       this->InsertPt = IP;
81       return expand(SH);
82     }
83
84     /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
85     /// we can to share the casts.
86     static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
87                                  const Type *Ty);
88     /// InsertBinop - Insert the specified binary operator, doing a small amount
89     /// of work to avoid inserting an obviously redundant operation.
90     static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
91                               Value *RHS, Instruction *&InsertPt);
92   protected:
93     Value *expand(SCEV *S);
94     
95     Value *visitConstant(SCEVConstant *S) {
96       return S->getValue();
97     }
98
99     Value *visitTruncateExpr(SCEVTruncateExpr *S) {
100       Value *V = expand(S->getOperand());
101       return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
102     }
103
104     Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
105       Value *V = expand(S->getOperand());
106       return CastInst::CreateZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
107     }
108
109     Value *visitSignExtendExpr(SCEVSignExtendExpr *S) {
110       Value *V = expand(S->getOperand());
111       return CastInst::CreateSExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
112     }
113
114     Value *visitAddExpr(SCEVAddExpr *S) {
115       Value *V = expand(S->getOperand(S->getNumOperands()-1));
116
117       // Emit a bunch of add instructions
118       for (int i = S->getNumOperands()-2; i >= 0; --i)
119         V = InsertBinop(Instruction::Add, V, expand(S->getOperand(i)),
120                         InsertPt);
121       return V;
122     }
123
124     Value *visitMulExpr(SCEVMulExpr *S);
125
126     Value *visitUDivExpr(SCEVUDivExpr *S) {
127       Value *LHS = expand(S->getLHS());
128       Value *RHS = expand(S->getRHS());
129       return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
130     }
131
132     Value *visitAddRecExpr(SCEVAddRecExpr *S);
133
134     Value *visitSMaxExpr(SCEVSMaxExpr *S);
135
136     Value *visitUMaxExpr(SCEVUMaxExpr *S);
137
138     Value *visitUnknown(SCEVUnknown *S) {
139       return S->getValue();
140     }
141   };
142 }
143
144 #endif
145