ScalarEvolutionExpander.h revision 890f92b744fb074465bc2b7006ee753a181f62a4
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  class TargetData;
24
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  struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
32    ScalarEvolution &SE;
33    LoopInfo &LI;
34    const TargetData &TD;
35    std::map<SCEVHandle, Value*> InsertedExpressions;
36    std::set<Instruction*> InsertedInstructions;
37
38    Instruction *InsertPt;
39
40    friend struct SCEVVisitor<SCEVExpander, Value*>;
41  public:
42    SCEVExpander(ScalarEvolution &se, LoopInfo &li, const TargetData &td)
43      : SE(se), LI(li), TD(td) {}
44
45    LoopInfo &getLoopInfo() const { return LI; }
46
47    /// clear - Erase the contents of the InsertedExpressions map so that users
48    /// trying to expand the same expression into multiple BasicBlocks or
49    /// different places within the same BasicBlock can do so.
50    void clear() { InsertedExpressions.clear(); }
51
52    /// isInsertedInstruction - Return true if the specified instruction was
53    /// inserted by the code rewriter.  If so, the client should not modify the
54    /// instruction.
55    bool isInsertedInstruction(Instruction *I) const {
56      return InsertedInstructions.count(I);
57    }
58
59    /// getOrInsertCanonicalInductionVariable - This method returns the
60    /// canonical induction variable of the specified type for the specified
61    /// loop (inserting one if there is none).  A canonical induction variable
62    /// starts at zero and steps by one on each iteration.
63    Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
64      assert(Ty->isInteger() && "Can only insert integer induction variables!");
65      SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
66                                      SE.getIntegerSCEV(1, Ty), L);
67      return expand(H);
68    }
69
70    /// addInsertedValue - Remember the specified instruction as being the
71    /// canonical form for the specified SCEV.
72    void addInsertedValue(Instruction *I, const SCEV *S) {
73      InsertedExpressions[S] = (Value*)I;
74      InsertedInstructions.insert(I);
75    }
76
77    Instruction *getInsertionPoint() const { return InsertPt; }
78
79    /// expandCodeFor - Insert code to directly compute the specified SCEV
80    /// expression into the program.  The inserted code is inserted into the
81    /// specified block.
82    Value *expandCodeFor(SCEVHandle SH, const Type *Ty, Instruction *IP);
83
84    /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
85    /// we can to share the casts.
86    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(const SCEV *S);
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
125