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