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*> {
32 std::map<SCEVHandle, Value*> InsertedExpressions;
33 std::set<Instruction*> InsertedInstructions;
35 BasicBlock::iterator InsertPt;
37 friend struct SCEVVisitor<SCEVExpander, Value*>;
39 SCEVExpander(ScalarEvolution &se, LoopInfo &li)
42 LoopInfo &getLoopInfo() const { return LI; }
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(); }
49 /// isInsertedInstruction - Return true if the specified instruction was
50 /// inserted by the code rewriter. If so, the client should not modify the
52 bool isInsertedInstruction(Instruction *I) const {
53 return InsertedInstructions.count(I);
56 /// getOrInsertCanonicalInductionVariable - This method returns the
57 /// canonical induction variable of the specified type for the specified
58 /// loop (inserting one if there is none). A canonical induction variable
59 /// starts at zero and steps by one on each iteration.
60 Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
61 assert(Ty->isInteger() && "Can only insert integer induction variables!");
62 SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
63 SE.getIntegerSCEV(1, Ty), L);
67 /// addInsertedValue - Remember the specified instruction as being the
68 /// canonical form for the specified SCEV.
69 void addInsertedValue(Instruction *I, const SCEV *S) {
70 InsertedExpressions[S] = (Value*)I;
71 InsertedInstructions.insert(I);
74 void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
76 BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
78 /// expandCodeFor - Insert code to directly compute the specified SCEV
79 /// expression into the program. The inserted code is inserted into the
80 /// SCEVExpander's current insertion point.
81 Value *expandCodeFor(SCEVHandle SH, const Type *Ty);
83 /// expandCodeFor - Insert code to directly compute the specified SCEV
84 /// expression into the program. The inserted code is inserted into the
86 Value *expandCodeFor(SCEVHandle SH, const Type *Ty,
87 BasicBlock::iterator IP) {
88 setInsertionPoint(IP);
89 return expandCodeFor(SH, Ty);
92 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
93 /// we can to share the casts.
94 Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
97 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
98 /// which must be possible with a noop cast.
99 Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
101 /// InsertBinop - Insert the specified binary operator, doing a small amount
102 /// of work to avoid inserting an obviously redundant operation.
103 static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
104 Value *RHS, BasicBlock::iterator InsertPt);
107 Value *expand(const SCEV *S);
109 Value *visitConstant(const SCEVConstant *S) {
110 return S->getValue();
113 Value *visitTruncateExpr(const SCEVTruncateExpr *S);
115 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
117 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
119 Value *visitAddExpr(const SCEVAddExpr *S);
121 Value *visitMulExpr(const SCEVMulExpr *S);
123 Value *visitUDivExpr(const SCEVUDivExpr *S);
125 Value *visitAddRecExpr(const SCEVAddRecExpr *S);
127 Value *visitSMaxExpr(const SCEVSMaxExpr *S);
129 Value *visitUMaxExpr(const SCEVUMaxExpr *S);
131 Value *visitUnknown(const SCEVUnknown *S) {
132 return S->getValue();