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