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