1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- 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 contains the implementation of the scalar evolution expander,
11 // which is used to generate the code corresponding to a given scalar evolution
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/ScalarEvolutionExpander.h"
17 #include "llvm/Analysis/LoopInfo.h"
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,
24 // Short-circuit unnecessary bitcasts.
25 if (opcode == Instruction::BitCast && V->getType() == Ty)
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);
38 // FIXME: keep track of the cast instruction.
39 if (Constant *C = dyn_cast<Constant>(V))
40 return ConstantExpr::getCast(opcode, C, Ty);
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();
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());
57 return CastInst::Create(opcode, V, Ty, V->getName(),
58 A->getParent()->getEntryBlock().begin());
61 Instruction *I = cast<Instruction>(V);
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();
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.
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);
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);
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, BasicBlock::iterator 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);
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;
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)
121 if (IP == BlockBegin) break;
125 // If we haven't found this binop, insert it.
126 return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
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);
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);
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())
150 int i = S->getNumOperands()-2;
151 Value *V = expand(S->getOperand(i+1));
152 V = InsertNoopCastOfTo(V, Ty);
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);
161 // -1 * ... ---> 0 - ...
163 V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt);
167 Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
168 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
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()),
180 Value *RHS = expand(S->getRHS());
181 RHS = InsertNoopCastOfTo(RHS, Ty);
182 return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
185 Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
186 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
187 const Loop *L = S->getLoop();
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);
198 // FIXME: look for an existing add to use.
199 return InsertBinop(Instruction::Add, Rest, Start, InsertPt);
202 // {0,+,1} --> Insert a canonical induction variable into the loop!
204 S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
205 // Create and insert the PHI node for the induction variable in the
207 BasicBlock *Header = L->getHeader();
208 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
209 PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
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?");
217 // Insert a unit add instruction right before the terminator corresponding
219 Constant *One = ConstantInt::get(Ty, 1);
220 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
221 (*HPI)->getTerminator());
223 pred_iterator PI = pred_begin(Header);
224 if (*PI == L->getLoopPreheader())
226 PN->addIncoming(Add, *PI);
230 // Get the canonical induction variable I for this loop.
231 Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
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);
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)
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
247 BasicBlock::iterator MulInsertPt = getInsertionPoint();
248 Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
249 if (InsertPtLoop != L && InsertPtLoop &&
250 L->contains(InsertPtLoop->getHeader())) {
252 // If we cannot hoist the multiply out of this loop, don't.
253 if (!InsertPtLoop->isLoopInvariant(F)) break;
255 BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader();
257 // If this loop hasn't got a preheader, we aren't able to hoist the
262 // Otherwise, move the insert point to the preheader.
263 MulInsertPt = InsertPtLoopPH->getTerminator();
264 InsertPtLoop = InsertPtLoop->getParentLoop();
265 } while (InsertPtLoop != L);
268 return InsertBinop(Instruction::Mul, I, F, MulInsertPt);
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
275 SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV.
277 SCEVHandle V = S->evaluateAtIteration(IH, SE);
278 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
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 new TruncInst(V, Ty, "tmp.", InsertPt);
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 new ZExtInst(V, Ty, "tmp.", InsertPt);
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 new SExtInst(V, Ty, "tmp.", InsertPt);
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);
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);
330 Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
331 BasicBlock::iterator 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!");
336 Value *V = expand(SH);
337 return InsertNoopCastOfTo(V, Ty);
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())
347 InsertedExpressions[S] = V;