1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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 // FIXME: keep track of the cast instruction.
25 if (Constant
*C
= dyn_cast
<Constant
>(V
))
26 return ConstantExpr::getCast(opcode
, C
, Ty
);
28 if (Argument
*A
= dyn_cast
<Argument
>(V
)) {
29 // Check to see if there is already a cast!
30 for (Value::use_iterator UI
= A
->use_begin(), E
= A
->use_end();
32 if ((*UI
)->getType() == Ty
)
33 if (CastInst
*CI
= dyn_cast
<CastInst
>(cast
<Instruction
>(*UI
))) {
34 // If the cast isn't the first instruction of the function, move it.
35 if (BasicBlock::iterator(CI
) !=
36 A
->getParent()->getEntryBlock().begin()) {
37 CI
->moveBefore(A
->getParent()->getEntryBlock().begin());
42 return CastInst::create(opcode
, V
, Ty
, V
->getName(),
43 A
->getParent()->getEntryBlock().begin());
46 Instruction
*I
= cast
<Instruction
>(V
);
48 // Check to see if there is already a cast. If there is, use it.
49 for (Value::use_iterator UI
= I
->use_begin(), E
= I
->use_end();
51 if ((*UI
)->getType() == Ty
)
52 if (CastInst
*CI
= dyn_cast
<CastInst
>(cast
<Instruction
>(*UI
))) {
53 BasicBlock::iterator It
= I
; ++It
;
54 if (isa
<InvokeInst
>(I
))
55 It
= cast
<InvokeInst
>(I
)->getNormalDest()->begin();
56 while (isa
<PHINode
>(It
)) ++It
;
57 if (It
!= BasicBlock::iterator(CI
)) {
58 // Splice the cast immediately after the operand in question.
64 BasicBlock::iterator IP
= I
; ++IP
;
65 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(I
))
66 IP
= II
->getNormalDest()->begin();
67 while (isa
<PHINode
>(IP
)) ++IP
;
68 return CastInst::create(opcode
, V
, Ty
, V
->getName(), IP
);
71 /// InsertBinop - Insert the specified binary operator, doing a small amount
72 /// of work to avoid inserting an obviously redundant operation.
73 Value
*SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode
, Value
*LHS
,
74 Value
*RHS
, Instruction
*&InsertPt
) {
75 // Fold a binop with constant operands.
76 if (Constant
*CLHS
= dyn_cast
<Constant
>(LHS
))
77 if (Constant
*CRHS
= dyn_cast
<Constant
>(RHS
))
78 return ConstantExpr::get(Opcode
, CLHS
, CRHS
);
80 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
81 unsigned ScanLimit
= 6;
82 for (BasicBlock::iterator IP
= InsertPt
, E
= InsertPt
->getParent()->begin();
83 ScanLimit
; --IP
, --ScanLimit
) {
84 if (BinaryOperator
*BinOp
= dyn_cast
<BinaryOperator
>(IP
))
85 if (BinOp
->getOpcode() == Opcode
&& BinOp
->getOperand(0) == LHS
&&
86 BinOp
->getOperand(1) == RHS
) {
87 // If we found the instruction *at* the insert point, insert later
88 // instructions after it.
89 if (BinOp
== InsertPt
)
97 return BinaryOperator::create(Opcode
, LHS
, RHS
, "tmp.", InsertPt
);
100 Value
*SCEVExpander::visitMulExpr(SCEVMulExpr
*S
) {
101 int FirstOp
= 0; // Set if we should emit a subtract.
102 if (SCEVConstant
*SC
= dyn_cast
<SCEVConstant
>(S
->getOperand(0)))
103 if (SC
->getValue()->isAllOnesValue())
106 int i
= S
->getNumOperands()-2;
107 Value
*V
= expand(S
->getOperand(i
+1));
109 // Emit a bunch of multiply instructions
110 for (; i
>= FirstOp
; --i
)
111 V
= InsertBinop(Instruction::Mul
, V
, expand(S
->getOperand(i
)),
113 // -1 * ... ---> 0 - ...
115 V
= InsertBinop(Instruction::Sub
, Constant::getNullValue(V
->getType()), V
,
120 Value
*SCEVExpander::visitAddRecExpr(SCEVAddRecExpr
*S
) {
121 const Type
*Ty
= S
->getType();
122 const Loop
*L
= S
->getLoop();
123 // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
124 assert(Ty
->isInteger() && "Cannot expand fp recurrences yet!");
126 // {X,+,F} --> X + {0,+,F}
127 if (!isa
<SCEVConstant
>(S
->getStart()) ||
128 !cast
<SCEVConstant
>(S
->getStart())->getValue()->isZero()) {
129 Value
*Start
= expand(S
->getStart());
130 std::vector
<SCEVHandle
> NewOps(S
->op_begin(), S
->op_end());
131 NewOps
[0] = SCEVUnknown::getIntegerSCEV(0, Ty
);
132 Value
*Rest
= expand(SCEVAddRecExpr::get(NewOps
, L
));
134 // FIXME: look for an existing add to use.
135 return InsertBinop(Instruction::Add
, Rest
, Start
, InsertPt
);
138 // {0,+,1} --> Insert a canonical induction variable into the loop!
139 if (S
->getNumOperands() == 2 &&
140 S
->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty
)) {
141 // Create and insert the PHI node for the induction variable in the
143 BasicBlock
*Header
= L
->getHeader();
144 PHINode
*PN
= new PHINode(Ty
, "indvar", Header
->begin());
145 PN
->addIncoming(Constant::getNullValue(Ty
), L
->getLoopPreheader());
147 pred_iterator HPI
= pred_begin(Header
);
148 assert(HPI
!= pred_end(Header
) && "Loop with zero preds???");
149 if (!L
->contains(*HPI
)) ++HPI
;
150 assert(HPI
!= pred_end(Header
) && L
->contains(*HPI
) &&
151 "No backedge in loop?");
153 // Insert a unit add instruction right before the terminator corresponding
155 Constant
*One
= ConstantInt::get(Ty
, 1);
156 Instruction
*Add
= BinaryOperator::createAdd(PN
, One
, "indvar.next",
157 (*HPI
)->getTerminator());
159 pred_iterator PI
= pred_begin(Header
);
160 if (*PI
== L
->getLoopPreheader())
162 PN
->addIncoming(Add
, *PI
);
166 // Get the canonical induction variable I for this loop.
167 Value
*I
= getOrInsertCanonicalInductionVariable(L
, Ty
);
169 // If this is a simple linear addrec, emit it now as a special case.
170 if (S
->getNumOperands() == 2) { // {0,+,F} --> i*F
171 Value
*F
= expand(S
->getOperand(1));
173 // IF the step is by one, just return the inserted IV.
174 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(F
))
175 if (CI
->getValue() == 1)
178 // If the insert point is directly inside of the loop, emit the multiply at
179 // the insert point. Otherwise, L is a loop that is a parent of the insert
180 // point loop. If we can, move the multiply to the outer most loop that it
182 Instruction
*MulInsertPt
= InsertPt
;
183 Loop
*InsertPtLoop
= LI
.getLoopFor(MulInsertPt
->getParent());
184 if (InsertPtLoop
!= L
&& InsertPtLoop
&&
185 L
->contains(InsertPtLoop
->getHeader())) {
186 while (InsertPtLoop
!= L
) {
187 // If we cannot hoist the multiply out of this loop, don't.
188 if (!InsertPtLoop
->isLoopInvariant(F
)) break;
190 // Otherwise, move the insert point to the preheader of the loop.
191 MulInsertPt
= InsertPtLoop
->getLoopPreheader()->getTerminator();
192 InsertPtLoop
= InsertPtLoop
->getParentLoop();
196 return InsertBinop(Instruction::Mul
, I
, F
, MulInsertPt
);
199 // If this is a chain of recurrences, turn it into a closed form, using the
200 // folders, then expandCodeFor the closed form. This allows the folders to
201 // simplify the expression without having to build a bunch of special code
203 SCEVHandle IH
= SCEVUnknown::get(I
); // Get I as a "symbolic" SCEV.
205 SCEVHandle V
= S
->evaluateAtIteration(IH
);
206 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";