Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 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 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 20 | /// InsertCastOfTo - Insert a cast of V to the specified type, doing what |
| 21 | /// we can to share the casts. |
Reid Spencer | 3ba68b9 | 2006-12-13 08:06:42 +0000 | [diff] [blame] | 22 | Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, |
| 23 | const Type *Ty) { |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 24 | // FIXME: keep track of the cast instruction. |
| 25 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 26 | return ConstantExpr::getCast(opcode, C, Ty); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 27 | |
| 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(); |
| 31 | UI != E; ++UI) { |
| 32 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 33 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 34 | if (CI->getOpcode() == opcode) { |
| 35 | // If the cast isn't the first instruction of the function, move it. |
| 36 | if (BasicBlock::iterator(CI) != |
| 37 | A->getParent()->getEntryBlock().begin()) { |
| 38 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 39 | } |
| 40 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 41 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 42 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 43 | return CastInst::Create(opcode, V, Ty, V->getName(), |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 44 | A->getParent()->getEntryBlock().begin()); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 45 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 46 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 47 | Instruction *I = cast<Instruction>(V); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 48 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 49 | // Check to see if there is already a cast. If there is, use it. |
| 50 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 51 | UI != E; ++UI) { |
| 52 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 53 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 54 | if (CI->getOpcode() == opcode) { |
| 55 | BasicBlock::iterator It = I; ++It; |
| 56 | if (isa<InvokeInst>(I)) |
| 57 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
| 58 | while (isa<PHINode>(It)) ++It; |
| 59 | if (It != BasicBlock::iterator(CI)) { |
| 60 | // Splice the cast immediately after the operand in question. |
| 61 | CI->moveBefore(It); |
| 62 | } |
| 63 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 65 | } |
| 66 | BasicBlock::iterator IP = I; ++IP; |
| 67 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 68 | IP = II->getNormalDest()->begin(); |
| 69 | while (isa<PHINode>(IP)) ++IP; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 70 | return CastInst::Create(opcode, V, Ty, V->getName(), IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 73 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 74 | /// of work to avoid inserting an obviously redundant operation. |
| 75 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 76 | Value *RHS, Instruction *InsertPt) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 77 | // Fold a binop with constant operands. |
| 78 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 79 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 80 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
| 81 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 82 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 83 | unsigned ScanLimit = 6; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 84 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 85 | if (InsertPt != BlockBegin) { |
| 86 | // Scanning starts from the last instruction before InsertPt. |
| 87 | BasicBlock::iterator IP = InsertPt; |
| 88 | --IP; |
| 89 | for (; ScanLimit; --IP, --ScanLimit) { |
| 90 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP)) |
| 91 | if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS && |
| 92 | BinOp->getOperand(1) == RHS) |
| 93 | return BinOp; |
| 94 | if (IP == BlockBegin) break; |
| 95 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 96 | } |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 97 | |
| 98 | // If we haven't found this binop, insert it. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 99 | return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt); |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame^] | 102 | Value *SCEVExpander::visitAddExpr(SCEVAddExpr *S) { |
| 103 | Value *V = expand(S->getOperand(S->getNumOperands()-1)); |
| 104 | |
| 105 | // Emit a bunch of add instructions |
| 106 | for (int i = S->getNumOperands()-2; i >= 0; --i) |
| 107 | V = InsertBinop(Instruction::Add, V, expand(S->getOperand(i)), |
| 108 | InsertPt); |
| 109 | return V; |
| 110 | } |
| 111 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 112 | Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 113 | int FirstOp = 0; // Set if we should emit a subtract. |
| 114 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
| 115 | if (SC->getValue()->isAllOnesValue()) |
| 116 | FirstOp = 1; |
| 117 | |
| 118 | int i = S->getNumOperands()-2; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 119 | Value *V = expand(S->getOperand(i+1)); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 120 | |
| 121 | // Emit a bunch of multiply instructions |
| 122 | for (; i >= FirstOp; --i) |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 123 | V = InsertBinop(Instruction::Mul, V, expand(S->getOperand(i)), |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 124 | InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 125 | // -1 * ... ---> 0 - ... |
| 126 | if (FirstOp == 1) |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 127 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(V->getType()), V, |
| 128 | InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 129 | return V; |
| 130 | } |
| 131 | |
| 132 | Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { |
| 133 | const Type *Ty = S->getType(); |
| 134 | const Loop *L = S->getLoop(); |
| 135 | // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 136 | assert(Ty->isInteger() && "Cannot expand fp recurrences yet!"); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 137 | |
| 138 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 139 | if (!S->getStart()->isZero()) { |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 140 | Value *Start = expand(S->getStart()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 141 | std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 142 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
| 143 | Value *Rest = expand(SE.getAddRecExpr(NewOps, L)); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 144 | |
| 145 | // FIXME: look for an existing add to use. |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 146 | return InsertBinop(Instruction::Add, Rest, Start, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 150 | if (S->getNumOperands() == 2 && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 151 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 152 | // Create and insert the PHI node for the induction variable in the |
| 153 | // specified loop. |
| 154 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 155 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 156 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 157 | |
| 158 | pred_iterator HPI = pred_begin(Header); |
| 159 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 160 | if (!L->contains(*HPI)) ++HPI; |
| 161 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 162 | "No backedge in loop?"); |
| 163 | |
| 164 | // Insert a unit add instruction right before the terminator corresponding |
| 165 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 166 | Constant *One = ConstantInt::get(Ty, 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 167 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 168 | (*HPI)->getTerminator()); |
| 169 | |
| 170 | pred_iterator PI = pred_begin(Header); |
| 171 | if (*PI == L->getLoopPreheader()) |
| 172 | ++PI; |
| 173 | PN->addIncoming(Add, *PI); |
| 174 | return PN; |
| 175 | } |
| 176 | |
| 177 | // Get the canonical induction variable I for this loop. |
| 178 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 179 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 180 | // If this is a simple linear addrec, emit it now as a special case. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 181 | if (S->getNumOperands() == 2) { // {0,+,F} --> i*F |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 182 | Value *F = expand(S->getOperand(1)); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 183 | |
| 184 | // IF the step is by one, just return the inserted IV. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 185 | if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) |
Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 186 | if (CI->getValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 187 | return I; |
| 188 | |
| 189 | // If the insert point is directly inside of the loop, emit the multiply at |
| 190 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 191 | // point loop. If we can, move the multiply to the outer most loop that it |
| 192 | // is safe to be in. |
| 193 | Instruction *MulInsertPt = InsertPt; |
| 194 | Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent()); |
| 195 | if (InsertPtLoop != L && InsertPtLoop && |
| 196 | L->contains(InsertPtLoop->getHeader())) { |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 197 | do { |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 198 | // If we cannot hoist the multiply out of this loop, don't. |
| 199 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 200 | |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 201 | BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); |
| 202 | |
| 203 | // If this loop hasn't got a preheader, we aren't able to hoist the |
| 204 | // multiply. |
| 205 | if (!InsertPtLoopPH) |
| 206 | break; |
| 207 | |
| 208 | // Otherwise, move the insert point to the preheader. |
| 209 | MulInsertPt = InsertPtLoopPH->getTerminator(); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 210 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 211 | } while (InsertPtLoop != L); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 214 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 218 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 219 | // simplify the expression without having to build a bunch of special code |
| 220 | // into this folder. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 221 | SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 222 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 223 | SCEVHandle V = S->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 224 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 225 | |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 226 | return expand(V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 227 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 228 | |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 229 | Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) { |
| 230 | Value *LHS = expand(S->getOperand(0)); |
| 231 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 232 | Value *RHS = expand(S->getOperand(i)); |
| 233 | Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 234 | LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 235 | } |
| 236 | return LHS; |
| 237 | } |
| 238 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 239 | Value *SCEVExpander::visitUMaxExpr(SCEVUMaxExpr *S) { |
| 240 | Value *LHS = expand(S->getOperand(0)); |
| 241 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 242 | Value *RHS = expand(S->getOperand(i)); |
| 243 | Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 244 | LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 245 | } |
| 246 | return LHS; |
| 247 | } |
| 248 | |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 249 | Value *SCEVExpander::expand(SCEV *S) { |
| 250 | // Check to see if we already expanded this. |
| 251 | std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S); |
| 252 | if (I != InsertedExpressions.end()) |
| 253 | return I->second; |
| 254 | |
| 255 | Value *V = visit(S); |
| 256 | InsertedExpressions[S] = V; |
| 257 | return V; |
| 258 | } |