ScalarEvolutionExpander.h revision cf5ab820227dedd77fb91d0904b6dc3694a7c196
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
22namespace 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<Value*> InsertedValues;
34
35    BasicBlock::iterator InsertPt;
36
37    friend struct SCEVVisitor<SCEVExpander, Value*>;
38  public:
39    SCEVExpander(ScalarEvolution &se, LoopInfo &li)
40      : SE(se), LI(li) {}
41
42    LoopInfo &getLoopInfo() const { return LI; }
43
44    /// clear - Erase the contents of the InsertedExpressions map so that users
45    /// trying to expand the same expression into multiple BasicBlocks or
46    /// different places within the same BasicBlock can do so.
47    void clear() { InsertedExpressions.clear(); }
48
49    /// isInsertedInstruction - Return true if the specified instruction was
50    /// inserted by the code rewriter.  If so, the client should not modify the
51    /// instruction.
52    bool isInsertedInstruction(Instruction *I) const {
53      return InsertedValues.count(I);
54    }
55
56    /// isInsertedExpression - Return true if the the code rewriter has a
57    /// Value* recorded for the given expression.
58    bool isInsertedExpression(const SCEV *S) const {
59      return InsertedExpressions.count(S);
60    }
61
62    /// getOrInsertCanonicalInductionVariable - This method returns the
63    /// canonical induction variable of the specified type for the specified
64    /// loop (inserting one if there is none).  A canonical induction variable
65    /// starts at zero and steps by one on each iteration.
66    Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
67      assert(Ty->isInteger() && "Can only insert integer induction variables!");
68      SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
69                                      SE.getIntegerSCEV(1, Ty), L);
70      return expand(H);
71    }
72
73    /// addInsertedValue - Remember the specified instruction as being the
74    /// canonical form for the specified SCEV.
75    void addInsertedValue(Value *V, const SCEV *S) {
76      InsertedExpressions[S] = V;
77      InsertedValues.insert(V);
78    }
79
80    void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
81
82    BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
83
84    /// expandCodeFor - Insert code to directly compute the specified SCEV
85    /// expression into the program.  The inserted code is inserted into the
86    /// SCEVExpander's current insertion point.
87    Value *expandCodeFor(SCEVHandle SH, const Type *Ty);
88
89    /// expandCodeFor - Insert code to directly compute the specified SCEV
90    /// expression into the program.  The inserted code is inserted into the
91    /// specified block.
92    Value *expandCodeFor(SCEVHandle SH, const Type *Ty,
93                         BasicBlock::iterator IP) {
94      setInsertionPoint(IP);
95      return expandCodeFor(SH, Ty);
96    }
97
98    /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
99    /// we can to share the casts.
100    Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
101                          const Type *Ty);
102
103    /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
104    /// which must be possible with a noop cast.
105    Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
106
107    /// InsertBinop - Insert the specified binary operator, doing a small amount
108    /// of work to avoid inserting an obviously redundant operation.
109    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
110                       Value *RHS, BasicBlock::iterator InsertPt);
111
112  private:
113    Value *expand(const SCEV *S);
114
115    Value *visitConstant(const SCEVConstant *S) {
116      return S->getValue();
117    }
118
119    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
120
121    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
122
123    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
124
125    Value *visitAddExpr(const SCEVAddExpr *S);
126
127    Value *visitMulExpr(const SCEVMulExpr *S);
128
129    Value *visitUDivExpr(const SCEVUDivExpr *S);
130
131    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
132
133    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
134
135    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
136
137    Value *visitUnknown(const SCEVUnknown *S) {
138      return S->getValue();
139    }
140  };
141}
142
143#endif
144
145