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 | // |
| 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 | |
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) |
| 33 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 34 | // If the cast isn't the first instruction of the function, move it. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 35 | if (BasicBlock::iterator(CI) != |
| 36 | A->getParent()->getEntryBlock().begin()) { |
| 37 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 38 | } |
| 39 | return CI; |
| 40 | } |
| 41 | } |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 42 | return CastInst::create(opcode, V, Ty, V->getName(), |
| 43 | A->getParent()->getEntryBlock().begin()); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | Instruction *I = cast<Instruction>(V); |
| 47 | |
| 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(); |
| 50 | UI != E; ++UI) { |
| 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. |
| 59 | CI->moveBefore(It); |
| 60 | } |
| 61 | return CI; |
| 62 | } |
| 63 | } |
| 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; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 68 | return CastInst::create(opcode, V, Ty, V->getName(), IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame^] | 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 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 76 | unsigned ScanLimit = 6; |
| 77 | for (BasicBlock::iterator IP = InsertPt, E = InsertPt->getParent()->begin(); |
| 78 | ScanLimit; --IP, --ScanLimit) { |
| 79 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP)) |
| 80 | if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS && |
| 81 | BinOp->getOperand(1) == RHS) |
| 82 | return BinOp; |
| 83 | if (IP == E) break; |
| 84 | } |
| 85 | |
| 86 | // If we don't have |
| 87 | return BinaryOperator::create(Opcode, LHS, RHS, "tmp.", InsertPt); |
| 88 | } |
| 89 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 90 | Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { |
| 91 | const Type *Ty = S->getType(); |
| 92 | int FirstOp = 0; // Set if we should emit a subtract. |
| 93 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
| 94 | if (SC->getValue()->isAllOnesValue()) |
| 95 | FirstOp = 1; |
| 96 | |
| 97 | int i = S->getNumOperands()-2; |
| 98 | Value *V = expandInTy(S->getOperand(i+1), Ty); |
| 99 | |
| 100 | // Emit a bunch of multiply instructions |
| 101 | for (; i >= FirstOp; --i) |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame^] | 102 | V = InsertBinop(Instruction::Mul, V, expandInTy(S->getOperand(i), Ty), |
| 103 | InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 104 | // -1 * ... ---> 0 - ... |
| 105 | if (FirstOp == 1) |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame^] | 106 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(V->getType()), V, |
| 107 | InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 108 | return V; |
| 109 | } |
| 110 | |
| 111 | Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { |
| 112 | const Type *Ty = S->getType(); |
| 113 | const Loop *L = S->getLoop(); |
| 114 | // 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] | 115 | assert(Ty->isInteger() && "Cannot expand fp recurrences yet!"); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 116 | |
| 117 | // {X,+,F} --> X + {0,+,F} |
| 118 | if (!isa<SCEVConstant>(S->getStart()) || |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 119 | !cast<SCEVConstant>(S->getStart())->getValue()->isZero()) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 120 | Value *Start = expandInTy(S->getStart(), Ty); |
| 121 | std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); |
| 122 | NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty); |
| 123 | Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty); |
| 124 | |
| 125 | // FIXME: look for an existing add to use. |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame^] | 126 | return InsertBinop(Instruction::Add, Rest, Start, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 130 | if (S->getNumOperands() == 2 && |
| 131 | S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) { |
| 132 | // Create and insert the PHI node for the induction variable in the |
| 133 | // specified loop. |
| 134 | BasicBlock *Header = L->getHeader(); |
| 135 | PHINode *PN = new PHINode(Ty, "indvar", Header->begin()); |
| 136 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 137 | |
| 138 | pred_iterator HPI = pred_begin(Header); |
| 139 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 140 | if (!L->contains(*HPI)) ++HPI; |
| 141 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 142 | "No backedge in loop?"); |
| 143 | |
| 144 | // Insert a unit add instruction right before the terminator corresponding |
| 145 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 146 | Constant *One = ConstantInt::get(Ty, 1); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 147 | Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next", |
| 148 | (*HPI)->getTerminator()); |
| 149 | |
| 150 | pred_iterator PI = pred_begin(Header); |
| 151 | if (*PI == L->getLoopPreheader()) |
| 152 | ++PI; |
| 153 | PN->addIncoming(Add, *PI); |
| 154 | return PN; |
| 155 | } |
| 156 | |
| 157 | // Get the canonical induction variable I for this loop. |
| 158 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 159 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 160 | // 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] | 161 | if (S->getNumOperands() == 2) { // {0,+,F} --> i*F |
| 162 | Value *F = expandInTy(S->getOperand(1), Ty); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 163 | |
| 164 | // IF the step is by one, just return the inserted IV. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 165 | if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) |
Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 166 | if (CI->getValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 167 | return I; |
| 168 | |
| 169 | // If the insert point is directly inside of the loop, emit the multiply at |
| 170 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 171 | // point loop. If we can, move the multiply to the outer most loop that it |
| 172 | // is safe to be in. |
| 173 | Instruction *MulInsertPt = InsertPt; |
| 174 | Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent()); |
| 175 | if (InsertPtLoop != L && InsertPtLoop && |
| 176 | L->contains(InsertPtLoop->getHeader())) { |
| 177 | while (InsertPtLoop != L) { |
| 178 | // If we cannot hoist the multiply out of this loop, don't. |
| 179 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 180 | |
| 181 | // Otherwise, move the insert point to the preheader of the loop. |
| 182 | MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator(); |
| 183 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
| 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame^] | 187 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 191 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 192 | // simplify the expression without having to build a bunch of special code |
| 193 | // into this folder. |
| 194 | SCEVHandle IH = SCEVUnknown::get(I); // Get I as a "symbolic" SCEV. |
| 195 | |
| 196 | SCEVHandle V = S->evaluateAtIteration(IH); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 197 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 198 | |
| 199 | return expandInTy(V, Ty); |
| 200 | } |