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