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" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 21 | /// InsertCastOfTo - Insert a cast of V to the specified type, doing what |
| 22 | /// we can to share the casts. |
Reid Spencer | 3ba68b9 | 2006-12-13 08:06:42 +0000 | [diff] [blame] | 23 | Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, |
| 24 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 25 | // Short-circuit unnecessary bitcasts. |
| 26 | if (opcode == Instruction::BitCast && V->getType() == Ty) |
| 27 | return V; |
| 28 | |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 29 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
| 30 | if (opcode == Instruction::PtrToInt && Ty == TD.getIntPtrType()) |
| 31 | if (IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(V)) |
| 32 | return ITP->getOperand(0); |
| 33 | if (opcode == Instruction::IntToPtr && V->getType() == TD.getIntPtrType()) |
| 34 | if (PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(V)) |
| 35 | return PTI->getOperand(0); |
| 36 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 37 | // FIXME: keep track of the cast instruction. |
| 38 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 39 | return ConstantExpr::getCast(opcode, C, Ty); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 40 | |
| 41 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 42 | // Check to see if there is already a cast! |
| 43 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); |
| 44 | UI != E; ++UI) { |
| 45 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 46 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 47 | if (CI->getOpcode() == opcode) { |
| 48 | // If the cast isn't the first instruction of the function, move it. |
| 49 | if (BasicBlock::iterator(CI) != |
| 50 | A->getParent()->getEntryBlock().begin()) { |
| 51 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 52 | } |
| 53 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 55 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 56 | return CastInst::Create(opcode, V, Ty, V->getName(), |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 57 | A->getParent()->getEntryBlock().begin()); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 58 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 60 | Instruction *I = cast<Instruction>(V); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 61 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 62 | // Check to see if there is already a cast. If there is, use it. |
| 63 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 64 | UI != E; ++UI) { |
| 65 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 66 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 67 | if (CI->getOpcode() == opcode) { |
| 68 | BasicBlock::iterator It = I; ++It; |
| 69 | if (isa<InvokeInst>(I)) |
| 70 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
| 71 | while (isa<PHINode>(It)) ++It; |
| 72 | if (It != BasicBlock::iterator(CI)) { |
| 73 | // Splice the cast immediately after the operand in question. |
| 74 | CI->moveBefore(It); |
| 75 | } |
| 76 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 77 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 78 | } |
| 79 | BasicBlock::iterator IP = I; ++IP; |
| 80 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 81 | IP = II->getNormalDest()->begin(); |
| 82 | while (isa<PHINode>(IP)) ++IP; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 83 | return CastInst::Create(opcode, V, Ty, V->getName(), IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 86 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 87 | /// of work to avoid inserting an obviously redundant operation. |
| 88 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 89 | Value *RHS, Instruction *InsertPt) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 90 | // Fold a binop with constant operands. |
| 91 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 92 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 93 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
| 94 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 95 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 96 | unsigned ScanLimit = 6; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 97 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 98 | if (InsertPt != BlockBegin) { |
| 99 | // Scanning starts from the last instruction before InsertPt. |
| 100 | BasicBlock::iterator IP = InsertPt; |
| 101 | --IP; |
| 102 | for (; ScanLimit; --IP, --ScanLimit) { |
| 103 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP)) |
| 104 | if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS && |
| 105 | BinOp->getOperand(1) == RHS) |
| 106 | return BinOp; |
| 107 | if (IP == BlockBegin) break; |
| 108 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 109 | } |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 110 | |
| 111 | // If we haven't found this binop, insert it. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 112 | return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt); |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 115 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 116 | const Type *Ty = S->getType(); |
| 117 | if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 118 | Value *V = expand(S->getOperand(S->getNumOperands()-1)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 119 | V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 120 | |
| 121 | // Emit a bunch of add instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 122 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 123 | Value *W = expand(S->getOperand(i)); |
| 124 | W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty); |
| 125 | V = InsertBinop(Instruction::Add, V, W, InsertPt); |
| 126 | } |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 127 | return V; |
| 128 | } |
| 129 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 130 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 131 | const Type *Ty = S->getType(); |
| 132 | if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 133 | int FirstOp = 0; // Set if we should emit a subtract. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 134 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 135 | if (SC->getValue()->isAllOnesValue()) |
| 136 | FirstOp = 1; |
| 137 | |
| 138 | int i = S->getNumOperands()-2; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 139 | Value *V = expand(S->getOperand(i+1)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 140 | V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 141 | |
| 142 | // Emit a bunch of multiply instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 143 | for (; i >= FirstOp; --i) { |
| 144 | Value *W = expand(S->getOperand(i)); |
| 145 | W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty); |
| 146 | V = InsertBinop(Instruction::Mul, V, W, InsertPt); |
| 147 | } |
| 148 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 149 | // -1 * ... ---> 0 - ... |
| 150 | if (FirstOp == 1) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 151 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 152 | return V; |
| 153 | } |
| 154 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 155 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 156 | const Type *Ty = S->getType(); |
| 157 | if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); |
| 158 | |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 159 | Value *LHS = expand(S->getLHS()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 160 | LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 161 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 162 | const APInt &RHS = SC->getValue()->getValue(); |
| 163 | if (RHS.isPowerOf2()) |
| 164 | return InsertBinop(Instruction::LShr, LHS, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 165 | ConstantInt::get(Ty, RHS.logBase2()), |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 166 | InsertPt); |
| 167 | } |
| 168 | |
| 169 | Value *RHS = expand(S->getRHS()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 170 | RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), RHS, Ty); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 171 | return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); |
| 172 | } |
| 173 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 174 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 175 | const Type *Ty = S->getType(); |
| 176 | const Loop *L = S->getLoop(); |
| 177 | // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 178 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
| 179 | "Cannot expand fp recurrences yet!"); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 180 | |
| 181 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 182 | if (!S->getStart()->isZero()) { |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 183 | Value *Start = expand(S->getStart()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 184 | if (isa<PointerType>(Start->getType())) |
| 185 | Start = InsertCastOfTo(Instruction::PtrToInt, Start, TD.getIntPtrType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 186 | std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 187 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
| 188 | Value *Rest = expand(SE.getAddRecExpr(NewOps, L)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 189 | if (isa<PointerType>(Rest->getType())) |
| 190 | Rest = InsertCastOfTo(Instruction::PtrToInt, Rest, TD.getIntPtrType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 191 | |
| 192 | // FIXME: look for an existing add to use. |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 193 | return InsertBinop(Instruction::Add, Rest, Start, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 197 | if (S->isAffine() && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 198 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 199 | // Create and insert the PHI node for the induction variable in the |
| 200 | // specified loop. |
| 201 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 202 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 203 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 204 | |
| 205 | pred_iterator HPI = pred_begin(Header); |
| 206 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 207 | if (!L->contains(*HPI)) ++HPI; |
| 208 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 209 | "No backedge in loop?"); |
| 210 | |
| 211 | // Insert a unit add instruction right before the terminator corresponding |
| 212 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 213 | Constant *One = ConstantInt::get(Ty, 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 214 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 215 | (*HPI)->getTerminator()); |
| 216 | |
| 217 | pred_iterator PI = pred_begin(Header); |
| 218 | if (*PI == L->getLoopPreheader()) |
| 219 | ++PI; |
| 220 | PN->addIncoming(Add, *PI); |
| 221 | return PN; |
| 222 | } |
| 223 | |
| 224 | // Get the canonical induction variable I for this loop. |
| 225 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 226 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 227 | // If this is a simple linear addrec, emit it now as a special case. |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 228 | if (S->isAffine()) { // {0,+,F} --> i*F |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 229 | Value *F = expand(S->getOperand(1)); |
Dan Gohman | 4d17759 | 2009-04-16 21:34:54 +0000 | [diff] [blame] | 230 | if (isa<PointerType>(F->getType())) |
| 231 | F = InsertCastOfTo(Instruction::PtrToInt, F, TD.getIntPtrType()); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 232 | |
| 233 | // IF the step is by one, just return the inserted IV. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 234 | if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) |
Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 235 | if (CI->getValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 236 | return I; |
| 237 | |
| 238 | // If the insert point is directly inside of the loop, emit the multiply at |
| 239 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 240 | // point loop. If we can, move the multiply to the outer most loop that it |
| 241 | // is safe to be in. |
| 242 | Instruction *MulInsertPt = InsertPt; |
| 243 | Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent()); |
| 244 | if (InsertPtLoop != L && InsertPtLoop && |
| 245 | L->contains(InsertPtLoop->getHeader())) { |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 246 | do { |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 247 | // If we cannot hoist the multiply out of this loop, don't. |
| 248 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 249 | |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 250 | BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); |
| 251 | |
| 252 | // If this loop hasn't got a preheader, we aren't able to hoist the |
| 253 | // multiply. |
| 254 | if (!InsertPtLoopPH) |
| 255 | break; |
| 256 | |
| 257 | // Otherwise, move the insert point to the preheader. |
| 258 | MulInsertPt = InsertPtLoopPH->getTerminator(); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 259 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 260 | } while (InsertPtLoop != L); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 263 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 267 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 268 | // simplify the expression without having to build a bunch of special code |
| 269 | // into this folder. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 270 | SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 271 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 272 | SCEVHandle V = S->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 273 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 274 | |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 275 | return expand(V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 276 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 277 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 278 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 279 | Value *V = expand(S->getOperand()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 280 | if (isa<PointerType>(V->getType())) |
| 281 | V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); |
Gabor Greif | e9324f3 | 2008-10-13 10:21:17 +0000 | [diff] [blame] | 282 | return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 285 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 286 | const Type *Ty = S->getType(); |
| 287 | if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 288 | Value *V = expand(S->getOperand()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 289 | if (isa<PointerType>(V->getType())) |
| 290 | V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 291 | return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 294 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 295 | const Type *Ty = S->getType(); |
| 296 | if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 297 | Value *V = expand(S->getOperand()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 298 | if (isa<PointerType>(V->getType())) |
| 299 | V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 300 | return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 303 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 304 | const Type *Ty = S->getType(); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 305 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 306 | LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 307 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 308 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 309 | RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), |
| 310 | RHS, Ty); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 311 | Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 312 | LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 313 | } |
| 314 | return LHS; |
| 315 | } |
| 316 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 317 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 318 | const Type *Ty = S->getType(); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 319 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 320 | LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 321 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 322 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | 6524d3a | 2009-04-16 16:15:25 +0000 | [diff] [blame] | 323 | RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), |
| 324 | RHS, Ty); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 325 | Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 326 | LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 327 | } |
| 328 | return LHS; |
| 329 | } |
| 330 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 331 | Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty, |
| 332 | Instruction *IP) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 333 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 334 | assert(TD.getTypeSizeInBits(Ty) == TD.getTypeSizeInBits(SH->getType()) && |
| 335 | "non-trivial casts should be done with the SCEVs directly!"); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 336 | this->InsertPt = IP; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 337 | Value *V = expand(SH); |
| 338 | return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 341 | Value *SCEVExpander::expand(const SCEV *S) { |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 342 | // Check to see if we already expanded this. |
| 343 | std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S); |
| 344 | if (I != InsertedExpressions.end()) |
| 345 | return I->second; |
| 346 | |
| 347 | Value *V = visit(S); |
| 348 | InsertedExpressions[S] = V; |
| 349 | return V; |
| 350 | } |