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);
37 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(V
))
38 if ((CE
->getOpcode() == Instruction::PtrToInt
||
39 CE
->getOpcode() == Instruction::IntToPtr
) &&
40 SE
.getTypeSizeInBits(CE
->getType()) ==
41 SE
.getTypeSizeInBits(CE
->getOperand(0)->getType()))
42 return CE
->getOperand(0);
45 // FIXME: keep track of the cast instruction.
46 if (Constant
*C
= dyn_cast
<Constant
>(V
))
47 return ConstantExpr::getCast(opcode
, C
, Ty
);
49 if (Argument
*A
= dyn_cast
<Argument
>(V
)) {
50 // Check to see if there is already a cast!
51 for (Value::use_iterator UI
= A
->use_begin(), E
= A
->use_end();
53 if ((*UI
)->getType() == Ty
)
54 if (CastInst
*CI
= dyn_cast
<CastInst
>(cast
<Instruction
>(*UI
)))
55 if (CI
->getOpcode() == opcode
) {
56 // If the cast isn't the first instruction of the function, move it.
57 if (BasicBlock::iterator(CI
) !=
58 A
->getParent()->getEntryBlock().begin()) {
59 // If the CastInst is the insert point, change the insert point.
60 if (CI
== InsertPt
) ++InsertPt
;
61 // Splice the cast at the beginning of the entry block.
62 CI
->moveBefore(A
->getParent()->getEntryBlock().begin());
67 Instruction
*I
= CastInst::Create(opcode
, V
, Ty
, V
->getName(),
68 A
->getParent()->getEntryBlock().begin());
69 InsertedValues
.insert(I
);
73 Instruction
*I
= cast
<Instruction
>(V
);
75 // Check to see if there is already a cast. If there is, use it.
76 for (Value::use_iterator UI
= I
->use_begin(), E
= I
->use_end();
78 if ((*UI
)->getType() == Ty
)
79 if (CastInst
*CI
= dyn_cast
<CastInst
>(cast
<Instruction
>(*UI
)))
80 if (CI
->getOpcode() == opcode
) {
81 BasicBlock::iterator It
= I
; ++It
;
82 if (isa
<InvokeInst
>(I
))
83 It
= cast
<InvokeInst
>(I
)->getNormalDest()->begin();
84 while (isa
<PHINode
>(It
)) ++It
;
85 if (It
!= BasicBlock::iterator(CI
)) {
86 // If the CastInst is the insert point, change the insert point.
87 if (CI
== InsertPt
) ++InsertPt
;
88 // Splice the cast immediately after the operand in question.
94 BasicBlock::iterator IP
= I
; ++IP
;
95 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(I
))
96 IP
= II
->getNormalDest()->begin();
97 while (isa
<PHINode
>(IP
)) ++IP
;
98 Instruction
*CI
= CastInst::Create(opcode
, V
, Ty
, V
->getName(), IP
);
99 InsertedValues
.insert(CI
);
103 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
104 /// which must be possible with a noop cast.
105 Value
*SCEVExpander::InsertNoopCastOfTo(Value
*V
, const Type
*Ty
) {
106 Instruction::CastOps Op
= CastInst::getCastOpcode(V
, false, Ty
, false);
107 assert((Op
== Instruction::BitCast
||
108 Op
== Instruction::PtrToInt
||
109 Op
== Instruction::IntToPtr
) &&
110 "InsertNoopCastOfTo cannot perform non-noop casts!");
111 assert(SE
.getTypeSizeInBits(V
->getType()) == SE
.getTypeSizeInBits(Ty
) &&
112 "InsertNoopCastOfTo cannot change sizes!");
113 return InsertCastOfTo(Op
, V
, Ty
);
116 /// InsertBinop - Insert the specified binary operator, doing a small amount
117 /// of work to avoid inserting an obviously redundant operation.
118 Value
*SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode
, Value
*LHS
,
119 Value
*RHS
, BasicBlock::iterator InsertPt
) {
120 // Fold a binop with constant operands.
121 if (Constant
*CLHS
= dyn_cast
<Constant
>(LHS
))
122 if (Constant
*CRHS
= dyn_cast
<Constant
>(RHS
))
123 return ConstantExpr::get(Opcode
, CLHS
, CRHS
);
125 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
126 unsigned ScanLimit
= 6;
127 BasicBlock::iterator BlockBegin
= InsertPt
->getParent()->begin();
128 if (InsertPt
!= BlockBegin
) {
129 // Scanning starts from the last instruction before InsertPt.
130 BasicBlock::iterator IP
= InsertPt
;
132 for (; ScanLimit
; --IP
, --ScanLimit
) {
133 if (BinaryOperator
*BinOp
= dyn_cast
<BinaryOperator
>(IP
))
134 if (BinOp
->getOpcode() == Opcode
&& BinOp
->getOperand(0) == LHS
&&
135 BinOp
->getOperand(1) == RHS
)
137 if (IP
== BlockBegin
) break;
141 // If we haven't found this binop, insert it.
142 Instruction
*BO
= BinaryOperator::Create(Opcode
, LHS
, RHS
, "tmp", InsertPt
);
143 InsertedValues
.insert(BO
);
147 Value
*SCEVExpander::visitAddExpr(const SCEVAddExpr
*S
) {
148 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
149 Value
*V
= expand(S
->getOperand(S
->getNumOperands()-1));
150 V
= InsertNoopCastOfTo(V
, Ty
);
152 // Emit a bunch of add instructions
153 for (int i
= S
->getNumOperands()-2; i
>= 0; --i
) {
154 Value
*W
= expand(S
->getOperand(i
));
155 W
= InsertNoopCastOfTo(W
, Ty
);
156 V
= InsertBinop(Instruction::Add
, V
, W
, InsertPt
);
161 Value
*SCEVExpander::visitMulExpr(const SCEVMulExpr
*S
) {
162 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
163 int FirstOp
= 0; // Set if we should emit a subtract.
164 if (const SCEVConstant
*SC
= dyn_cast
<SCEVConstant
>(S
->getOperand(0)))
165 if (SC
->getValue()->isAllOnesValue())
168 int i
= S
->getNumOperands()-2;
169 Value
*V
= expand(S
->getOperand(i
+1));
170 V
= InsertNoopCastOfTo(V
, Ty
);
172 // Emit a bunch of multiply instructions
173 for (; i
>= FirstOp
; --i
) {
174 Value
*W
= expand(S
->getOperand(i
));
175 W
= InsertNoopCastOfTo(W
, Ty
);
176 V
= InsertBinop(Instruction::Mul
, V
, W
, InsertPt
);
179 // -1 * ... ---> 0 - ...
181 V
= InsertBinop(Instruction::Sub
, Constant::getNullValue(Ty
), V
, InsertPt
);
185 Value
*SCEVExpander::visitUDivExpr(const SCEVUDivExpr
*S
) {
186 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
188 Value
*LHS
= expand(S
->getLHS());
189 LHS
= InsertNoopCastOfTo(LHS
, Ty
);
190 if (const SCEVConstant
*SC
= dyn_cast
<SCEVConstant
>(S
->getRHS())) {
191 const APInt
&RHS
= SC
->getValue()->getValue();
192 if (RHS
.isPowerOf2())
193 return InsertBinop(Instruction::LShr
, LHS
,
194 ConstantInt::get(Ty
, RHS
.logBase2()),
198 Value
*RHS
= expand(S
->getRHS());
199 RHS
= InsertNoopCastOfTo(RHS
, Ty
);
200 return InsertBinop(Instruction::UDiv
, LHS
, RHS
, InsertPt
);
203 Value
*SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr
*S
) {
204 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
205 const Loop
*L
= S
->getLoop();
207 // {X,+,F} --> X + {0,+,F}
208 if (!S
->getStart()->isZero()) {
209 Value
*Start
= expand(S
->getStart());
210 Start
= InsertNoopCastOfTo(Start
, Ty
);
211 std::vector
<SCEVHandle
> NewOps(S
->op_begin(), S
->op_end());
212 NewOps
[0] = SE
.getIntegerSCEV(0, Ty
);
213 Value
*Rest
= expand(SE
.getAddRecExpr(NewOps
, L
));
214 Rest
= InsertNoopCastOfTo(Rest
, Ty
);
216 // FIXME: look for an existing add to use.
217 return InsertBinop(Instruction::Add
, Rest
, Start
, InsertPt
);
220 // {0,+,1} --> Insert a canonical induction variable into the loop!
222 S
->getOperand(1) == SE
.getIntegerSCEV(1, Ty
)) {
223 // Create and insert the PHI node for the induction variable in the
225 BasicBlock
*Header
= L
->getHeader();
226 PHINode
*PN
= PHINode::Create(Ty
, "indvar", Header
->begin());
227 InsertedValues
.insert(PN
);
228 PN
->addIncoming(Constant::getNullValue(Ty
), L
->getLoopPreheader());
230 pred_iterator HPI
= pred_begin(Header
);
231 assert(HPI
!= pred_end(Header
) && "Loop with zero preds???");
232 if (!L
->contains(*HPI
)) ++HPI
;
233 assert(HPI
!= pred_end(Header
) && L
->contains(*HPI
) &&
234 "No backedge in loop?");
236 // Insert a unit add instruction right before the terminator corresponding
238 Constant
*One
= ConstantInt::get(Ty
, 1);
239 Instruction
*Add
= BinaryOperator::CreateAdd(PN
, One
, "indvar.next",
240 (*HPI
)->getTerminator());
241 InsertedValues
.insert(Add
);
243 pred_iterator PI
= pred_begin(Header
);
244 if (*PI
== L
->getLoopPreheader())
246 PN
->addIncoming(Add
, *PI
);
250 // Get the canonical induction variable I for this loop.
251 Value
*I
= getOrInsertCanonicalInductionVariable(L
, Ty
);
253 // If this is a simple linear addrec, emit it now as a special case.
254 if (S
->isAffine()) { // {0,+,F} --> i*F
255 Value
*F
= expand(S
->getOperand(1));
256 F
= InsertNoopCastOfTo(F
, Ty
);
258 // IF the step is by one, just return the inserted IV.
259 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(F
))
260 if (CI
->getValue() == 1)
263 // If the insert point is directly inside of the loop, emit the multiply at
264 // the insert point. Otherwise, L is a loop that is a parent of the insert
265 // point loop. If we can, move the multiply to the outer most loop that it
267 BasicBlock::iterator MulInsertPt
= getInsertionPoint();
268 Loop
*InsertPtLoop
= LI
.getLoopFor(MulInsertPt
->getParent());
269 if (InsertPtLoop
!= L
&& InsertPtLoop
&&
270 L
->contains(InsertPtLoop
->getHeader())) {
272 // If we cannot hoist the multiply out of this loop, don't.
273 if (!InsertPtLoop
->isLoopInvariant(F
)) break;
275 BasicBlock
*InsertPtLoopPH
= InsertPtLoop
->getLoopPreheader();
277 // If this loop hasn't got a preheader, we aren't able to hoist the
282 // Otherwise, move the insert point to the preheader.
283 MulInsertPt
= InsertPtLoopPH
->getTerminator();
284 InsertPtLoop
= InsertPtLoop
->getParentLoop();
285 } while (InsertPtLoop
!= L
);
288 return InsertBinop(Instruction::Mul
, I
, F
, MulInsertPt
);
291 // If this is a chain of recurrences, turn it into a closed form, using the
292 // folders, then expandCodeFor the closed form. This allows the folders to
293 // simplify the expression without having to build a bunch of special code
295 SCEVHandle IH
= SE
.getUnknown(I
); // Get I as a "symbolic" SCEV.
297 SCEVHandle V
= S
->evaluateAtIteration(IH
, SE
);
298 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
303 Value
*SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr
*S
) {
304 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
305 Value
*V
= expand(S
->getOperand());
306 V
= InsertNoopCastOfTo(V
, SE
.getEffectiveSCEVType(V
->getType()));
307 Instruction
*I
= new TruncInst(V
, Ty
, "tmp.", InsertPt
);
308 InsertedValues
.insert(I
);
312 Value
*SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr
*S
) {
313 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
314 Value
*V
= expand(S
->getOperand());
315 V
= InsertNoopCastOfTo(V
, SE
.getEffectiveSCEVType(V
->getType()));
316 Instruction
*I
= new ZExtInst(V
, Ty
, "tmp.", InsertPt
);
317 InsertedValues
.insert(I
);
321 Value
*SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr
*S
) {
322 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
323 Value
*V
= expand(S
->getOperand());
324 V
= InsertNoopCastOfTo(V
, SE
.getEffectiveSCEVType(V
->getType()));
325 Instruction
*I
= new SExtInst(V
, Ty
, "tmp.", InsertPt
);
326 InsertedValues
.insert(I
);
330 Value
*SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr
*S
) {
331 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
332 Value
*LHS
= expand(S
->getOperand(0));
333 LHS
= InsertNoopCastOfTo(LHS
, Ty
);
334 for (unsigned i
= 1; i
< S
->getNumOperands(); ++i
) {
335 Value
*RHS
= expand(S
->getOperand(i
));
336 RHS
= InsertNoopCastOfTo(RHS
, Ty
);
338 new ICmpInst(ICmpInst::ICMP_SGT
, LHS
, RHS
, "tmp", InsertPt
);
339 InsertedValues
.insert(ICmp
);
340 Instruction
*Sel
= SelectInst::Create(ICmp
, LHS
, RHS
, "smax", InsertPt
);
341 InsertedValues
.insert(Sel
);
347 Value
*SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr
*S
) {
348 const Type
*Ty
= SE
.getEffectiveSCEVType(S
->getType());
349 Value
*LHS
= expand(S
->getOperand(0));
350 LHS
= InsertNoopCastOfTo(LHS
, Ty
);
351 for (unsigned i
= 1; i
< S
->getNumOperands(); ++i
) {
352 Value
*RHS
= expand(S
->getOperand(i
));
353 RHS
= InsertNoopCastOfTo(RHS
, Ty
);
355 new ICmpInst(ICmpInst::ICMP_UGT
, LHS
, RHS
, "tmp", InsertPt
);
356 InsertedValues
.insert(ICmp
);
357 Instruction
*Sel
= SelectInst::Create(ICmp
, LHS
, RHS
, "umax", InsertPt
);
358 InsertedValues
.insert(Sel
);
364 Value
*SCEVExpander::expandCodeFor(SCEVHandle SH
, const Type
*Ty
) {
365 // Expand the code for this SCEV.
366 assert(SE
.getTypeSizeInBits(Ty
) == SE
.getTypeSizeInBits(SH
->getType()) &&
367 "non-trivial casts should be done with the SCEVs directly!");
368 Value
*V
= expand(SH
);
369 return InsertNoopCastOfTo(V
, Ty
);
372 Value
*SCEVExpander::expand(const SCEV
*S
) {
373 // Check to see if we already expanded this.
374 std::map
<SCEVHandle
, Value
*>::iterator I
= InsertedExpressions
.find(S
);
375 if (I
!= InsertedExpressions
.end())
379 InsertedExpressions
[S
] = V
;