For PR411:
[oota-llvm.git] / lib / Analysis / ScalarEvolutionExpander.cpp
1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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/LoopInfo.h"
17 #include "llvm/Analysis/ScalarEvolutionExpander.h"
18 using namespace llvm;
19
20 Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
21   const Type *Ty = S->getType();
22   int FirstOp = 0;  // Set if we should emit a subtract.
23   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
24     if (SC->getValue()->isAllOnesValue())
25       FirstOp = 1;
26
27   int i = S->getNumOperands()-2;
28   Value *V = expandInTy(S->getOperand(i+1), Ty);
29
30   // Emit a bunch of multiply instructions
31   for (; i >= FirstOp; --i)
32     V = BinaryOperator::createMul(V, expandInTy(S->getOperand(i), Ty),
33                                   "tmp.", InsertPt);
34   // -1 * ...  --->  0 - ...
35   if (FirstOp == 1)
36     V = BinaryOperator::createNeg(V, "tmp.", InsertPt);
37   return V;
38 }
39
40 Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
41   const Type *Ty = S->getType();
42   const Loop *L = S->getLoop();
43   // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
44   assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!");
45
46   // {X,+,F} --> X + {0,+,F}
47   if (!isa<SCEVConstant>(S->getStart()) ||
48       !cast<SCEVConstant>(S->getStart())->getValue()->isNullValue()) {
49     Value *Start = expandInTy(S->getStart(), Ty);
50     std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
51     NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty);
52     Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty);
53
54     // FIXME: look for an existing add to use.
55     return BinaryOperator::createAdd(Rest, Start, "tmp.", InsertPt);
56   }
57
58   // {0,+,1} --> Insert a canonical induction variable into the loop!
59   if (S->getNumOperands() == 2 &&
60       S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) {
61     // Create and insert the PHI node for the induction variable in the
62     // specified loop.
63     BasicBlock *Header = L->getHeader();
64     PHINode *PN = new PHINode(Ty, "indvar", Header->begin());
65     PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
66
67     pred_iterator HPI = pred_begin(Header);
68     assert(HPI != pred_end(Header) && "Loop with zero preds???");
69     if (!L->contains(*HPI)) ++HPI;
70     assert(HPI != pred_end(Header) && L->contains(*HPI) &&
71            "No backedge in loop?");
72
73     // Insert a unit add instruction right before the terminator corresponding
74     // to the back-edge.
75     Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0)
76                                           : ConstantInt::get(Ty, 1);
77     Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next",
78                                                  (*HPI)->getTerminator());
79
80     pred_iterator PI = pred_begin(Header);
81     if (*PI == L->getLoopPreheader())
82       ++PI;
83     PN->addIncoming(Add, *PI);
84     return PN;
85   }
86
87   // Get the canonical induction variable I for this loop.
88   Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
89
90   // If this is a simple linear addrec, emit it now as a special case.
91   if (S->getNumOperands() == 2) {   // {0,+,F} --> i*F
92     Value *F = expandInTy(S->getOperand(1), Ty);
93     
94     // IF the step is by one, just return the inserted IV.
95     if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(F))
96       if (CI->getRawValue() == 1)
97         return I;
98     
99     // If the insert point is directly inside of the loop, emit the multiply at
100     // the insert point.  Otherwise, L is a loop that is a parent of the insert
101     // point loop.  If we can, move the multiply to the outer most loop that it
102     // is safe to be in.
103     Instruction *MulInsertPt = InsertPt;
104     Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
105     if (InsertPtLoop != L && InsertPtLoop &&
106         L->contains(InsertPtLoop->getHeader())) {
107       while (InsertPtLoop != L) {
108         // If we cannot hoist the multiply out of this loop, don't.
109         if (!InsertPtLoop->isLoopInvariant(F)) break;
110
111         // Otherwise, move the insert point to the preheader of the loop.
112         MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator();
113         InsertPtLoop = InsertPtLoop->getParentLoop();
114       }
115     }
116     
117     return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt);
118   }
119
120   // If this is a chain of recurrences, turn it into a closed form, using the
121   // folders, then expandCodeFor the closed form.  This allows the folders to
122   // simplify the expression without having to build a bunch of special code
123   // into this folder.
124   SCEVHandle IH = SCEVUnknown::get(I);   // Get I as a "symbolic" SCEV.
125
126   SCEVHandle V = S->evaluateAtIteration(IH);
127   //std::cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
128
129   return expandInTy(V, Ty);
130 }