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 | |
| 16 | #include "llvm/Analysis/LoopInfo.h" |
| 17 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 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. |
| 22 | Value *SCEVExpander::InsertCastOfTo(Value *V, const Type *Ty) { |
| 23 | // FIXME: keep track of the cast instruction. |
| 24 | if (Constant *C = dyn_cast<Constant>(V)) |
| 25 | return ConstantExpr::getCast(C, Ty); |
| 26 | |
| 27 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 28 | // Check to see if there is already a cast! |
| 29 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); |
| 30 | UI != E; ++UI) { |
| 31 | if ((*UI)->getType() == Ty) |
| 32 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) { |
| 33 | // If the cast isn't in the first instruction of the function, |
| 34 | // move it. |
| 35 | if (BasicBlock::iterator(CI) != |
| 36 | A->getParent()->getEntryBlock().begin()) { |
| 37 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 38 | } |
| 39 | return CI; |
| 40 | } |
| 41 | } |
| 42 | return new CastInst(V, Ty, V->getName(), |
| 43 | A->getParent()->getEntryBlock().begin()); |
| 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; |
| 68 | return new CastInst(V, Ty, V->getName(), IP); |
| 69 | } |
| 70 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 71 | Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { |
| 72 | const Type *Ty = S->getType(); |
| 73 | int FirstOp = 0; // Set if we should emit a subtract. |
| 74 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
| 75 | if (SC->getValue()->isAllOnesValue()) |
| 76 | FirstOp = 1; |
| 77 | |
| 78 | int i = S->getNumOperands()-2; |
| 79 | Value *V = expandInTy(S->getOperand(i+1), Ty); |
| 80 | |
| 81 | // Emit a bunch of multiply instructions |
| 82 | for (; i >= FirstOp; --i) |
| 83 | V = BinaryOperator::createMul(V, expandInTy(S->getOperand(i), Ty), |
| 84 | "tmp.", InsertPt); |
| 85 | // -1 * ... ---> 0 - ... |
| 86 | if (FirstOp == 1) |
| 87 | V = BinaryOperator::createNeg(V, "tmp.", InsertPt); |
| 88 | return V; |
| 89 | } |
| 90 | |
| 91 | Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { |
| 92 | const Type *Ty = S->getType(); |
| 93 | const Loop *L = S->getLoop(); |
| 94 | // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} |
| 95 | assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!"); |
| 96 | |
| 97 | // {X,+,F} --> X + {0,+,F} |
| 98 | if (!isa<SCEVConstant>(S->getStart()) || |
| 99 | !cast<SCEVConstant>(S->getStart())->getValue()->isNullValue()) { |
| 100 | Value *Start = expandInTy(S->getStart(), Ty); |
| 101 | std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); |
| 102 | NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty); |
| 103 | Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty); |
| 104 | |
| 105 | // FIXME: look for an existing add to use. |
| 106 | return BinaryOperator::createAdd(Rest, Start, "tmp.", InsertPt); |
| 107 | } |
| 108 | |
| 109 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 110 | if (S->getNumOperands() == 2 && |
| 111 | S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) { |
| 112 | // Create and insert the PHI node for the induction variable in the |
| 113 | // specified loop. |
| 114 | BasicBlock *Header = L->getHeader(); |
| 115 | PHINode *PN = new PHINode(Ty, "indvar", Header->begin()); |
| 116 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 117 | |
| 118 | pred_iterator HPI = pred_begin(Header); |
| 119 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 120 | if (!L->contains(*HPI)) ++HPI; |
| 121 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 122 | "No backedge in loop?"); |
| 123 | |
| 124 | // Insert a unit add instruction right before the terminator corresponding |
| 125 | // to the back-edge. |
| 126 | Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0) |
| 127 | : ConstantInt::get(Ty, 1); |
| 128 | Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next", |
| 129 | (*HPI)->getTerminator()); |
| 130 | |
| 131 | pred_iterator PI = pred_begin(Header); |
| 132 | if (*PI == L->getLoopPreheader()) |
| 133 | ++PI; |
| 134 | PN->addIncoming(Add, *PI); |
| 135 | return PN; |
| 136 | } |
| 137 | |
| 138 | // Get the canonical induction variable I for this loop. |
| 139 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 140 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 141 | // 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] | 142 | if (S->getNumOperands() == 2) { // {0,+,F} --> i*F |
| 143 | Value *F = expandInTy(S->getOperand(1), Ty); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 144 | |
| 145 | // IF the step is by one, just return the inserted IV. |
| 146 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(F)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame^] | 147 | if (CI->getZExtValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 148 | return I; |
| 149 | |
| 150 | // If the insert point is directly inside of the loop, emit the multiply at |
| 151 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 152 | // point loop. If we can, move the multiply to the outer most loop that it |
| 153 | // is safe to be in. |
| 154 | Instruction *MulInsertPt = InsertPt; |
| 155 | Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent()); |
| 156 | if (InsertPtLoop != L && InsertPtLoop && |
| 157 | L->contains(InsertPtLoop->getHeader())) { |
| 158 | while (InsertPtLoop != L) { |
| 159 | // If we cannot hoist the multiply out of this loop, don't. |
| 160 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 161 | |
| 162 | // Otherwise, move the insert point to the preheader of the loop. |
| 163 | MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator(); |
| 164 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 172 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 173 | // simplify the expression without having to build a bunch of special code |
| 174 | // into this folder. |
| 175 | SCEVHandle IH = SCEVUnknown::get(I); // Get I as a "symbolic" SCEV. |
| 176 | |
| 177 | SCEVHandle V = S->evaluateAtIteration(IH); |
| 178 | //std::cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
| 179 | |
| 180 | return expandInTy(V, Ty); |
| 181 | } |