blob: b6d77b93ae053ee9ccf65edfcb68a81e2d32e61b [file] [log] [blame]
Nate Begeman36f891b2005-07-30 00:12:19 +00001//===- 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"
18using namespace llvm;
19
Chris Lattnerca1a4be2006-02-04 09:51:53 +000020/// InsertCastOfTo - Insert a cast of V to the specified type, doing what
21/// we can to share the casts.
22Value *SCEVExpander::InsertCastOfTo(Value *V, const Type *Ty) {
23 // FIXME: keep track of the cast instruction.
24 if (Constant *C = dyn_cast<Constant>(V))
25 return ConstantExpr::getCast(C, Ty);
26
27 if (Argument *A = dyn_cast<Argument>(V)) {
28 // Check to see if there is already a cast!
29 for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
30 UI != E; ++UI) {
31 if ((*UI)->getType() == Ty)
32 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
Reid Spencer3da59db2006-11-27 01:05:10 +000033 // If the cast isn't the first instruction of the function, move it.
Chris Lattnerca1a4be2006-02-04 09:51:53 +000034 if (BasicBlock::iterator(CI) !=
35 A->getParent()->getEntryBlock().begin()) {
36 CI->moveBefore(A->getParent()->getEntryBlock().begin());
37 }
38 return CI;
39 }
40 }
Reid Spencer3da59db2006-11-27 01:05:10 +000041 return CastInst::createInferredCast(V, Ty, V->getName(),
42 A->getParent()->getEntryBlock().begin());
Chris Lattnerca1a4be2006-02-04 09:51:53 +000043 }
44
45 Instruction *I = cast<Instruction>(V);
46
47 // Check to see if there is already a cast. If there is, use it.
48 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
49 UI != E; ++UI) {
50 if ((*UI)->getType() == Ty)
51 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
52 BasicBlock::iterator It = I; ++It;
53 if (isa<InvokeInst>(I))
54 It = cast<InvokeInst>(I)->getNormalDest()->begin();
55 while (isa<PHINode>(It)) ++It;
56 if (It != BasicBlock::iterator(CI)) {
57 // Splice the cast immediately after the operand in question.
58 CI->moveBefore(It);
59 }
60 return CI;
61 }
62 }
63 BasicBlock::iterator IP = I; ++IP;
64 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
65 IP = II->getNormalDest()->begin();
66 while (isa<PHINode>(IP)) ++IP;
Reid Spencer3da59db2006-11-27 01:05:10 +000067 return CastInst::createInferredCast(V, Ty, V->getName(), IP);
Chris Lattnerca1a4be2006-02-04 09:51:53 +000068}
69
Nate Begeman36f891b2005-07-30 00:12:19 +000070Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
71 const Type *Ty = S->getType();
72 int FirstOp = 0; // Set if we should emit a subtract.
73 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
74 if (SC->getValue()->isAllOnesValue())
75 FirstOp = 1;
76
77 int i = S->getNumOperands()-2;
78 Value *V = expandInTy(S->getOperand(i+1), Ty);
79
80 // Emit a bunch of multiply instructions
81 for (; i >= FirstOp; --i)
82 V = BinaryOperator::createMul(V, expandInTy(S->getOperand(i), Ty),
83 "tmp.", InsertPt);
84 // -1 * ... ---> 0 - ...
85 if (FirstOp == 1)
86 V = BinaryOperator::createNeg(V, "tmp.", InsertPt);
87 return V;
88}
89
90Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
91 const Type *Ty = S->getType();
92 const Loop *L = S->getLoop();
93 // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
94 assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!");
95
96 // {X,+,F} --> X + {0,+,F}
97 if (!isa<SCEVConstant>(S->getStart()) ||
98 !cast<SCEVConstant>(S->getStart())->getValue()->isNullValue()) {
99 Value *Start = expandInTy(S->getStart(), Ty);
100 std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
101 NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty);
102 Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty);
103
104 // FIXME: look for an existing add to use.
105 return BinaryOperator::createAdd(Rest, Start, "tmp.", InsertPt);
106 }
107
108 // {0,+,1} --> Insert a canonical induction variable into the loop!
109 if (S->getNumOperands() == 2 &&
110 S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) {
111 // Create and insert the PHI node for the induction variable in the
112 // specified loop.
113 BasicBlock *Header = L->getHeader();
114 PHINode *PN = new PHINode(Ty, "indvar", Header->begin());
115 PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
116
117 pred_iterator HPI = pred_begin(Header);
118 assert(HPI != pred_end(Header) && "Loop with zero preds???");
119 if (!L->contains(*HPI)) ++HPI;
120 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
121 "No backedge in loop?");
122
123 // Insert a unit add instruction right before the terminator corresponding
124 // to the back-edge.
125 Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0)
126 : ConstantInt::get(Ty, 1);
127 Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next",
128 (*HPI)->getTerminator());
129
130 pred_iterator PI = pred_begin(Header);
131 if (*PI == L->getLoopPreheader())
132 ++PI;
133 PN->addIncoming(Add, *PI);
134 return PN;
135 }
136
137 // Get the canonical induction variable I for this loop.
138 Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
139
Chris Lattnerdf14a042005-10-30 06:24:33 +0000140 // If this is a simple linear addrec, emit it now as a special case.
Nate Begeman36f891b2005-07-30 00:12:19 +0000141 if (S->getNumOperands() == 2) { // {0,+,F} --> i*F
142 Value *F = expandInTy(S->getOperand(1), Ty);
Chris Lattnerdf14a042005-10-30 06:24:33 +0000143
144 // IF the step is by one, just return the inserted IV.
145 if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(F))
Reid Spencerb83eb642006-10-20 07:07:24 +0000146 if (CI->getZExtValue() == 1)
Chris Lattnerdf14a042005-10-30 06:24:33 +0000147 return I;
148
149 // If the insert point is directly inside of the loop, emit the multiply at
150 // the insert point. Otherwise, L is a loop that is a parent of the insert
151 // point loop. If we can, move the multiply to the outer most loop that it
152 // is safe to be in.
153 Instruction *MulInsertPt = InsertPt;
154 Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
155 if (InsertPtLoop != L && InsertPtLoop &&
156 L->contains(InsertPtLoop->getHeader())) {
157 while (InsertPtLoop != L) {
158 // If we cannot hoist the multiply out of this loop, don't.
159 if (!InsertPtLoop->isLoopInvariant(F)) break;
160
161 // Otherwise, move the insert point to the preheader of the loop.
162 MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator();
163 InsertPtLoop = InsertPtLoop->getParentLoop();
164 }
165 }
166
167 return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt);
Nate Begeman36f891b2005-07-30 00:12:19 +0000168 }
169
170 // If this is a chain of recurrences, turn it into a closed form, using the
171 // folders, then expandCodeFor the closed form. This allows the folders to
172 // simplify the expression without having to build a bunch of special code
173 // into this folder.
174 SCEVHandle IH = SCEVUnknown::get(I); // Get I as a "symbolic" SCEV.
175
176 SCEVHandle V = S->evaluateAtIteration(IH);
177 //std::cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
178
179 return expandInTy(V, Ty);
180}