| 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 | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 115 | Value *SCEVExpander::visitAddExpr(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 |      | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 130 | Value *SCEVExpander::visitMulExpr(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. | 
 | 134 |   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) | 
 | 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 |  | 
| Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 155 | Value *SCEVExpander::visitUDivExpr(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); | 
| Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 161 |   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { | 
 | 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 |  | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 174 | Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { | 
 | 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)); | 
| Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 230 |      | 
 | 231 |     // IF the step is by one, just return the inserted IV. | 
| Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 232 |     if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) | 
| Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 233 |       if (CI->getValue() == 1) | 
| Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 234 |         return I; | 
 | 235 |      | 
 | 236 |     // If the insert point is directly inside of the loop, emit the multiply at | 
 | 237 |     // the insert point.  Otherwise, L is a loop that is a parent of the insert | 
 | 238 |     // point loop.  If we can, move the multiply to the outer most loop that it | 
 | 239 |     // is safe to be in. | 
 | 240 |     Instruction *MulInsertPt = InsertPt; | 
 | 241 |     Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent()); | 
 | 242 |     if (InsertPtLoop != L && InsertPtLoop && | 
 | 243 |         L->contains(InsertPtLoop->getHeader())) { | 
| Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 244 |       do { | 
| Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 245 |         // If we cannot hoist the multiply out of this loop, don't. | 
 | 246 |         if (!InsertPtLoop->isLoopInvariant(F)) break; | 
 | 247 |  | 
| Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 248 |         BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); | 
 | 249 |  | 
 | 250 |         // If this loop hasn't got a preheader, we aren't able to hoist the | 
 | 251 |         // multiply. | 
 | 252 |         if (!InsertPtLoopPH) | 
 | 253 |           break; | 
 | 254 |  | 
 | 255 |         // Otherwise, move the insert point to the preheader. | 
 | 256 |         MulInsertPt = InsertPtLoopPH->getTerminator(); | 
| Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 257 |         InsertPtLoop = InsertPtLoop->getParentLoop(); | 
| Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 258 |       } while (InsertPtLoop != L); | 
| Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 259 |     } | 
 | 260 |      | 
| Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 261 |     return InsertBinop(Instruction::Mul, I, F, MulInsertPt); | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 262 |   } | 
 | 263 |  | 
 | 264 |   // If this is a chain of recurrences, turn it into a closed form, using the | 
 | 265 |   // folders, then expandCodeFor the closed form.  This allows the folders to | 
 | 266 |   // simplify the expression without having to build a bunch of special code | 
 | 267 |   // into this folder. | 
| Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 268 |   SCEVHandle IH = SE.getUnknown(I);   // Get I as a "symbolic" SCEV. | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 269 |  | 
| Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 270 |   SCEVHandle V = S->evaluateAtIteration(IH, SE); | 
| Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 271 |   //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n"; | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 272 |  | 
| Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 273 |   return expand(V); | 
| Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 274 | } | 
| Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 275 |  | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 276 | Value *SCEVExpander::visitTruncateExpr(SCEVTruncateExpr *S) { | 
 | 277 |   Value *V = expand(S->getOperand()); | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 278 |   if (isa<PointerType>(V->getType())) | 
 | 279 |     V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); | 
| Gabor Greif | e9324f3 | 2008-10-13 10:21:17 +0000 | [diff] [blame] | 280 |   return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt); | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 281 | } | 
 | 282 |  | 
 | 283 | Value *SCEVExpander::visitZeroExtendExpr(SCEVZeroExtendExpr *S) { | 
 | 284 |   Value *V = expand(S->getOperand()); | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 285 |   if (isa<PointerType>(V->getType())) | 
 | 286 |     V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); | 
| Gabor Greif | e9324f3 | 2008-10-13 10:21:17 +0000 | [diff] [blame] | 287 |   return CastInst::CreateZExtOrBitCast(V, S->getType(), "tmp.", InsertPt); | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 288 | } | 
 | 289 |  | 
 | 290 | Value *SCEVExpander::visitSignExtendExpr(SCEVSignExtendExpr *S) { | 
 | 291 |   Value *V = expand(S->getOperand()); | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 292 |   if (isa<PointerType>(V->getType())) | 
 | 293 |     V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); | 
| Gabor Greif | e9324f3 | 2008-10-13 10:21:17 +0000 | [diff] [blame] | 294 |   return CastInst::CreateSExtOrBitCast(V, S->getType(), "tmp.", InsertPt); | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 295 | } | 
 | 296 |  | 
| Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 297 | Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) { | 
 | 298 |   Value *LHS = expand(S->getOperand(0)); | 
 | 299 |   for (unsigned i = 1; i < S->getNumOperands(); ++i) { | 
 | 300 |     Value *RHS = expand(S->getOperand(i)); | 
 | 301 |     Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); | 
| Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 302 |     LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); | 
| Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 303 |   } | 
 | 304 |   return LHS; | 
 | 305 | } | 
 | 306 |  | 
| Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 307 | Value *SCEVExpander::visitUMaxExpr(SCEVUMaxExpr *S) { | 
 | 308 |   Value *LHS = expand(S->getOperand(0)); | 
 | 309 |   for (unsigned i = 1; i < S->getNumOperands(); ++i) { | 
 | 310 |     Value *RHS = expand(S->getOperand(i)); | 
 | 311 |     Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); | 
| Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 312 |     LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); | 
| Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 313 |   } | 
 | 314 |   return LHS; | 
 | 315 | } | 
 | 316 |  | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 317 | Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty, | 
 | 318 |                                    Instruction *IP) { | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 319 |   // Expand the code for this SCEV. | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 320 |   assert(TD.getTypeSizeInBits(Ty) == TD.getTypeSizeInBits(SH->getType()) && | 
 | 321 |          "non-trivial casts should be done with the SCEVs directly!"); | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 322 |   this->InsertPt = IP; | 
| Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 323 |   Value *V = expand(SH); | 
 | 324 |   return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); | 
| Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 325 | } | 
 | 326 |  | 
| Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 327 | Value *SCEVExpander::expand(SCEV *S) { | 
 | 328 |   // Check to see if we already expanded this. | 
 | 329 |   std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S); | 
 | 330 |   if (I != InsertedExpressions.end()) | 
 | 331 |     return I->second; | 
 | 332 |    | 
 | 333 |   Value *V = visit(S); | 
 | 334 |   InsertedExpressions[S] = V; | 
 | 335 |   return V; | 
 | 336 | } |