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