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 | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 22 | /// InsertCastOfTo - Insert a cast of V to the specified type, doing what |
| 23 | /// we can to share the casts. |
Reid Spencer | 3ba68b9 | 2006-12-13 08:06:42 +0000 | [diff] [blame] | 24 | Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, |
| 25 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 26 | // Short-circuit unnecessary bitcasts. |
| 27 | if (opcode == Instruction::BitCast && V->getType() == Ty) |
| 28 | return V; |
| 29 | |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 30 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 31 | if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) && |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 32 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 33 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 34 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 35 | CI->getOpcode() == Instruction::IntToPtr) && |
| 36 | SE.getTypeSizeInBits(CI->getType()) == |
| 37 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 38 | return CI->getOperand(0); |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 39 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 40 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 41 | CE->getOpcode() == Instruction::IntToPtr) && |
| 42 | SE.getTypeSizeInBits(CE->getType()) == |
| 43 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 44 | return CE->getOperand(0); |
| 45 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 46 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 47 | // FIXME: keep track of the cast instruction. |
| 48 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 49 | return ConstantExpr::getCast(opcode, C, Ty); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 50 | |
| 51 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 52 | // Check to see if there is already a cast! |
| 53 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); |
| 54 | UI != E; ++UI) { |
| 55 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 56 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 57 | if (CI->getOpcode() == opcode) { |
| 58 | // If the cast isn't the first instruction of the function, move it. |
| 59 | if (BasicBlock::iterator(CI) != |
| 60 | A->getParent()->getEntryBlock().begin()) { |
Dan Gohman | aabb04f | 2009-04-22 16:11:16 +0000 | [diff] [blame] | 61 | // If the CastInst is the insert point, change the insert point. |
| 62 | if (CI == InsertPt) ++InsertPt; |
| 63 | // Splice the cast at the beginning of the entry block. |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 64 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 65 | } |
| 66 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 67 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 68 | } |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 69 | Instruction *I = CastInst::Create(opcode, V, Ty, V->getName(), |
| 70 | A->getParent()->getEntryBlock().begin()); |
| 71 | InsertedValues.insert(I); |
| 72 | return I; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 73 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 74 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 75 | Instruction *I = cast<Instruction>(V); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 76 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 77 | // Check to see if there is already a cast. If there is, use it. |
| 78 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 79 | UI != E; ++UI) { |
| 80 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 81 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 82 | if (CI->getOpcode() == opcode) { |
| 83 | BasicBlock::iterator It = I; ++It; |
| 84 | if (isa<InvokeInst>(I)) |
| 85 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
| 86 | while (isa<PHINode>(It)) ++It; |
| 87 | if (It != BasicBlock::iterator(CI)) { |
Dan Gohman | aabb04f | 2009-04-22 16:11:16 +0000 | [diff] [blame] | 88 | // If the CastInst is the insert point, change the insert point. |
| 89 | if (CI == InsertPt) ++InsertPt; |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 90 | // Splice the cast immediately after the operand in question. |
| 91 | CI->moveBefore(It); |
| 92 | } |
| 93 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 95 | } |
| 96 | BasicBlock::iterator IP = I; ++IP; |
| 97 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 98 | IP = II->getNormalDest()->begin(); |
| 99 | while (isa<PHINode>(IP)) ++IP; |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 100 | Instruction *CI = CastInst::Create(opcode, V, Ty, V->getName(), IP); |
| 101 | InsertedValues.insert(CI); |
| 102 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 105 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 106 | /// which must be possible with a noop cast. |
| 107 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) { |
| 108 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 109 | assert((Op == Instruction::BitCast || |
Devang Patel | e2a1746 | 2009-04-22 18:51:05 +0000 | [diff] [blame] | 110 | Op == Instruction::PtrToInt || |
| 111 | Op == Instruction::IntToPtr) && |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 112 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 113 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 114 | "InsertNoopCastOfTo cannot change sizes!"); |
| 115 | return InsertCastOfTo(Op, V, Ty); |
| 116 | } |
| 117 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 118 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 119 | /// of work to avoid inserting an obviously redundant operation. |
| 120 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, |
Dan Gohman | 6cdc727 | 2009-04-22 16:05:50 +0000 | [diff] [blame] | 121 | Value *RHS, BasicBlock::iterator InsertPt) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 122 | // Fold a binop with constant operands. |
| 123 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 124 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 125 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
| 126 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 127 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 128 | unsigned ScanLimit = 6; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 129 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 130 | if (InsertPt != BlockBegin) { |
| 131 | // Scanning starts from the last instruction before InsertPt. |
| 132 | BasicBlock::iterator IP = InsertPt; |
| 133 | --IP; |
| 134 | for (; ScanLimit; --IP, --ScanLimit) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 135 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
| 136 | IP->getOperand(1) == RHS) |
| 137 | return IP; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 138 | if (IP == BlockBegin) break; |
| 139 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 140 | } |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 141 | |
| 142 | // If we haven't found this binop, insert it. |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 143 | Instruction *BO = BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt); |
| 144 | InsertedValues.insert(BO); |
| 145 | return BO; |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 148 | /// FactorOutConstant - Test if S is divisible by Factor, using signed |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 149 | /// division. If so, update S with Factor divided out and return true. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 150 | /// S need not be evenly divisble if a reasonable remainder can be |
| 151 | /// computed. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 152 | /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made |
| 153 | /// unnecessary; in its place, just signed-divide Ops[i] by the scale and |
| 154 | /// check to see if the divide was folded. |
| 155 | static bool FactorOutConstant(SCEVHandle &S, |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 156 | SCEVHandle &Remainder, |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 157 | const APInt &Factor, |
| 158 | ScalarEvolution &SE) { |
| 159 | // Everything is divisible by one. |
| 160 | if (Factor == 1) |
| 161 | return true; |
| 162 | |
| 163 | // For a Constant, check for a multiple of the given factor. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 164 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
| 165 | ConstantInt *CI = |
| 166 | ConstantInt::get(C->getValue()->getValue().sdiv(Factor)); |
| 167 | // If the quotient is zero and the remainder is non-zero, reject |
| 168 | // the value at this scale. It will be considered for subsequent |
| 169 | // smaller scales. |
| 170 | if (C->isZero() || !CI->isZero()) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 171 | SCEVHandle Div = SE.getConstant(CI); |
| 172 | S = Div; |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 173 | Remainder = |
| 174 | SE.getAddExpr(Remainder, |
| 175 | SE.getConstant(C->getValue()->getValue().srem(Factor))); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 178 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 179 | |
| 180 | // In a Mul, check if there is a constant operand which is a multiple |
| 181 | // of the given factor. |
| 182 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) |
| 183 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
| 184 | if (!C->getValue()->getValue().srem(Factor)) { |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 185 | const SmallVectorImpl<SCEVHandle> &MOperands = M->getOperands(); |
| 186 | SmallVector<SCEVHandle, 4> NewMulOps(MOperands.begin(), MOperands.end()); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 187 | NewMulOps[0] = |
| 188 | SE.getConstant(C->getValue()->getValue().sdiv(Factor)); |
| 189 | S = SE.getMulExpr(NewMulOps); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | // In an AddRec, check if both start and step are divisible. |
| 194 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 195 | SCEVHandle Step = A->getStepRecurrence(SE); |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 196 | SCEVHandle StepRem = SE.getIntegerSCEV(0, Step->getType()); |
| 197 | if (!FactorOutConstant(Step, StepRem, Factor, SE)) |
| 198 | return false; |
| 199 | if (!StepRem->isZero()) |
| 200 | return false; |
| 201 | SCEVHandle Start = A->getStart(); |
| 202 | if (!FactorOutConstant(Start, Remainder, Factor, SE)) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 203 | return false; |
| 204 | S = SE.getAddRecExpr(Start, Step, A->getLoop()); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 211 | /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 212 | /// instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 213 | /// BasicAliasAnalysis analyze the result. However, it suffers from the |
| 214 | /// underlying bug described in PR2831. Addition in LLVM currently always |
| 215 | /// has two's complement wrapping guaranteed. However, the semantics for |
| 216 | /// getelementptr overflow are ambiguous. In the common case though, this |
| 217 | /// expansion gets used when a GEP in the original code has been converted |
| 218 | /// into integer arithmetic, in which case the resulting code will be no |
| 219 | /// more undefined than it was originally. |
| 220 | /// |
| 221 | /// Design note: It might seem desirable for this function to be more |
| 222 | /// loop-aware. If some of the indices are loop-invariant while others |
| 223 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 224 | /// loop-invariant portions of the overall computation outside the loop. |
| 225 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 226 | /// arithmetic is a low-level optimization that often isn't very |
| 227 | /// important until late in the optimization process. In fact, passes |
| 228 | /// like InstructionCombining will combine GEPs, even if it means |
| 229 | /// pushing loop-invariant computation down into loops, so even if the |
| 230 | /// GEPs were split here, the work would quickly be undone. The |
| 231 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 232 | /// after the last InstructionCombining pass), takes care of hoisting |
| 233 | /// loop-invariant portions of expressions, after considering what |
| 234 | /// can be folded using target addressing modes. |
| 235 | /// |
| 236 | Value *SCEVExpander::expandAddToGEP(const SCEVHandle *op_begin, |
| 237 | const SCEVHandle *op_end, |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 238 | const PointerType *PTy, |
| 239 | const Type *Ty, |
| 240 | Value *V) { |
| 241 | const Type *ElTy = PTy->getElementType(); |
| 242 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 243 | SmallVector<SCEVHandle, 8> Ops(op_begin, op_end); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 244 | bool AnyNonZeroIndices = false; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 245 | |
| 246 | // Decend down the pointer's type and attempt to convert the other |
| 247 | // operands into GEP indices, at each level. The first index in a GEP |
| 248 | // indexes into the array implied by the pointer operand; the rest of |
| 249 | // the indices index into the element or field type selected by the |
| 250 | // preceding index. |
| 251 | for (;;) { |
| 252 | APInt ElSize = APInt(SE.getTypeSizeInBits(Ty), |
| 253 | ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0); |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 254 | SmallVector<SCEVHandle, 8> NewOps; |
| 255 | SmallVector<SCEVHandle, 8> ScaledOps; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 257 | // Split AddRecs up into parts as either of the parts may be usable |
| 258 | // without the other. |
| 259 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) |
| 260 | if (!A->getStart()->isZero()) { |
| 261 | SCEVHandle Start = A->getStart(); |
| 262 | Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()), |
| 263 | A->getStepRecurrence(SE), |
| 264 | A->getLoop())); |
| 265 | Ops[i] = Start; |
| 266 | ++e; |
| 267 | } |
| 268 | // If the scale size is not 0, attempt to factor out a scale. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 269 | if (ElSize != 0) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 270 | SCEVHandle Op = Ops[i]; |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 271 | SCEVHandle Remainder = SE.getIntegerSCEV(0, Op->getType()); |
| 272 | if (FactorOutConstant(Op, Remainder, ElSize, SE)) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 273 | ScaledOps.push_back(Op); // Op now has ElSize factored out. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 274 | NewOps.push_back(Remainder); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 275 | continue; |
| 276 | } |
| 277 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 278 | // If the operand was not divisible, add it to the list of operands |
| 279 | // we'll scan next iteration. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 280 | NewOps.push_back(Ops[i]); |
| 281 | } |
| 282 | Ops = NewOps; |
| 283 | AnyNonZeroIndices |= !ScaledOps.empty(); |
| 284 | Value *Scaled = ScaledOps.empty() ? |
| 285 | Constant::getNullValue(Ty) : |
| 286 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 287 | GepIndices.push_back(Scaled); |
| 288 | |
| 289 | // Collect struct field index operands. |
| 290 | if (!Ops.empty()) |
| 291 | while (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 292 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 293 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 294 | const StructLayout &SL = *SE.TD->getStructLayout(STy); |
| 295 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 296 | if (FullOffset < SL.getSizeInBytes()) { |
| 297 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
| 298 | GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx)); |
| 299 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 300 | Ops[0] = |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame^] | 301 | SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 302 | AnyNonZeroIndices = true; |
| 303 | continue; |
| 304 | } |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) { |
| 310 | ElTy = ATy->getElementType(); |
| 311 | continue; |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | // If none of the operands were convertable to proper GEP indices, cast |
| 317 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 318 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 319 | if (!AnyNonZeroIndices) { |
| 320 | V = InsertNoopCastOfTo(V, |
| 321 | Type::Int8Ty->getPointerTo(PTy->getAddressSpace())); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 322 | Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 323 | |
| 324 | // Fold a GEP with constant operands. |
| 325 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 326 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Dan Gohman | 278b49a | 2009-05-19 19:18:01 +0000 | [diff] [blame] | 327 | return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 328 | |
| 329 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 330 | unsigned ScanLimit = 6; |
| 331 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 332 | if (InsertPt != BlockBegin) { |
| 333 | // Scanning starts from the last instruction before InsertPt. |
| 334 | BasicBlock::iterator IP = InsertPt; |
| 335 | --IP; |
| 336 | for (; ScanLimit; --IP, --ScanLimit) { |
| 337 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 338 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 339 | return IP; |
| 340 | if (IP == BlockBegin) break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | Value *GEP = GetElementPtrInst::Create(V, Idx, "scevgep", InsertPt); |
| 345 | InsertedValues.insert(GEP); |
| 346 | return GEP; |
| 347 | } |
| 348 | |
| 349 | // Insert a pretty getelementptr. |
| 350 | Value *GEP = GetElementPtrInst::Create(V, |
| 351 | GepIndices.begin(), |
| 352 | GepIndices.end(), |
| 353 | "scevgep", InsertPt); |
| 354 | Ops.push_back(SE.getUnknown(GEP)); |
| 355 | InsertedValues.insert(GEP); |
| 356 | return expand(SE.getAddExpr(Ops)); |
| 357 | } |
| 358 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 359 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 360 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 361 | Value *V = expand(S->getOperand(S->getNumOperands()-1)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 362 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 363 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 364 | // comments on expandAddToGEP for details. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 365 | if (SE.TD) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 366 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) { |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 367 | const SmallVectorImpl<SCEVHandle> &Ops = S->getOperands(); |
Dan Gohman | fb5a341 | 2009-05-24 19:02:45 +0000 | [diff] [blame] | 368 | return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 369 | PTy, Ty, V); |
| 370 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 371 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 372 | V = InsertNoopCastOfTo(V, Ty); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 373 | |
| 374 | // Emit a bunch of add instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 375 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 376 | Value *W = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 377 | V = InsertBinop(Instruction::Add, V, W, InsertPt); |
| 378 | } |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 379 | return V; |
| 380 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 381 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 382 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 383 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 384 | int FirstOp = 0; // Set if we should emit a subtract. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 385 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 386 | if (SC->getValue()->isAllOnesValue()) |
| 387 | FirstOp = 1; |
| 388 | |
| 389 | int i = S->getNumOperands()-2; |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 390 | Value *V = expandCodeFor(S->getOperand(i+1), Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 391 | |
| 392 | // Emit a bunch of multiply instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 393 | for (; i >= FirstOp; --i) { |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 394 | Value *W = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 395 | V = InsertBinop(Instruction::Mul, V, W, InsertPt); |
| 396 | } |
| 397 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 398 | // -1 * ... ---> 0 - ... |
| 399 | if (FirstOp == 1) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 400 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 401 | return V; |
| 402 | } |
| 403 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 404 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 405 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 406 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 407 | Value *LHS = expandCodeFor(S->getLHS(), Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 408 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 409 | const APInt &RHS = SC->getValue()->getValue(); |
| 410 | if (RHS.isPowerOf2()) |
| 411 | return InsertBinop(Instruction::LShr, LHS, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 412 | ConstantInt::get(Ty, RHS.logBase2()), |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 413 | InsertPt); |
| 414 | } |
| 415 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 416 | Value *RHS = expandCodeFor(S->getRHS(), Ty); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 417 | return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); |
| 418 | } |
| 419 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 420 | /// Move parts of Base into Rest to leave Base with the minimal |
| 421 | /// expression that provides a pointer operand suitable for a |
| 422 | /// GEP expansion. |
| 423 | static void ExposePointerBase(SCEVHandle &Base, SCEVHandle &Rest, |
| 424 | ScalarEvolution &SE) { |
| 425 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 426 | Base = A->getStart(); |
| 427 | Rest = SE.getAddExpr(Rest, |
| 428 | SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()), |
| 429 | A->getStepRecurrence(SE), |
| 430 | A->getLoop())); |
| 431 | } |
| 432 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 433 | Base = A->getOperand(A->getNumOperands()-1); |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 434 | SmallVector<SCEVHandle, 8> NewAddOps(A->op_begin(), A->op_end()); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 435 | NewAddOps.back() = Rest; |
| 436 | Rest = SE.getAddExpr(NewAddOps); |
| 437 | ExposePointerBase(Base, Rest, SE); |
| 438 | } |
| 439 | } |
| 440 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 441 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 442 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 443 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 444 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 445 | // First check for an existing canonical IV in a suitable type. |
| 446 | PHINode *CanonicalIV = 0; |
| 447 | if (PHINode *PN = L->getCanonicalInductionVariable()) |
| 448 | if (SE.isSCEVable(PN->getType()) && |
| 449 | isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) && |
| 450 | SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) |
| 451 | CanonicalIV = PN; |
| 452 | |
| 453 | // Rewrite an AddRec in terms of the canonical induction variable, if |
| 454 | // its type is more narrow. |
| 455 | if (CanonicalIV && |
| 456 | SE.getTypeSizeInBits(CanonicalIV->getType()) > |
| 457 | SE.getTypeSizeInBits(Ty)) { |
| 458 | SCEVHandle Start = SE.getAnyExtendExpr(S->getStart(), |
| 459 | CanonicalIV->getType()); |
| 460 | SCEVHandle Step = SE.getAnyExtendExpr(S->getStepRecurrence(SE), |
| 461 | CanonicalIV->getType()); |
| 462 | Value *V = expand(SE.getAddRecExpr(Start, Step, S->getLoop())); |
| 463 | BasicBlock::iterator SaveInsertPt = getInsertionPoint(); |
| 464 | BasicBlock::iterator NewInsertPt = |
| 465 | next(BasicBlock::iterator(cast<Instruction>(V))); |
| 466 | while (isa<PHINode>(NewInsertPt)) ++NewInsertPt; |
| 467 | V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0, |
| 468 | NewInsertPt); |
| 469 | setInsertionPoint(SaveInsertPt); |
| 470 | return V; |
| 471 | } |
| 472 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 473 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 474 | if (!S->getStart()->isZero()) { |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 475 | const SmallVectorImpl<SCEVHandle> &SOperands = S->getOperands(); |
| 476 | SmallVector<SCEVHandle, 4> NewOps(SOperands.begin(), SOperands.end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 477 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 478 | SCEVHandle Rest = SE.getAddRecExpr(NewOps, L); |
| 479 | |
| 480 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 481 | // comments on expandAddToGEP for details. |
| 482 | if (SE.TD) { |
| 483 | SCEVHandle Base = S->getStart(); |
Dan Gohman | f1a8048 | 2009-05-27 02:07:15 +0000 | [diff] [blame] | 484 | SCEVHandle RestArray[1] = { Rest }; |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 485 | // Dig into the expression to find the pointer base for a GEP. |
| 486 | ExposePointerBase(Base, RestArray[0], SE); |
| 487 | // If we found a pointer, expand the AddRec with a GEP. |
| 488 | if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
Dan Gohman | f876ad0 | 2009-05-26 17:41:16 +0000 | [diff] [blame] | 489 | // Make sure the Base isn't something exotic, such as a multiplied |
| 490 | // or divided pointer value. In those cases, the result type isn't |
| 491 | // actually a pointer type. |
| 492 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 493 | Value *StartV = expand(Base); |
| 494 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
| 495 | return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); |
| 496 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | Value *RestV = expand(Rest); |
| 501 | return expand(SE.getAddExpr(S->getStart(), SE.getUnknown(RestV))); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 505 | if (S->isAffine() && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 506 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 507 | // If there's a canonical IV, just use it. |
| 508 | if (CanonicalIV) { |
| 509 | assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && |
| 510 | "IVs with types different from the canonical IV should " |
| 511 | "already have been handled!"); |
| 512 | return CanonicalIV; |
| 513 | } |
| 514 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 515 | // Create and insert the PHI node for the induction variable in the |
| 516 | // specified loop. |
| 517 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 518 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 519 | InsertedValues.insert(PN); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 520 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 521 | |
| 522 | pred_iterator HPI = pred_begin(Header); |
| 523 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 524 | if (!L->contains(*HPI)) ++HPI; |
| 525 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 526 | "No backedge in loop?"); |
| 527 | |
| 528 | // Insert a unit add instruction right before the terminator corresponding |
| 529 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 530 | Constant *One = ConstantInt::get(Ty, 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 531 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 532 | (*HPI)->getTerminator()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 533 | InsertedValues.insert(Add); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 534 | |
| 535 | pred_iterator PI = pred_begin(Header); |
| 536 | if (*PI == L->getLoopPreheader()) |
| 537 | ++PI; |
| 538 | PN->addIncoming(Add, *PI); |
| 539 | return PN; |
| 540 | } |
| 541 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 542 | // {0,+,F} --> {0,+,1} * F |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 543 | // Get the canonical induction variable I for this loop. |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 544 | Value *I = CanonicalIV ? |
| 545 | CanonicalIV : |
| 546 | getOrInsertCanonicalInductionVariable(L, Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 547 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 548 | // 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] | 549 | if (S->isAffine()) { // {0,+,F} --> i*F |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 550 | Value *F = expandCodeFor(S->getOperand(1), Ty); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 551 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 552 | // If the insert point is directly inside of the loop, emit the multiply at |
| 553 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 554 | // point loop. If we can, move the multiply to the outer most loop that it |
| 555 | // is safe to be in. |
Dan Gohman | 6cdc727 | 2009-04-22 16:05:50 +0000 | [diff] [blame] | 556 | BasicBlock::iterator MulInsertPt = getInsertionPoint(); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 557 | Loop *InsertPtLoop = SE.LI->getLoopFor(MulInsertPt->getParent()); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 558 | if (InsertPtLoop != L && InsertPtLoop && |
| 559 | L->contains(InsertPtLoop->getHeader())) { |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 560 | do { |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 561 | // If we cannot hoist the multiply out of this loop, don't. |
| 562 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 563 | |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 564 | BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); |
| 565 | |
| 566 | // If this loop hasn't got a preheader, we aren't able to hoist the |
| 567 | // multiply. |
| 568 | if (!InsertPtLoopPH) |
| 569 | break; |
| 570 | |
| 571 | // Otherwise, move the insert point to the preheader. |
| 572 | MulInsertPt = InsertPtLoopPH->getTerminator(); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 573 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 574 | } while (InsertPtLoop != L); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 577 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 581 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 582 | // simplify the expression without having to build a bunch of special code |
| 583 | // into this folder. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 584 | SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 585 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 586 | // Promote S up to the canonical IV type, if the cast is foldable. |
| 587 | SCEVHandle NewS = S; |
| 588 | SCEVHandle Ext = SE.getNoopOrAnyExtend(S, I->getType()); |
| 589 | if (isa<SCEVAddRecExpr>(Ext)) |
| 590 | NewS = Ext; |
| 591 | |
| 592 | SCEVHandle V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 593 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 594 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 595 | // Truncate the result down to the original type, if needed. |
| 596 | SCEVHandle T = SE.getTruncateOrNoop(V, Ty); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 597 | return expand(V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 598 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 599 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 600 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 601 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 602 | Value *V = expandCodeFor(S->getOperand(), |
| 603 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 604 | Instruction *I = new TruncInst(V, Ty, "tmp.", InsertPt); |
| 605 | InsertedValues.insert(I); |
| 606 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 609 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 610 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 611 | Value *V = expandCodeFor(S->getOperand(), |
| 612 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 613 | Instruction *I = new ZExtInst(V, Ty, "tmp.", InsertPt); |
| 614 | InsertedValues.insert(I); |
| 615 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 618 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 619 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 620 | Value *V = expandCodeFor(S->getOperand(), |
| 621 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 622 | Instruction *I = new SExtInst(V, Ty, "tmp.", InsertPt); |
| 623 | InsertedValues.insert(I); |
| 624 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 627 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 628 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 629 | Value *LHS = expandCodeFor(S->getOperand(0), Ty); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 630 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 631 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 632 | Instruction *ICmp = |
| 633 | new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); |
| 634 | InsertedValues.insert(ICmp); |
| 635 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); |
| 636 | InsertedValues.insert(Sel); |
| 637 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 638 | } |
| 639 | return LHS; |
| 640 | } |
| 641 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 642 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 643 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 644 | Value *LHS = expandCodeFor(S->getOperand(0), Ty); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 645 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 646 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 647 | Instruction *ICmp = |
| 648 | new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); |
| 649 | InsertedValues.insert(ICmp); |
| 650 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); |
| 651 | InsertedValues.insert(Sel); |
| 652 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 653 | } |
| 654 | return LHS; |
| 655 | } |
| 656 | |
Dan Gohman | 752ec7d | 2009-04-23 15:16:49 +0000 | [diff] [blame] | 657 | Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 658 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 659 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 660 | if (Ty) { |
| 661 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 662 | "non-trivial casts should be done with the SCEVs directly!"); |
| 663 | V = InsertNoopCastOfTo(V, Ty); |
| 664 | } |
| 665 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 668 | Value *SCEVExpander::expand(const SCEV *S) { |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 669 | // Check to see if we already expanded this. |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 670 | std::map<SCEVHandle, AssertingVH<Value> >::iterator I = |
| 671 | InsertedExpressions.find(S); |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 672 | if (I != InsertedExpressions.end()) |
| 673 | return I->second; |
| 674 | |
| 675 | Value *V = visit(S); |
| 676 | InsertedExpressions[S] = V; |
| 677 | return V; |
| 678 | } |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 679 | |
| 680 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 681 | /// canonical induction variable of the specified type for the specified |
| 682 | /// loop (inserting one if there is none). A canonical induction variable |
| 683 | /// starts at zero and steps by one on each iteration. |
| 684 | Value * |
| 685 | SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, |
| 686 | const Type *Ty) { |
| 687 | assert(Ty->isInteger() && "Can only insert integer induction variables!"); |
| 688 | SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty), |
| 689 | SE.getIntegerSCEV(1, Ty), L); |
| 690 | return expand(H); |
| 691 | } |