1 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/BasicBlock.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Type.h"
21 #include "llvm/Analysis/ScalarEvolution.h"
22 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 #include "llvm/Support/CFG.h"
26 /// SCEVExpander - This class uses information about analyze scalars to
27 /// rewrite expressions in canonical form.
29 /// Clients should create an instance of this class when rewriting is needed,
30 /// and destroy it when finished to allow the release of the associated
32 struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
35 std::map<SCEVHandle, Value*> InsertedExpressions;
36 std::set<Instruction*> InsertedInstructions;
38 Instruction *InsertPt;
40 friend struct SCEVVisitor<SCEVExpander, Value*>;
42 SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
44 LoopInfo &getLoopInfo() const { return LI; }
46 /// clear - Erase the contents of the InsertedExpressions map so that users
47 /// trying to expand the same expression into multiple BasicBlocks or
48 /// different places within the same BasicBlock can do so.
49 void clear() { InsertedExpressions.clear(); }
51 /// isInsertedInstruction - Return true if the specified instruction was
52 /// inserted by the code rewriter. If so, the client should not modify the
54 bool isInsertedInstruction(Instruction *I) const {
55 return InsertedInstructions.count(I);
58 /// getOrInsertCanonicalInductionVariable - This method returns the
59 /// canonical induction variable of the specified type for the specified
60 /// loop (inserting one if there is none). A canonical induction variable
61 /// starts at zero and steps by one on each iteration.
62 Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
63 assert(Ty->isInteger() && "Can only insert integer induction variables!");
64 SCEVHandle H = SCEVAddRecExpr::get(SCEVUnknown::getIntegerSCEV(0, Ty),
65 SCEVUnknown::getIntegerSCEV(1, Ty), L);
69 /// addInsertedValue - Remember the specified instruction as being the
70 /// canonical form for the specified SCEV.
71 void addInsertedValue(Instruction *I, SCEV *S) {
72 InsertedExpressions[S] = (Value*)I;
73 InsertedInstructions.insert(I);
76 Instruction *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
82 /// If a particular value sign is required, a type may be specified for the
84 Value *expandCodeFor(SCEVHandle SH, Instruction *IP, const Type *Ty = 0) {
85 // Expand the code for this SCEV.
87 return expandInTy(SH, Ty);
90 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
91 /// we can to share the casts.
92 static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
94 /// InsertBinop - Insert the specified binary operator, doing a small amount
95 /// of work to avoid inserting an obviously redundant operation.
96 static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
97 Value *RHS, Instruction *&InsertPt);
99 Value *expand(SCEV *S) {
100 // Check to see if we already expanded this.
101 std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
102 if (I != InsertedExpressions.end())
106 InsertedExpressions[S] = V;
110 Value *expandInTy(SCEV *S, const Type *Ty) {
111 Value *V = expand(S);
112 if (Ty && V->getType() != Ty) {
113 if (isa<PointerType>(Ty) && V->getType()->isInteger())
114 return InsertCastOfTo(Instruction::IntToPtr, V, Ty);
115 else if (Ty->isInteger() && isa<PointerType>(V->getType()))
116 return InsertCastOfTo(Instruction::PtrToInt, V, Ty);
117 else if (Ty->getPrimitiveSizeInBits() ==
118 V->getType()->getPrimitiveSizeInBits())
119 return InsertCastOfTo(Instruction::BitCast, V, Ty);
120 else if (Ty->getPrimitiveSizeInBits() >
121 V->getType()->getPrimitiveSizeInBits())
122 return InsertCastOfTo(Instruction::ZExt, V, Ty);
124 return InsertCastOfTo(Instruction::Trunc, V, Ty);
129 Value *visitConstant(SCEVConstant *S) {
130 return S->getValue();
133 Value *visitTruncateExpr(SCEVTruncateExpr *S) {
134 Value *V = expand(S->getOperand());
135 return CastInst::createTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
138 Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
139 Value *V = expandInTy(S->getOperand(), S->getType());
140 return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
143 Value *visitAddExpr(SCEVAddExpr *S) {
144 const Type *Ty = S->getType();
145 Value *V = expandInTy(S->getOperand(S->getNumOperands()-1), Ty);
147 // Emit a bunch of add instructions
148 for (int i = S->getNumOperands()-2; i >= 0; --i)
149 V = InsertBinop(Instruction::Add, V, expandInTy(S->getOperand(i), Ty),
154 Value *visitMulExpr(SCEVMulExpr *S);
156 Value *visitSDivExpr(SCEVSDivExpr *S) {
157 const Type *Ty = S->getType();
158 Value *LHS = expandInTy(S->getLHS(), Ty);
159 Value *RHS = expandInTy(S->getRHS(), Ty);
160 return InsertBinop(Instruction::SDiv, LHS, RHS, InsertPt);
163 Value *visitAddRecExpr(SCEVAddRecExpr *S);
165 Value *visitUnknown(SCEVUnknown *S) {
166 return S->getValue();