1 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the classes used to generate code from scalar expressions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
17 #include "llvm/Instructions.h"
18 #include "llvm/Type.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 /// SCEVExpander - This class uses information about analyze scalars to
24 /// rewrite expressions in canonical form.
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
29 struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
31 std::map<const SCEV*, AssertingVH<Value> > InsertedExpressions;
32 std::set<Value*> InsertedValues;
34 BasicBlock::iterator InsertPt;
36 friend struct SCEVVisitor<SCEVExpander, Value*>;
38 explicit SCEVExpander(ScalarEvolution &se)
41 /// clear - Erase the contents of the InsertedExpressions map so that users
42 /// trying to expand the same expression into multiple BasicBlocks or
43 /// different places within the same BasicBlock can do so.
44 void clear() { InsertedExpressions.clear(); }
46 /// isInsertedInstruction - Return true if the specified instruction was
47 /// inserted by the code rewriter. If so, the client should not modify the
49 bool isInsertedInstruction(Instruction *I) const {
50 return InsertedValues.count(I);
53 /// isInsertedExpression - Return true if the the code rewriter has a
54 /// Value* recorded for the given expression.
55 bool isInsertedExpression(const SCEV *S) const {
56 return InsertedExpressions.count(S);
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);
65 /// addInsertedValue - Remember the specified instruction as being the
66 /// canonical form for the specified SCEV.
67 void addInsertedValue(Value *V, const SCEV *S) {
68 InsertedExpressions[S] = V;
69 InsertedValues.insert(V);
72 void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
74 BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
76 /// expandCodeFor - Insert code to directly compute the specified SCEV
77 /// expression into the program. The inserted code is inserted into the
78 /// SCEVExpander's current insertion point. If a type is specified, the
79 /// result will be expanded to have that type, with a cast if necessary.
80 Value *expandCodeFor(const SCEV* SH, const Type *Ty = 0);
82 /// expandCodeFor - Insert code to directly compute the specified SCEV
83 /// expression into the program. The inserted code is inserted into the
85 Value *expandCodeFor(const SCEV* SH, const Type *Ty,
86 BasicBlock::iterator IP) {
87 setInsertionPoint(IP);
88 return expandCodeFor(SH, Ty);
91 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
92 /// we can to share the casts.
93 Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
96 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
97 /// which must be possible with a noop cast.
98 Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
100 /// InsertBinop - Insert the specified binary operator, doing a small amount
101 /// of work to avoid inserting an obviously redundant operation.
102 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
103 Value *RHS, BasicBlock::iterator InsertPt);
106 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
107 /// instead of using ptrtoint+arithmetic+inttoptr.
108 Value *expandAddToGEP(const SCEV* const *op_begin,
109 const SCEV* const *op_end,
110 const PointerType *PTy, const Type *Ty, Value *V);
112 Value *expand(const SCEV *S);
114 Value *visitConstant(const SCEVConstant *S) {
115 return S->getValue();
118 Value *visitTruncateExpr(const SCEVTruncateExpr *S);
120 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
122 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
124 Value *visitAddExpr(const SCEVAddExpr *S);
126 Value *visitMulExpr(const SCEVMulExpr *S);
128 Value *visitUDivExpr(const SCEVUDivExpr *S);
130 Value *visitAddRecExpr(const SCEVAddRecExpr *S);
132 Value *visitSMaxExpr(const SCEVSMaxExpr *S);
134 Value *visitUMaxExpr(const SCEVUMaxExpr *S);
136 Value *visitUnknown(const SCEVUnknown *S) {
137 return S->getValue();