Introduce encapsulation for ScalarEvolution's TargetData object, and refactor
[oota-llvm.git] / lib / Analysis / ScalarEvolutionExpander.cpp
1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- 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 contains the implementation of the scalar evolution expander,
11 // which is used to generate the code corresponding to a given scalar evolution
12 // expression.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/ScalarEvolutionExpander.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 using namespace llvm;
19
20 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
21 /// we can to share the casts.
22 Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
23                                     const Type *Ty) {
24   // Short-circuit unnecessary bitcasts.
25   if (opcode == Instruction::BitCast && V->getType() == Ty)
26     return V;
27
28   // Short-circuit unnecessary inttoptr<->ptrtoint casts.
29   if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) &&
30       SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType()))
31     if (CastInst *CI = dyn_cast<CastInst>(V))
32       if ((CI->getOpcode() == Instruction::PtrToInt ||
33            CI->getOpcode() == Instruction::IntToPtr) &&
34           SE.getTypeSizeInBits(CI->getType()) ==
35           SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
36         return CI->getOperand(0);
37
38   // FIXME: keep track of the cast instruction.
39   if (Constant *C = dyn_cast<Constant>(V))
40     return ConstantExpr::getCast(opcode, C, Ty);
41   
42   if (Argument *A = dyn_cast<Argument>(V)) {
43     // Check to see if there is already a cast!
44     for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
45          UI != E; ++UI) {
46       if ((*UI)->getType() == Ty)
47         if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
48           if (CI->getOpcode() == opcode) {
49             // If the cast isn't the first instruction of the function, move it.
50             if (BasicBlock::iterator(CI) != 
51                 A->getParent()->getEntryBlock().begin()) {
52               CI->moveBefore(A->getParent()->getEntryBlock().begin());
53             }
54             return CI;
55           }
56     }
57     return CastInst::Create(opcode, V, Ty, V->getName(), 
58                             A->getParent()->getEntryBlock().begin());
59   }
60
61   Instruction *I = cast<Instruction>(V);
62
63   // Check to see if there is already a cast.  If there is, use it.
64   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
65        UI != E; ++UI) {
66     if ((*UI)->getType() == Ty)
67       if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
68         if (CI->getOpcode() == opcode) {
69           BasicBlock::iterator It = I; ++It;
70           if (isa<InvokeInst>(I))
71             It = cast<InvokeInst>(I)->getNormalDest()->begin();
72           while (isa<PHINode>(It)) ++It;
73           if (It != BasicBlock::iterator(CI)) {
74             // Splice the cast immediately after the operand in question.
75             CI->moveBefore(It);
76           }
77           return CI;
78         }
79   }
80   BasicBlock::iterator IP = I; ++IP;
81   if (InvokeInst *II = dyn_cast<InvokeInst>(I))
82     IP = II->getNormalDest()->begin();
83   while (isa<PHINode>(IP)) ++IP;
84   return CastInst::Create(opcode, V, Ty, V->getName(), IP);
85 }
86
87 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
88 /// which must be possible with a noop cast.
89 Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
90   Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
91   assert((Op == Instruction::BitCast ||
92           Op == Instruction::Instruction::PtrToInt ||
93           Op == Instruction::Instruction::IntToPtr) &&
94          "InsertNoopCastOfTo cannot perform non-noop casts!");
95   assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
96          "InsertNoopCastOfTo cannot change sizes!");
97   return InsertCastOfTo(Op, V, Ty);
98 }
99
100 /// InsertBinop - Insert the specified binary operator, doing a small amount
101 /// of work to avoid inserting an obviously redundant operation.
102 Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
103                                  Value *RHS, Instruction *InsertPt) {
104   // Fold a binop with constant operands.
105   if (Constant *CLHS = dyn_cast<Constant>(LHS))
106     if (Constant *CRHS = dyn_cast<Constant>(RHS))
107       return ConstantExpr::get(Opcode, CLHS, CRHS);
108
109   // Do a quick scan to see if we have this binop nearby.  If so, reuse it.
110   unsigned ScanLimit = 6;
111   BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin();
112   if (InsertPt != BlockBegin) {
113     // Scanning starts from the last instruction before InsertPt.
114     BasicBlock::iterator IP = InsertPt;
115     --IP;
116     for (; ScanLimit; --IP, --ScanLimit) {
117       if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP))
118         if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS &&
119             BinOp->getOperand(1) == RHS)
120           return BinOp;
121       if (IP == BlockBegin) break;
122     }
123   }
124   
125   // If we haven't found this binop, insert it.
126   return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
127 }
128
129 Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
130   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
131   Value *V = expand(S->getOperand(S->getNumOperands()-1));
132   V = InsertNoopCastOfTo(V, Ty);
133
134   // Emit a bunch of add instructions
135   for (int i = S->getNumOperands()-2; i >= 0; --i) {
136     Value *W = expand(S->getOperand(i));
137     W = InsertNoopCastOfTo(W, Ty);
138     V = InsertBinop(Instruction::Add, V, W, InsertPt);
139   }
140   return V;
141 }
142     
143 Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
144   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
145   int FirstOp = 0;  // Set if we should emit a subtract.
146   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
147     if (SC->getValue()->isAllOnesValue())
148       FirstOp = 1;
149
150   int i = S->getNumOperands()-2;
151   Value *V = expand(S->getOperand(i+1));
152   V = InsertNoopCastOfTo(V, Ty);
153
154   // Emit a bunch of multiply instructions
155   for (; i >= FirstOp; --i) {
156     Value *W = expand(S->getOperand(i));
157     W = InsertNoopCastOfTo(W, Ty);
158     V = InsertBinop(Instruction::Mul, V, W, InsertPt);
159   }
160
161   // -1 * ...  --->  0 - ...
162   if (FirstOp == 1)
163     V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt);
164   return V;
165 }
166
167 Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
168   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
169
170   Value *LHS = expand(S->getLHS());
171   LHS = InsertNoopCastOfTo(LHS, Ty);
172   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
173     const APInt &RHS = SC->getValue()->getValue();
174     if (RHS.isPowerOf2())
175       return InsertBinop(Instruction::LShr, LHS,
176                          ConstantInt::get(Ty, RHS.logBase2()),
177                          InsertPt);
178   }
179
180   Value *RHS = expand(S->getRHS());
181   RHS = InsertNoopCastOfTo(RHS, Ty);
182   return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
183 }
184
185 Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
186   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
187   const Loop *L = S->getLoop();
188
189   // {X,+,F} --> X + {0,+,F}
190   if (!S->getStart()->isZero()) {
191     Value *Start = expand(S->getStart());
192     Start = InsertNoopCastOfTo(Start, Ty);
193     std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
194     NewOps[0] = SE.getIntegerSCEV(0, Ty);
195     Value *Rest = expand(SE.getAddRecExpr(NewOps, L));
196     Rest = InsertNoopCastOfTo(Rest, Ty);
197
198     // FIXME: look for an existing add to use.
199     return InsertBinop(Instruction::Add, Rest, Start, InsertPt);
200   }
201
202   // {0,+,1} --> Insert a canonical induction variable into the loop!
203   if (S->isAffine() &&
204       S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
205     // Create and insert the PHI node for the induction variable in the
206     // specified loop.
207     BasicBlock *Header = L->getHeader();
208     PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
209     PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
210
211     pred_iterator HPI = pred_begin(Header);
212     assert(HPI != pred_end(Header) && "Loop with zero preds???");
213     if (!L->contains(*HPI)) ++HPI;
214     assert(HPI != pred_end(Header) && L->contains(*HPI) &&
215            "No backedge in loop?");
216
217     // Insert a unit add instruction right before the terminator corresponding
218     // to the back-edge.
219     Constant *One = ConstantInt::get(Ty, 1);
220     Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
221                                                  (*HPI)->getTerminator());
222
223     pred_iterator PI = pred_begin(Header);
224     if (*PI == L->getLoopPreheader())
225       ++PI;
226     PN->addIncoming(Add, *PI);
227     return PN;
228   }
229
230   // Get the canonical induction variable I for this loop.
231   Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
232
233   // If this is a simple linear addrec, emit it now as a special case.
234   if (S->isAffine()) {   // {0,+,F} --> i*F
235     Value *F = expand(S->getOperand(1));
236     F = InsertNoopCastOfTo(F, Ty);
237     
238     // IF the step is by one, just return the inserted IV.
239     if (ConstantInt *CI = dyn_cast<ConstantInt>(F))
240       if (CI->getValue() == 1)
241         return I;
242     
243     // If the insert point is directly inside of the loop, emit the multiply at
244     // the insert point.  Otherwise, L is a loop that is a parent of the insert
245     // point loop.  If we can, move the multiply to the outer most loop that it
246     // is safe to be in.
247     Instruction *MulInsertPt = InsertPt;
248     Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
249     if (InsertPtLoop != L && InsertPtLoop &&
250         L->contains(InsertPtLoop->getHeader())) {
251       do {
252         // If we cannot hoist the multiply out of this loop, don't.
253         if (!InsertPtLoop->isLoopInvariant(F)) break;
254
255         BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader();
256
257         // If this loop hasn't got a preheader, we aren't able to hoist the
258         // multiply.
259         if (!InsertPtLoopPH)
260           break;
261
262         // Otherwise, move the insert point to the preheader.
263         MulInsertPt = InsertPtLoopPH->getTerminator();
264         InsertPtLoop = InsertPtLoop->getParentLoop();
265       } while (InsertPtLoop != L);
266     }
267     
268     return InsertBinop(Instruction::Mul, I, F, MulInsertPt);
269   }
270
271   // If this is a chain of recurrences, turn it into a closed form, using the
272   // folders, then expandCodeFor the closed form.  This allows the folders to
273   // simplify the expression without having to build a bunch of special code
274   // into this folder.
275   SCEVHandle IH = SE.getUnknown(I);   // Get I as a "symbolic" SCEV.
276
277   SCEVHandle V = S->evaluateAtIteration(IH, SE);
278   //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
279
280   return expand(V);
281 }
282
283 Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
284   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
285   Value *V = expand(S->getOperand());
286   V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
287   return CastInst::CreateTruncOrBitCast(V, Ty, "tmp.", InsertPt);
288 }
289
290 Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
291   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
292   Value *V = expand(S->getOperand());
293   V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
294   return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt);
295 }
296
297 Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
298   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
299   Value *V = expand(S->getOperand());
300   V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
301   return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt);
302 }
303
304 Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
305   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
306   Value *LHS = expand(S->getOperand(0));
307   LHS = InsertNoopCastOfTo(LHS, Ty);
308   for (unsigned i = 1; i < S->getNumOperands(); ++i) {
309     Value *RHS = expand(S->getOperand(i));
310     RHS = InsertNoopCastOfTo(RHS, Ty);
311     Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
312     LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt);
313   }
314   return LHS;
315 }
316
317 Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
318   const Type *Ty = SE.getEffectiveSCEVType(S->getType());
319   Value *LHS = expand(S->getOperand(0));
320   LHS = InsertNoopCastOfTo(LHS, Ty);
321   for (unsigned i = 1; i < S->getNumOperands(); ++i) {
322     Value *RHS = expand(S->getOperand(i));
323     RHS = InsertNoopCastOfTo(RHS, Ty);
324     Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt);
325     LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt);
326   }
327   return LHS;
328 }
329
330 Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
331                                    Instruction *IP) {
332   // Expand the code for this SCEV.
333   assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
334          "non-trivial casts should be done with the SCEVs directly!");
335   this->InsertPt = IP;
336   Value *V = expand(SH);
337   return InsertNoopCastOfTo(V, Ty);
338 }
339
340 Value *SCEVExpander::expand(const SCEV *S) {
341   // Check to see if we already expanded this.
342   std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
343   if (I != InsertedExpressions.end())
344     return I->second;
345   
346   Value *V = visit(S);
347   InsertedExpressions[S] = V;
348   return V;
349 }