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" |
Dale Johannesen | 8d50ea7 | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 18 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 19 | #include "llvm/LLVMContext.h" |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 22 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Gabor Greif | 19e5ada | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 25 | /// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP, |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 26 | /// reusing an existing cast if a suitable one exists, moving an existing |
| 27 | /// cast if a suitable one exists but isn't in the right place, or |
Gabor Greif | 19e5ada | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 28 | /// creating a new one. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 29 | Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty, |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 30 | Instruction::CastOps Op, |
| 31 | BasicBlock::iterator IP) { |
| 32 | // Check to see if there is already a cast! |
| 33 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
Gabor Greif | f64f9cf | 2010-07-09 16:39:02 +0000 | [diff] [blame] | 34 | UI != E; ++UI) { |
| 35 | User *U = *UI; |
| 36 | if (U->getType() == Ty) |
Gabor Greif | 19e5ada | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 37 | if (CastInst *CI = dyn_cast<CastInst>(U)) |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 38 | if (CI->getOpcode() == Op) { |
| 39 | // If the cast isn't where we want it, fix it. |
| 40 | if (BasicBlock::iterator(CI) != IP) { |
| 41 | // Create a new cast, and leave the old cast in place in case |
| 42 | // it is being used as an insert point. Clear its operand |
| 43 | // so that it doesn't hold anything live. |
| 44 | Instruction *NewCI = CastInst::Create(Op, V, Ty, "", IP); |
| 45 | NewCI->takeName(CI); |
| 46 | CI->replaceAllUsesWith(NewCI); |
| 47 | CI->setOperand(0, UndefValue::get(V->getType())); |
| 48 | rememberInstruction(NewCI); |
| 49 | return NewCI; |
| 50 | } |
Dan Gohman | 6f5fed2 | 2010-06-19 22:50:35 +0000 | [diff] [blame] | 51 | rememberInstruction(CI); |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 52 | return CI; |
| 53 | } |
Gabor Greif | f64f9cf | 2010-07-09 16:39:02 +0000 | [diff] [blame] | 54 | } |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 55 | |
| 56 | // Create a new cast. |
| 57 | Instruction *I = CastInst::Create(Op, V, Ty, V->getName(), IP); |
| 58 | rememberInstruction(I); |
| 59 | return I; |
| 60 | } |
| 61 | |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 62 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 63 | /// which must be possible with a noop cast, doing what we can to share |
| 64 | /// the casts. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 65 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) { |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 66 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 67 | assert((Op == Instruction::BitCast || |
| 68 | Op == Instruction::PtrToInt || |
| 69 | Op == Instruction::IntToPtr) && |
| 70 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 71 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 72 | "InsertNoopCastOfTo cannot change sizes!"); |
| 73 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 74 | // Short-circuit unnecessary bitcasts. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 75 | if (Op == Instruction::BitCast && V->getType() == Ty) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 76 | return V; |
| 77 | |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 78 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 79 | if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) && |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 80 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 81 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 82 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 83 | CI->getOpcode() == Instruction::IntToPtr) && |
| 84 | SE.getTypeSizeInBits(CI->getType()) == |
| 85 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 86 | return CI->getOperand(0); |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 87 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 88 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 89 | CE->getOpcode() == Instruction::IntToPtr) && |
| 90 | SE.getTypeSizeInBits(CE->getType()) == |
| 91 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 92 | return CE->getOperand(0); |
| 93 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 94 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 95 | // Fold a cast of a constant. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 96 | if (Constant *C = dyn_cast<Constant>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 97 | return ConstantExpr::getCast(Op, C, Ty); |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 99 | // Cast the argument at the beginning of the entry block, after |
| 100 | // any bitcasts of other arguments. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 101 | if (Argument *A = dyn_cast<Argument>(V)) { |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 102 | BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin(); |
| 103 | while ((isa<BitCastInst>(IP) && |
| 104 | isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) && |
| 105 | cast<BitCastInst>(IP)->getOperand(0) != A) || |
| 106 | isa<DbgInfoIntrinsic>(IP)) |
| 107 | ++IP; |
| 108 | return ReuseOrCreateCast(A, Ty, Op, IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 109 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 110 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 111 | // Cast the instruction immediately after the instruction. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 112 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 113 | BasicBlock::iterator IP = I; ++IP; |
| 114 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 115 | IP = II->getNormalDest()->begin(); |
Jim Grosbach | 08f55d0 | 2010-06-16 21:13:38 +0000 | [diff] [blame] | 116 | while (isa<PHINode>(IP) || isa<DbgInfoIntrinsic>(IP)) ++IP; |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 117 | return ReuseOrCreateCast(I, Ty, Op, IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 120 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 121 | /// of work to avoid inserting an obviously redundant operation. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 122 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, |
| 123 | Value *LHS, Value *RHS) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 124 | // Fold a binop with constant operands. |
| 125 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 126 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 127 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 129 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 130 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 131 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 132 | // Scanning starts from the last instruction before the insertion point. |
| 133 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 134 | if (IP != BlockBegin) { |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 135 | --IP; |
| 136 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | 8d50ea7 | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 137 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 138 | // generated code. |
| 139 | if (isa<DbgInfoIntrinsic>(IP)) |
| 140 | ScanLimit++; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 141 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
| 142 | IP->getOperand(1) == RHS) |
| 143 | return IP; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 144 | if (IP == BlockBegin) break; |
| 145 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 146 | } |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 147 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 148 | // Save the original insertion point so we can restore it when we're done. |
| 149 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 150 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 151 | |
| 152 | // Move the insertion point out of as many loops as we can. |
| 153 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 154 | if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; |
| 155 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 156 | if (!Preheader) break; |
| 157 | |
| 158 | // Ok, move up a level. |
| 159 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 160 | } |
| 161 | |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 162 | // If we haven't found this binop, insert it. |
Devang Patel | df3ad66 | 2011-06-22 20:56:56 +0000 | [diff] [blame] | 163 | Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS, "tmp")); |
| 164 | BO->setDebugLoc(SaveInsertPt->getDebugLoc()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 165 | rememberInstruction(BO); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 166 | |
| 167 | // Restore the original insert point. |
| 168 | if (SaveInsertBB) |
| 169 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
| 170 | |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 171 | return BO; |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 174 | /// FactorOutConstant - Test if S is divisible by Factor, using signed |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 175 | /// division. If so, update S with Factor divided out and return true. |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 176 | /// S need not be evenly divisible if a reasonable remainder can be |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 177 | /// computed. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 178 | /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made |
| 179 | /// unnecessary; in its place, just signed-divide Ops[i] by the scale and |
| 180 | /// check to see if the divide was folded. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 181 | static bool FactorOutConstant(const SCEV *&S, |
| 182 | const SCEV *&Remainder, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 183 | const SCEV *Factor, |
| 184 | ScalarEvolution &SE, |
| 185 | const TargetData *TD) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 186 | // Everything is divisible by one. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 187 | if (Factor->isOne()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 188 | return true; |
| 189 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 190 | // x/x == 1. |
| 191 | if (S == Factor) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 192 | S = SE.getConstant(S->getType(), 1); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 196 | // For a Constant, check for a multiple of the given factor. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 197 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 198 | // 0/x == 0. |
| 199 | if (C->isZero()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 200 | return true; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 201 | // Check for divisibility. |
| 202 | if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) { |
| 203 | ConstantInt *CI = |
| 204 | ConstantInt::get(SE.getContext(), |
| 205 | C->getValue()->getValue().sdiv( |
| 206 | FC->getValue()->getValue())); |
| 207 | // If the quotient is zero and the remainder is non-zero, reject |
| 208 | // the value at this scale. It will be considered for subsequent |
| 209 | // smaller scales. |
| 210 | if (!CI->isZero()) { |
| 211 | const SCEV *Div = SE.getConstant(CI); |
| 212 | S = Div; |
| 213 | Remainder = |
| 214 | SE.getAddExpr(Remainder, |
| 215 | SE.getConstant(C->getValue()->getValue().srem( |
| 216 | FC->getValue()->getValue()))); |
| 217 | return true; |
| 218 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 219 | } |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 220 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 221 | |
| 222 | // In a Mul, check if there is a constant operand which is a multiple |
| 223 | // of the given factor. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 224 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
| 225 | if (TD) { |
| 226 | // With TargetData, the size is known. Check if there is a constant |
| 227 | // operand which is a multiple of the given factor. If so, we can |
| 228 | // factor it. |
| 229 | const SCEVConstant *FC = cast<SCEVConstant>(Factor); |
| 230 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
| 231 | if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 232 | SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 233 | NewMulOps[0] = |
| 234 | SE.getConstant(C->getValue()->getValue().sdiv( |
| 235 | FC->getValue()->getValue())); |
| 236 | S = SE.getMulExpr(NewMulOps); |
| 237 | return true; |
| 238 | } |
| 239 | } else { |
| 240 | // Without TargetData, check if Factor can be factored out of any of the |
| 241 | // Mul's operands. If so, we can just remove it. |
| 242 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 243 | const SCEV *SOp = M->getOperand(i); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 244 | const SCEV *Remainder = SE.getConstant(SOp->getType(), 0); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 245 | if (FactorOutConstant(SOp, Remainder, Factor, SE, TD) && |
| 246 | Remainder->isZero()) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 247 | SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 248 | NewMulOps[i] = SOp; |
| 249 | S = SE.getMulExpr(NewMulOps); |
| 250 | return true; |
| 251 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 252 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 255 | |
| 256 | // In an AddRec, check if both start and step are divisible. |
| 257 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 258 | const SCEV *Step = A->getStepRecurrence(SE); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 259 | const SCEV *StepRem = SE.getConstant(Step->getType(), 0); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 260 | if (!FactorOutConstant(Step, StepRem, Factor, SE, TD)) |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 261 | return false; |
| 262 | if (!StepRem->isZero()) |
| 263 | return false; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 264 | const SCEV *Start = A->getStart(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 265 | if (!FactorOutConstant(Start, Remainder, Factor, SE, TD)) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 266 | return false; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 267 | // FIXME: can use A->getNoWrapFlags(FlagNW) |
| 268 | S = SE.getAddRecExpr(Start, Step, A->getLoop(), SCEV::FlagAnyWrap); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
| 272 | return false; |
| 273 | } |
| 274 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 275 | /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs |
| 276 | /// is the number of SCEVAddRecExprs present, which are kept at the end of |
| 277 | /// the list. |
| 278 | /// |
| 279 | static void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 280 | Type *Ty, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 281 | ScalarEvolution &SE) { |
| 282 | unsigned NumAddRecs = 0; |
| 283 | for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i) |
| 284 | ++NumAddRecs; |
| 285 | // Group Ops into non-addrecs and addrecs. |
| 286 | SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs); |
| 287 | SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end()); |
| 288 | // Let ScalarEvolution sort and simplify the non-addrecs list. |
| 289 | const SCEV *Sum = NoAddRecs.empty() ? |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 290 | SE.getConstant(Ty, 0) : |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 291 | SE.getAddExpr(NoAddRecs); |
| 292 | // If it returned an add, use the operands. Otherwise it simplified |
| 293 | // the sum into a single value, so just use that. |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 294 | Ops.clear(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 295 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum)) |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 296 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 297 | else if (!Sum->isZero()) |
| 298 | Ops.push_back(Sum); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 299 | // Then append the addrecs. |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 300 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// SplitAddRecs - Flatten a list of add operands, moving addrec start values |
| 304 | /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}. |
| 305 | /// This helps expose more opportunities for folding parts of the expressions |
| 306 | /// into GEP indices. |
| 307 | /// |
| 308 | static void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 309 | Type *Ty, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 310 | ScalarEvolution &SE) { |
| 311 | // Find the addrecs. |
| 312 | SmallVector<const SCEV *, 8> AddRecs; |
| 313 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 314 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) { |
| 315 | const SCEV *Start = A->getStart(); |
| 316 | if (Start->isZero()) break; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 317 | const SCEV *Zero = SE.getConstant(Ty, 0); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 318 | AddRecs.push_back(SE.getAddRecExpr(Zero, |
| 319 | A->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 320 | A->getLoop(), |
| 321 | // FIXME: A->getNoWrapFlags(FlagNW) |
| 322 | SCEV::FlagAnyWrap)); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 323 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) { |
| 324 | Ops[i] = Zero; |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 325 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 326 | e += Add->getNumOperands(); |
| 327 | } else { |
| 328 | Ops[i] = Start; |
| 329 | } |
| 330 | } |
| 331 | if (!AddRecs.empty()) { |
| 332 | // Add the addrecs onto the end of the list. |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 333 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 334 | // Resort the operand list, moving any constants to the front. |
| 335 | SimplifyAddOperands(Ops, Ty, SE); |
| 336 | } |
| 337 | } |
| 338 | |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 339 | /// expandAddToGEP - Expand an addition expression with a pointer type into |
| 340 | /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 341 | /// BasicAliasAnalysis and other passes analyze the result. See the rules |
| 342 | /// for getelementptr vs. inttoptr in |
| 343 | /// http://llvm.org/docs/LangRef.html#pointeraliasing |
| 344 | /// for details. |
Dan Gohman | 13c5e35 | 2009-07-20 17:44:17 +0000 | [diff] [blame] | 345 | /// |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 346 | /// Design note: The correctness of using getelementptr here depends on |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 347 | /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as |
| 348 | /// they may introduce pointer arithmetic which may not be safely converted |
| 349 | /// into getelementptr. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 350 | /// |
| 351 | /// Design note: It might seem desirable for this function to be more |
| 352 | /// loop-aware. If some of the indices are loop-invariant while others |
| 353 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 354 | /// loop-invariant portions of the overall computation outside the loop. |
| 355 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 356 | /// arithmetic is a low-level optimization that often isn't very |
| 357 | /// important until late in the optimization process. In fact, passes |
| 358 | /// like InstructionCombining will combine GEPs, even if it means |
| 359 | /// pushing loop-invariant computation down into loops, so even if the |
| 360 | /// GEPs were split here, the work would quickly be undone. The |
| 361 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 362 | /// after the last InstructionCombining pass), takes care of hoisting |
| 363 | /// loop-invariant portions of expressions, after considering what |
| 364 | /// can be folded using target addressing modes. |
| 365 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 366 | Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin, |
| 367 | const SCEV *const *op_end, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 368 | PointerType *PTy, |
| 369 | Type *Ty, |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 370 | Value *V) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 371 | Type *ElTy = PTy->getElementType(); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 372 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 373 | SmallVector<const SCEV *, 8> Ops(op_begin, op_end); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 374 | bool AnyNonZeroIndices = false; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 375 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 376 | // Split AddRecs up into parts as either of the parts may be usable |
| 377 | // without the other. |
| 378 | SplitAddRecs(Ops, Ty, SE); |
| 379 | |
Bob Wilson | eb35699 | 2009-12-04 01:33:04 +0000 | [diff] [blame] | 380 | // Descend down the pointer's type and attempt to convert the other |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 381 | // operands into GEP indices, at each level. The first index in a GEP |
| 382 | // indexes into the array implied by the pointer operand; the rest of |
| 383 | // the indices index into the element or field type selected by the |
| 384 | // preceding index. |
| 385 | for (;;) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 386 | // If the scale size is not 0, attempt to factor out a scale for |
| 387 | // array indexing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 388 | SmallVector<const SCEV *, 8> ScaledOps; |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 389 | if (ElTy->isSized()) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 390 | const SCEV *ElSize = SE.getSizeOfExpr(ElTy); |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 391 | if (!ElSize->isZero()) { |
| 392 | SmallVector<const SCEV *, 8> NewOps; |
| 393 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 394 | const SCEV *Op = Ops[i]; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 395 | const SCEV *Remainder = SE.getConstant(Ty, 0); |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 396 | if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.TD)) { |
| 397 | // Op now has ElSize factored out. |
| 398 | ScaledOps.push_back(Op); |
| 399 | if (!Remainder->isZero()) |
| 400 | NewOps.push_back(Remainder); |
| 401 | AnyNonZeroIndices = true; |
| 402 | } else { |
| 403 | // The operand was not divisible, so add it to the list of operands |
| 404 | // we'll scan next iteration. |
| 405 | NewOps.push_back(Ops[i]); |
| 406 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 407 | } |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 408 | // If we made any changes, update Ops. |
| 409 | if (!ScaledOps.empty()) { |
| 410 | Ops = NewOps; |
| 411 | SimplifyAddOperands(Ops, Ty, SE); |
| 412 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 413 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 414 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 415 | |
| 416 | // Record the scaled array index for this level of the type. If |
| 417 | // we didn't find any operands that could be factored, tentatively |
| 418 | // assume that element zero was selected (since the zero offset |
| 419 | // would obviously be folded away). |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 420 | Value *Scaled = ScaledOps.empty() ? |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 421 | Constant::getNullValue(Ty) : |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 422 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 423 | GepIndices.push_back(Scaled); |
| 424 | |
| 425 | // Collect struct field index operands. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 426 | while (StructType *STy = dyn_cast<StructType>(ElTy)) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 427 | bool FoundFieldNo = false; |
| 428 | // An empty struct has no fields. |
| 429 | if (STy->getNumElements() == 0) break; |
| 430 | if (SE.TD) { |
| 431 | // With TargetData, field offsets are known. See if a constant offset |
| 432 | // falls within any of the struct fields. |
| 433 | if (Ops.empty()) break; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 434 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 435 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 436 | const StructLayout &SL = *SE.TD->getStructLayout(STy); |
| 437 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 438 | if (FullOffset < SL.getSizeInBytes()) { |
| 439 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 440 | GepIndices.push_back( |
| 441 | ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 442 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 443 | Ops[0] = |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 444 | SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 445 | AnyNonZeroIndices = true; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 446 | FoundFieldNo = true; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 449 | } else { |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 450 | // Without TargetData, just check for an offsetof expression of the |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 451 | // appropriate struct type. |
| 452 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 453 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 454 | Type *CTy; |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 455 | Constant *FieldNo; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 456 | if (U->isOffsetOf(CTy, FieldNo) && CTy == STy) { |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 457 | GepIndices.push_back(FieldNo); |
| 458 | ElTy = |
| 459 | STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 460 | Ops[i] = SE.getConstant(Ty, 0); |
| 461 | AnyNonZeroIndices = true; |
| 462 | FoundFieldNo = true; |
| 463 | break; |
| 464 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 465 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 466 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 467 | // If no struct field offsets were found, tentatively assume that |
| 468 | // field zero was selected (since the zero offset would obviously |
| 469 | // be folded away). |
| 470 | if (!FoundFieldNo) { |
| 471 | ElTy = STy->getTypeAtIndex(0u); |
| 472 | GepIndices.push_back( |
| 473 | Constant::getNullValue(Type::getInt32Ty(Ty->getContext()))); |
| 474 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 475 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 476 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 477 | if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 478 | ElTy = ATy->getElementType(); |
| 479 | else |
| 480 | break; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 483 | // If none of the operands were convertible to proper GEP indices, cast |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 484 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 485 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 486 | if (!AnyNonZeroIndices) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 487 | // Cast the base to i8*. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 488 | V = InsertNoopCastOfTo(V, |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 489 | Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 490 | |
| 491 | // Expand the operands for a plain byte offset. |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 492 | Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 493 | |
| 494 | // Fold a GEP with constant operands. |
| 495 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 496 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 497 | return ConstantExpr::getGetElementPtr(CLHS, CRHS); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 498 | |
| 499 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 500 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 501 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 502 | // Scanning starts from the last instruction before the insertion point. |
| 503 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 504 | if (IP != BlockBegin) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 505 | --IP; |
| 506 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | 8d50ea7 | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 507 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 508 | // generated code. |
| 509 | if (isa<DbgInfoIntrinsic>(IP)) |
| 510 | ScanLimit++; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 511 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 512 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 513 | return IP; |
| 514 | if (IP == BlockBegin) break; |
| 515 | } |
| 516 | } |
| 517 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 518 | // Save the original insertion point so we can restore it when we're done. |
| 519 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 520 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 521 | |
| 522 | // Move the insertion point out of as many loops as we can. |
| 523 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 524 | if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break; |
| 525 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 526 | if (!Preheader) break; |
| 527 | |
| 528 | // Ok, move up a level. |
| 529 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 530 | } |
| 531 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 532 | // Emit a GEP. |
| 533 | Value *GEP = Builder.CreateGEP(V, Idx, "uglygep"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 534 | rememberInstruction(GEP); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 535 | |
| 536 | // Restore the original insert point. |
| 537 | if (SaveInsertBB) |
| 538 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
| 539 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 540 | return GEP; |
| 541 | } |
| 542 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 543 | // Save the original insertion point so we can restore it when we're done. |
| 544 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 545 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 546 | |
| 547 | // Move the insertion point out of as many loops as we can. |
| 548 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 549 | if (!L->isLoopInvariant(V)) break; |
| 550 | |
| 551 | bool AnyIndexNotLoopInvariant = false; |
| 552 | for (SmallVectorImpl<Value *>::const_iterator I = GepIndices.begin(), |
| 553 | E = GepIndices.end(); I != E; ++I) |
| 554 | if (!L->isLoopInvariant(*I)) { |
| 555 | AnyIndexNotLoopInvariant = true; |
| 556 | break; |
| 557 | } |
| 558 | if (AnyIndexNotLoopInvariant) |
| 559 | break; |
| 560 | |
| 561 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 562 | if (!Preheader) break; |
| 563 | |
| 564 | // Ok, move up a level. |
| 565 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 566 | } |
| 567 | |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 568 | // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, |
| 569 | // because ScalarEvolution may have changed the address arithmetic to |
| 570 | // compute a value which is beyond the end of the allocated object. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 571 | Value *Casted = V; |
| 572 | if (V->getType() != PTy) |
| 573 | Casted = InsertNoopCastOfTo(Casted, PTy); |
| 574 | Value *GEP = Builder.CreateGEP(Casted, |
Jay Foad | 0a2a60a | 2011-07-22 08:16:57 +0000 | [diff] [blame^] | 575 | GepIndices, |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 576 | "scevgep"); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 577 | Ops.push_back(SE.getUnknown(GEP)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 578 | rememberInstruction(GEP); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 579 | |
| 580 | // Restore the original insert point. |
| 581 | if (SaveInsertBB) |
| 582 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
| 583 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 584 | return expand(SE.getAddExpr(Ops)); |
| 585 | } |
| 586 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 587 | /// isNonConstantNegative - Return true if the specified scev is negated, but |
| 588 | /// not a constant. |
| 589 | static bool isNonConstantNegative(const SCEV *F) { |
| 590 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(F); |
| 591 | if (!Mul) return false; |
| 592 | |
| 593 | // If there is a constant factor, it will be first. |
| 594 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); |
| 595 | if (!SC) return false; |
| 596 | |
| 597 | // Return true if the value is negative, this matches things like (-42 * V). |
| 598 | return SC->getValue()->getValue().isNegative(); |
| 599 | } |
| 600 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 601 | /// PickMostRelevantLoop - Given two loops pick the one that's most relevant for |
| 602 | /// SCEV expansion. If they are nested, this is the most nested. If they are |
| 603 | /// neighboring, pick the later. |
| 604 | static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B, |
| 605 | DominatorTree &DT) { |
| 606 | if (!A) return B; |
| 607 | if (!B) return A; |
| 608 | if (A->contains(B)) return B; |
| 609 | if (B->contains(A)) return A; |
| 610 | if (DT.dominates(A->getHeader(), B->getHeader())) return B; |
| 611 | if (DT.dominates(B->getHeader(), A->getHeader())) return A; |
| 612 | return A; // Arbitrarily break the tie. |
| 613 | } |
| 614 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 615 | /// getRelevantLoop - Get the most relevant loop associated with the given |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 616 | /// expression, according to PickMostRelevantLoop. |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 617 | const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) { |
| 618 | // Test whether we've already computed the most relevant loop for this SCEV. |
| 619 | std::pair<DenseMap<const SCEV *, const Loop *>::iterator, bool> Pair = |
| 620 | RelevantLoops.insert(std::make_pair(S, static_cast<const Loop *>(0))); |
| 621 | if (!Pair.second) |
| 622 | return Pair.first->second; |
| 623 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 624 | if (isa<SCEVConstant>(S)) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 625 | // A constant has no relevant loops. |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 626 | return 0; |
| 627 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 628 | if (const Instruction *I = dyn_cast<Instruction>(U->getValue())) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 629 | return Pair.first->second = SE.LI->getLoopFor(I->getParent()); |
| 630 | // A non-instruction has no relevant loops. |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) { |
| 634 | const Loop *L = 0; |
| 635 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 636 | L = AR->getLoop(); |
| 637 | for (SCEVNAryExpr::op_iterator I = N->op_begin(), E = N->op_end(); |
| 638 | I != E; ++I) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 639 | L = PickMostRelevantLoop(L, getRelevantLoop(*I), *SE.DT); |
| 640 | return RelevantLoops[N] = L; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 641 | } |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 642 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) { |
| 643 | const Loop *Result = getRelevantLoop(C->getOperand()); |
| 644 | return RelevantLoops[C] = Result; |
| 645 | } |
| 646 | if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 647 | const Loop *Result = |
| 648 | PickMostRelevantLoop(getRelevantLoop(D->getLHS()), |
| 649 | getRelevantLoop(D->getRHS()), |
| 650 | *SE.DT); |
| 651 | return RelevantLoops[D] = Result; |
| 652 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 653 | llvm_unreachable("Unexpected SCEV type!"); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 654 | return 0; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 657 | namespace { |
| 658 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 659 | /// LoopCompare - Compare loops by PickMostRelevantLoop. |
| 660 | class LoopCompare { |
| 661 | DominatorTree &DT; |
| 662 | public: |
| 663 | explicit LoopCompare(DominatorTree &dt) : DT(dt) {} |
| 664 | |
| 665 | bool operator()(std::pair<const Loop *, const SCEV *> LHS, |
| 666 | std::pair<const Loop *, const SCEV *> RHS) const { |
Dan Gohman | bb5d927 | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 667 | // Keep pointer operands sorted at the end. |
| 668 | if (LHS.second->getType()->isPointerTy() != |
| 669 | RHS.second->getType()->isPointerTy()) |
| 670 | return LHS.second->getType()->isPointerTy(); |
| 671 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 672 | // Compare loops with PickMostRelevantLoop. |
| 673 | if (LHS.first != RHS.first) |
| 674 | return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first; |
| 675 | |
| 676 | // If one operand is a non-constant negative and the other is not, |
| 677 | // put the non-constant negative on the right so that a sub can |
| 678 | // be used instead of a negate and add. |
| 679 | if (isNonConstantNegative(LHS.second)) { |
| 680 | if (!isNonConstantNegative(RHS.second)) |
| 681 | return false; |
| 682 | } else if (isNonConstantNegative(RHS.second)) |
| 683 | return true; |
| 684 | |
| 685 | // Otherwise they are equivalent according to this comparison. |
| 686 | return false; |
| 687 | } |
| 688 | }; |
| 689 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 692 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 693 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 694 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 695 | // Collect all the add operands in a loop, along with their associated loops. |
| 696 | // Iterate in reverse so that constants are emitted last, all else equal, and |
| 697 | // so that pointer operands are inserted first, which the code below relies on |
| 698 | // to form more involved GEPs. |
| 699 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 700 | for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()), |
| 701 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 702 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 703 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 704 | // Sort by loop. Use a stable sort so that constants follow non-constants and |
| 705 | // pointer operands precede non-pointer operands. |
| 706 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 707 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 708 | // Emit instructions to add all the operands. Hoist as much as possible |
| 709 | // out of loops, and form meaningful getelementptrs where possible. |
| 710 | Value *Sum = 0; |
| 711 | for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator |
| 712 | I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { |
| 713 | const Loop *CurLoop = I->first; |
| 714 | const SCEV *Op = I->second; |
| 715 | if (!Sum) { |
| 716 | // This is the first operand. Just expand it. |
| 717 | Sum = expand(Op); |
| 718 | ++I; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 719 | } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 720 | // The running sum expression is a pointer. Try to form a getelementptr |
| 721 | // at this level with that as the base. |
| 722 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | bb5d927 | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 723 | for (; I != E && I->first == CurLoop; ++I) { |
| 724 | // If the operand is SCEVUnknown and not instructions, peek through |
| 725 | // it, to enable more of it to be folded into the GEP. |
| 726 | const SCEV *X = I->second; |
| 727 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X)) |
| 728 | if (!isa<Instruction>(U->getValue())) |
| 729 | X = SE.getSCEV(U->getValue()); |
| 730 | NewOps.push_back(X); |
| 731 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 732 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 733 | } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 734 | // The running sum is an integer, and there's a pointer at this level. |
Dan Gohman | f8d0578 | 2010-04-09 19:14:31 +0000 | [diff] [blame] | 735 | // Try to form a getelementptr. If the running sum is instructions, |
| 736 | // use a SCEVUnknown to avoid re-analyzing them. |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 737 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | f8d0578 | 2010-04-09 19:14:31 +0000 | [diff] [blame] | 738 | NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) : |
| 739 | SE.getSCEV(Sum)); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 740 | for (++I; I != E && I->first == CurLoop; ++I) |
| 741 | NewOps.push_back(I->second); |
| 742 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op)); |
| 743 | } else if (isNonConstantNegative(Op)) { |
| 744 | // Instead of doing a negate and add, just do a subtract. |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 745 | Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 746 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 747 | Sum = InsertBinop(Instruction::Sub, Sum, W); |
| 748 | ++I; |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 749 | } else { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 750 | // A simple add. |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 751 | Value *W = expandCodeFor(Op, Ty); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 752 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 753 | // Canonicalize a constant to the RHS. |
| 754 | if (isa<Constant>(Sum)) std::swap(Sum, W); |
| 755 | Sum = InsertBinop(Instruction::Add, Sum, W); |
| 756 | ++I; |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 759 | |
| 760 | return Sum; |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 761 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 762 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 763 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 764 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 765 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 766 | // Collect all the mul operands in a loop, along with their associated loops. |
| 767 | // Iterate in reverse so that constants are emitted last, all else equal. |
| 768 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 769 | for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()), |
| 770 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 771 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 772 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 773 | // Sort by loop. Use a stable sort so that constants follow non-constants. |
| 774 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); |
| 775 | |
| 776 | // Emit instructions to mul all the operands. Hoist as much as possible |
| 777 | // out of loops. |
| 778 | Value *Prod = 0; |
| 779 | for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator |
| 780 | I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { |
| 781 | const SCEV *Op = I->second; |
| 782 | if (!Prod) { |
| 783 | // This is the first operand. Just expand it. |
| 784 | Prod = expand(Op); |
| 785 | ++I; |
| 786 | } else if (Op->isAllOnesValue()) { |
| 787 | // Instead of doing a multiply by negative one, just do a negate. |
| 788 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 789 | Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod); |
| 790 | ++I; |
| 791 | } else { |
| 792 | // A simple mul. |
| 793 | Value *W = expandCodeFor(Op, Ty); |
| 794 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 795 | // Canonicalize a constant to the RHS. |
| 796 | if (isa<Constant>(Prod)) std::swap(Prod, W); |
| 797 | Prod = InsertBinop(Instruction::Mul, Prod, W); |
| 798 | ++I; |
| 799 | } |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 802 | return Prod; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 805 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 806 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 807 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 808 | Value *LHS = expandCodeFor(S->getLHS(), Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 809 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 810 | const APInt &RHS = SC->getValue()->getValue(); |
| 811 | if (RHS.isPowerOf2()) |
| 812 | return InsertBinop(Instruction::LShr, LHS, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 813 | ConstantInt::get(Ty, RHS.logBase2())); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 816 | Value *RHS = expandCodeFor(S->getRHS(), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 817 | return InsertBinop(Instruction::UDiv, LHS, RHS); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 820 | /// Move parts of Base into Rest to leave Base with the minimal |
| 821 | /// expression that provides a pointer operand suitable for a |
| 822 | /// GEP expansion. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 823 | static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 824 | ScalarEvolution &SE) { |
| 825 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 826 | Base = A->getStart(); |
| 827 | Rest = SE.getAddExpr(Rest, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 828 | SE.getAddRecExpr(SE.getConstant(A->getType(), 0), |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 829 | A->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 830 | A->getLoop(), |
| 831 | // FIXME: A->getNoWrapFlags(FlagNW) |
| 832 | SCEV::FlagAnyWrap)); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 833 | } |
| 834 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 835 | Base = A->getOperand(A->getNumOperands()-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 836 | SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end()); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 837 | NewAddOps.back() = Rest; |
| 838 | Rest = SE.getAddExpr(NewAddOps); |
| 839 | ExposePointerBase(Base, Rest, SE); |
| 840 | } |
| 841 | } |
| 842 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 843 | /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand |
| 844 | /// the base addrec, which is the addrec without any non-loop-dominating |
| 845 | /// values, and return the PHI. |
| 846 | PHINode * |
| 847 | SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, |
| 848 | const Loop *L, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 849 | Type *ExpandTy, |
| 850 | Type *IntTy) { |
Benjamin Kramer | 93a896e | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 851 | assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position"); |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 852 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 853 | // Reuse a previously-inserted PHI, if present. |
| 854 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 855 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 856 | if (SE.isSCEVable(PN->getType()) && |
| 857 | (SE.getEffectiveSCEVType(PN->getType()) == |
| 858 | SE.getEffectiveSCEVType(Normalized->getType())) && |
| 859 | SE.getSCEV(PN) == Normalized) |
| 860 | if (BasicBlock *LatchBlock = L->getLoopLatch()) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 861 | Instruction *IncV = |
Dan Gohman | 22e6219 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 862 | cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)); |
| 863 | |
| 864 | // Determine if this is a well-behaved chain of instructions leading |
| 865 | // back to the PHI. It probably will be, if we're scanning an inner |
| 866 | // loop already visited by LSR for example, but it wouldn't have |
| 867 | // to be. |
| 868 | do { |
Dan Gohman | 0cbe91b | 2011-03-02 01:34:10 +0000 | [diff] [blame] | 869 | if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) || |
Dan Gohman | a7a841a | 2011-03-04 20:46:46 +0000 | [diff] [blame] | 870 | (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV))) { |
Dan Gohman | 22e6219 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 871 | IncV = 0; |
| 872 | break; |
| 873 | } |
Dan Gohman | 9feae9f | 2010-02-17 02:39:31 +0000 | [diff] [blame] | 874 | // If any of the operands don't dominate the insert position, bail. |
| 875 | // Addrec operands are always loop-invariant, so this can only happen |
| 876 | // if there are instructions which haven't been hoisted. |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 877 | if (L == IVIncInsertLoop) { |
| 878 | for (User::op_iterator OI = IncV->op_begin()+1, |
| 879 | OE = IncV->op_end(); OI != OE; ++OI) |
| 880 | if (Instruction *OInst = dyn_cast<Instruction>(OI)) |
| 881 | if (!SE.DT->dominates(OInst, IVIncInsertPos)) { |
| 882 | IncV = 0; |
| 883 | break; |
| 884 | } |
| 885 | } |
Dan Gohman | 9feae9f | 2010-02-17 02:39:31 +0000 | [diff] [blame] | 886 | if (!IncV) |
| 887 | break; |
| 888 | // Advance to the next instruction. |
Dan Gohman | 22e6219 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 889 | IncV = dyn_cast<Instruction>(IncV->getOperand(0)); |
| 890 | if (!IncV) |
| 891 | break; |
| 892 | if (IncV->mayHaveSideEffects()) { |
| 893 | IncV = 0; |
| 894 | break; |
| 895 | } |
| 896 | } while (IncV != PN); |
| 897 | |
| 898 | if (IncV) { |
| 899 | // Ok, the add recurrence looks usable. |
| 900 | // Remember this PHI, even in post-inc mode. |
| 901 | InsertedValues.insert(PN); |
| 902 | // Remember the increment. |
| 903 | IncV = cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)); |
| 904 | rememberInstruction(IncV); |
| 905 | if (L == IVIncInsertLoop) |
| 906 | do { |
| 907 | if (SE.DT->dominates(IncV, IVIncInsertPos)) |
| 908 | break; |
| 909 | // Make sure the increment is where we want it. But don't move it |
| 910 | // down past a potential existing post-inc user. |
| 911 | IncV->moveBefore(IVIncInsertPos); |
| 912 | IVIncInsertPos = IncV; |
| 913 | IncV = cast<Instruction>(IncV->getOperand(0)); |
| 914 | } while (IncV != PN); |
| 915 | return PN; |
| 916 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 917 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 918 | |
| 919 | // Save the original insertion point so we can restore it when we're done. |
| 920 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 921 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 922 | |
| 923 | // Expand code for the start value. |
| 924 | Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, |
| 925 | L->getHeader()->begin()); |
| 926 | |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 927 | // StartV must be hoisted into L's preheader to dominate the new phi. |
Benjamin Kramer | 93a896e | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 928 | assert(!isa<Instruction>(StartV) || |
| 929 | SE.DT->properlyDominates(cast<Instruction>(StartV)->getParent(), |
| 930 | L->getHeader())); |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 931 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 932 | // Expand code for the step value. Insert instructions right before the |
| 933 | // terminator corresponding to the back-edge. Do this before creating the PHI |
| 934 | // so that PHI reuse code doesn't see an incomplete PHI. If the stride is |
| 935 | // negative, insert a sub instead of an add for the increment (unless it's a |
| 936 | // constant, because subtracts of constants are canonicalized to adds). |
| 937 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 938 | bool isPointer = ExpandTy->isPointerTy(); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 939 | bool isNegative = !isPointer && isNonConstantNegative(Step); |
| 940 | if (isNegative) |
| 941 | Step = SE.getNegativeSCEV(Step); |
| 942 | Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); |
| 943 | |
| 944 | // Create the PHI. |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 945 | BasicBlock *Header = L->getHeader(); |
| 946 | Builder.SetInsertPoint(Header, Header->begin()); |
| 947 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Andrew Trick | 5e7645b | 2011-06-28 05:07:32 +0000 | [diff] [blame] | 948 | PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE), |
Andrew Trick | dc8e546 | 2011-06-28 05:41:52 +0000 | [diff] [blame] | 949 | Twine(IVName) + ".iv"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 950 | rememberInstruction(PN); |
| 951 | |
| 952 | // Create the step instructions and populate the PHI. |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 953 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 954 | BasicBlock *Pred = *HPI; |
| 955 | |
| 956 | // Add a start value. |
| 957 | if (!L->contains(Pred)) { |
| 958 | PN->addIncoming(StartV, Pred); |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | // Create a step value and add it to the PHI. If IVIncInsertLoop is |
| 963 | // non-null and equal to the addrec's loop, insert the instructions |
| 964 | // at IVIncInsertPos. |
| 965 | Instruction *InsertPos = L == IVIncInsertLoop ? |
| 966 | IVIncInsertPos : Pred->getTerminator(); |
Devang Patel | c5ecbdc | 2011-07-05 21:48:22 +0000 | [diff] [blame] | 967 | Builder.SetInsertPoint(InsertPos); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 968 | Value *IncV; |
| 969 | // If the PHI is a pointer, use a GEP, otherwise use an add or sub. |
| 970 | if (isPointer) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 971 | PointerType *GEPPtrTy = cast<PointerType>(ExpandTy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 972 | // If the step isn't constant, don't use an implicitly scaled GEP, because |
| 973 | // that would require a multiply inside the loop. |
| 974 | if (!isa<ConstantInt>(StepV)) |
| 975 | GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()), |
| 976 | GEPPtrTy->getAddressSpace()); |
| 977 | const SCEV *const StepArray[1] = { SE.getSCEV(StepV) }; |
| 978 | IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN); |
| 979 | if (IncV->getType() != PN->getType()) { |
| 980 | IncV = Builder.CreateBitCast(IncV, PN->getType(), "tmp"); |
| 981 | rememberInstruction(IncV); |
| 982 | } |
| 983 | } else { |
| 984 | IncV = isNegative ? |
Andrew Trick | dc8e546 | 2011-06-28 05:41:52 +0000 | [diff] [blame] | 985 | Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") : |
| 986 | Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 987 | rememberInstruction(IncV); |
| 988 | } |
| 989 | PN->addIncoming(IncV, Pred); |
| 990 | } |
| 991 | |
| 992 | // Restore the original insert point. |
| 993 | if (SaveInsertBB) |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 994 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 995 | |
| 996 | // Remember this PHI, even in post-inc mode. |
| 997 | InsertedValues.insert(PN); |
| 998 | |
| 999 | return PN; |
| 1000 | } |
| 1001 | |
| 1002 | Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1003 | Type *STy = S->getType(); |
| 1004 | Type *IntTy = SE.getEffectiveSCEVType(STy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1005 | const Loop *L = S->getLoop(); |
| 1006 | |
| 1007 | // Determine a normalized form of this expression, which is the expression |
| 1008 | // before any post-inc adjustment is made. |
| 1009 | const SCEVAddRecExpr *Normalized = S; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1010 | if (PostIncLoops.count(L)) { |
| 1011 | PostIncLoopSet Loops; |
| 1012 | Loops.insert(L); |
| 1013 | Normalized = |
| 1014 | cast<SCEVAddRecExpr>(TransformForPostIncUse(Normalize, S, 0, 0, |
| 1015 | Loops, SE, *SE.DT)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Strip off any non-loop-dominating component from the addrec start. |
| 1019 | const SCEV *Start = Normalized->getStart(); |
| 1020 | const SCEV *PostLoopOffset = 0; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1021 | if (!SE.properlyDominates(Start, L->getHeader())) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1022 | PostLoopOffset = Start; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1023 | Start = SE.getConstant(Normalized->getType(), 0); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1024 | Normalized = cast<SCEVAddRecExpr>( |
| 1025 | SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE), |
| 1026 | Normalized->getLoop(), |
| 1027 | // FIXME: Normalized->getNoWrapFlags(FlagNW) |
| 1028 | SCEV::FlagAnyWrap)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | // Strip off any non-loop-dominating component from the addrec step. |
| 1032 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
| 1033 | const SCEV *PostLoopScale = 0; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1034 | if (!SE.dominates(Step, L->getHeader())) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1035 | PostLoopScale = Step; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1036 | Step = SE.getConstant(Normalized->getType(), 1); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1037 | Normalized = |
| 1038 | cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start, Step, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1039 | Normalized->getLoop(), |
| 1040 | // FIXME: Normalized |
| 1041 | // ->getNoWrapFlags(FlagNW) |
| 1042 | SCEV::FlagAnyWrap)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Expand the core addrec. If we need post-loop scaling, force it to |
| 1046 | // expand to an integer type to avoid the need for additional casting. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1047 | Type *ExpandTy = PostLoopScale ? IntTy : STy; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1048 | PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy); |
| 1049 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1050 | // Accommodate post-inc mode, if necessary. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1051 | Value *Result; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1052 | if (!PostIncLoops.count(L)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1053 | Result = PN; |
| 1054 | else { |
| 1055 | // In PostInc mode, use the post-incremented value. |
| 1056 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 1057 | assert(LatchBlock && "PostInc mode requires a unique loop latch!"); |
| 1058 | Result = PN->getIncomingValueForBlock(LatchBlock); |
| 1059 | } |
| 1060 | |
| 1061 | // Re-apply any non-loop-dominating scale. |
| 1062 | if (PostLoopScale) { |
Dan Gohman | 0a799ab | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1063 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1064 | Result = Builder.CreateMul(Result, |
| 1065 | expandCodeFor(PostLoopScale, IntTy)); |
| 1066 | rememberInstruction(Result); |
| 1067 | } |
| 1068 | |
| 1069 | // Re-apply any non-loop-dominating offset. |
| 1070 | if (PostLoopOffset) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1071 | if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1072 | const SCEV *const OffsetArray[1] = { PostLoopOffset }; |
| 1073 | Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result); |
| 1074 | } else { |
Dan Gohman | 0a799ab | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1075 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1076 | Result = Builder.CreateAdd(Result, |
| 1077 | expandCodeFor(PostLoopOffset, IntTy)); |
| 1078 | rememberInstruction(Result); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return Result; |
| 1083 | } |
| 1084 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1085 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1086 | if (!CanonicalMode) return expandAddRecExprLiterally(S); |
| 1087 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1088 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1089 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1090 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1091 | // First check for an existing canonical IV in a suitable type. |
| 1092 | PHINode *CanonicalIV = 0; |
| 1093 | if (PHINode *PN = L->getCanonicalInductionVariable()) |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1094 | if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1095 | CanonicalIV = PN; |
| 1096 | |
| 1097 | // Rewrite an AddRec in terms of the canonical induction variable, if |
| 1098 | // its type is more narrow. |
| 1099 | if (CanonicalIV && |
| 1100 | SE.getTypeSizeInBits(CanonicalIV->getType()) > |
| 1101 | SE.getTypeSizeInBits(Ty)) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1102 | SmallVector<const SCEV *, 4> NewOps(S->getNumOperands()); |
| 1103 | for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i) |
| 1104 | NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType()); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1105 | Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(), |
| 1106 | // FIXME: S->getNoWrapFlags(FlagNW) |
| 1107 | SCEV::FlagAnyWrap)); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1108 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 1109 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1110 | BasicBlock::iterator NewInsertPt = |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1111 | llvm::next(BasicBlock::iterator(cast<Instruction>(V))); |
Jim Grosbach | 08f55d0 | 2010-06-16 21:13:38 +0000 | [diff] [blame] | 1112 | while (isa<PHINode>(NewInsertPt) || isa<DbgInfoIntrinsic>(NewInsertPt)) |
| 1113 | ++NewInsertPt; |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1114 | V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0, |
| 1115 | NewInsertPt); |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1116 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1117 | return V; |
| 1118 | } |
| 1119 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1120 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1121 | if (!S->getStart()->isZero()) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1122 | SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end()); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1123 | NewOps[0] = SE.getConstant(Ty, 0); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1124 | // FIXME: can use S->getNoWrapFlags() |
| 1125 | const SCEV *Rest = SE.getAddRecExpr(NewOps, L, SCEV::FlagAnyWrap); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 1128 | // comments on expandAddToGEP for details. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1129 | const SCEV *Base = S->getStart(); |
| 1130 | const SCEV *RestArray[1] = { Rest }; |
| 1131 | // Dig into the expression to find the pointer base for a GEP. |
| 1132 | ExposePointerBase(Base, RestArray[0], SE); |
| 1133 | // If we found a pointer, expand the AddRec with a GEP. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1134 | if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1135 | // Make sure the Base isn't something exotic, such as a multiplied |
| 1136 | // or divided pointer value. In those cases, the result type isn't |
| 1137 | // actually a pointer type. |
| 1138 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 1139 | Value *StartV = expand(Base); |
| 1140 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
| 1141 | return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1145 | // Just do a normal add. Pre-expand the operands to suppress folding. |
| 1146 | return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())), |
| 1147 | SE.getUnknown(expand(Rest)))); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1150 | // If we don't yet have a canonical IV, create one. |
| 1151 | if (!CanonicalIV) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1152 | // Create and insert the PHI node for the induction variable in the |
| 1153 | // specified loop. |
| 1154 | BasicBlock *Header = L->getHeader(); |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1155 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1156 | CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar", |
| 1157 | Header->begin()); |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1158 | rememberInstruction(CanonicalIV); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1159 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1160 | Constant *One = ConstantInt::get(Ty, 1); |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1161 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1162 | BasicBlock *HP = *HPI; |
| 1163 | if (L->contains(HP)) { |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 1164 | // Insert a unit add instruction right before the terminator |
| 1165 | // corresponding to the back-edge. |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1166 | Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One, |
| 1167 | "indvar.next", |
| 1168 | HP->getTerminator()); |
Devang Patel | df3ad66 | 2011-06-22 20:56:56 +0000 | [diff] [blame] | 1169 | Add->setDebugLoc(HP->getTerminator()->getDebugLoc()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1170 | rememberInstruction(Add); |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1171 | CanonicalIV->addIncoming(Add, HP); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1172 | } else { |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1173 | CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1174 | } |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1175 | } |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1178 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 1179 | if (S->isAffine() && S->getOperand(1)->isOne()) { |
| 1180 | assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && |
| 1181 | "IVs with types different from the canonical IV should " |
| 1182 | "already have been handled!"); |
| 1183 | return CanonicalIV; |
| 1184 | } |
| 1185 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1186 | // {0,+,F} --> {0,+,1} * F |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 1188 | // 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] | 1189 | if (S->isAffine()) // {0,+,F} --> i*F |
| 1190 | return |
| 1191 | expand(SE.getTruncateOrNoop( |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1192 | SE.getMulExpr(SE.getUnknown(CanonicalIV), |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1193 | SE.getNoopOrAnyExtend(S->getOperand(1), |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1194 | CanonicalIV->getType())), |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1195 | Ty)); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1196 | |
| 1197 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 1198 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 1199 | // simplify the expression without having to build a bunch of special code |
| 1200 | // into this folder. |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1201 | const SCEV *IH = SE.getUnknown(CanonicalIV); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1202 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1203 | // 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] | 1204 | const SCEV *NewS = S; |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1205 | const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType()); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1206 | if (isa<SCEVAddRecExpr>(Ext)) |
| 1207 | NewS = Ext; |
| 1208 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1209 | const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1210 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1211 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1212 | // Truncate the result down to the original type, if needed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1213 | const SCEV *T = SE.getTruncateOrNoop(V, Ty); |
Dan Gohman | 469f3cd | 2009-06-22 22:08:45 +0000 | [diff] [blame] | 1214 | return expand(T); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1215 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1216 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1217 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1218 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1219 | Value *V = expandCodeFor(S->getOperand(), |
| 1220 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1221 | Value *I = Builder.CreateTrunc(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1222 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1223 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1226 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1227 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1228 | Value *V = expandCodeFor(S->getOperand(), |
| 1229 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1230 | Value *I = Builder.CreateZExt(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1231 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1232 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1235 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1236 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1237 | Value *V = expandCodeFor(S->getOperand(), |
| 1238 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1239 | Value *I = Builder.CreateSExt(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1240 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1241 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1244 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1245 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1246 | Type *Ty = LHS->getType(); |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1247 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1248 | // In the case of mixed integer and pointer types, do the |
| 1249 | // rest of the comparisons as integer. |
| 1250 | if (S->getOperand(i)->getType() != Ty) { |
| 1251 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1252 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1253 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1254 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1255 | Value *ICmp = Builder.CreateICmpSGT(LHS, RHS, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1256 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1257 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1258 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1259 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1260 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1261 | // In the case of mixed integer and pointer types, cast the |
| 1262 | // final result back to the pointer type. |
| 1263 | if (LHS->getType() != S->getType()) |
| 1264 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1265 | return LHS; |
| 1266 | } |
| 1267 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1268 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1269 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1270 | Type *Ty = LHS->getType(); |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1271 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1272 | // In the case of mixed integer and pointer types, do the |
| 1273 | // rest of the comparisons as integer. |
| 1274 | if (S->getOperand(i)->getType() != Ty) { |
| 1275 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1276 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1277 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1278 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1279 | Value *ICmp = Builder.CreateICmpUGT(LHS, RHS, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1280 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1281 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1282 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1283 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1284 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1285 | // In the case of mixed integer and pointer types, cast the |
| 1286 | // final result back to the pointer type. |
| 1287 | if (LHS->getType() != S->getType()) |
| 1288 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1289 | return LHS; |
| 1290 | } |
| 1291 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1292 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, |
Dan Gohman | 6c7ed6b | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1293 | Instruction *I) { |
| 1294 | BasicBlock::iterator IP = I; |
| 1295 | while (isInsertedInstruction(IP) || isa<DbgInfoIntrinsic>(IP)) |
| 1296 | ++IP; |
| 1297 | Builder.SetInsertPoint(IP->getParent(), IP); |
| 1298 | return expandCodeFor(SH, Ty); |
| 1299 | } |
| 1300 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1301 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1302 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1303 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 1304 | if (Ty) { |
| 1305 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 1306 | "non-trivial casts should be done with the SCEVs directly!"); |
| 1307 | V = InsertNoopCastOfTo(V, Ty); |
| 1308 | } |
| 1309 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1312 | Value *SCEVExpander::expand(const SCEV *S) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1313 | // Compute an insertion point for this SCEV object. Hoist the instructions |
| 1314 | // as far out in the loop nest as possible. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1315 | Instruction *InsertPt = Builder.GetInsertPoint(); |
| 1316 | for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1317 | L = L->getParentLoop()) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1318 | if (SE.isLoopInvariant(S, L)) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1319 | if (!L) break; |
Dan Gohman | e059ee8 | 2010-03-23 21:53:22 +0000 | [diff] [blame] | 1320 | if (BasicBlock *Preheader = L->getLoopPreheader()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1321 | InsertPt = Preheader->getTerminator(); |
| 1322 | } else { |
| 1323 | // If the SCEV is computable at this level, insert it into the header |
| 1324 | // after the PHIs (and after any other instructions that we've inserted |
| 1325 | // there) so that it is guaranteed to dominate any user inside the loop. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1326 | if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1327 | InsertPt = L->getHeader()->getFirstNonPHI(); |
Dan Gohman | 6c7ed6b | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1328 | while (isInsertedInstruction(InsertPt) || isa<DbgInfoIntrinsic>(InsertPt)) |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1329 | InsertPt = llvm::next(BasicBlock::iterator(InsertPt)); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1332 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1333 | // Check to see if we already expanded this here. |
| 1334 | std::map<std::pair<const SCEV *, Instruction *>, |
| 1335 | AssertingVH<Value> >::iterator I = |
| 1336 | InsertedExpressions.find(std::make_pair(S, InsertPt)); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1337 | if (I != InsertedExpressions.end()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1338 | return I->second; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1339 | |
| 1340 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 1341 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 1342 | Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Expand the expression into instructions. |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1345 | Value *V = visit(S); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1346 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1347 | // Remember the expanded value for this SCEV at this location. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1348 | if (PostIncLoops.empty()) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1349 | InsertedExpressions[std::make_pair(S, InsertPt)] = V; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1350 | |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1351 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1352 | return V; |
| 1353 | } |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1354 | |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1355 | void SCEVExpander::rememberInstruction(Value *I) { |
Dan Gohman | 25fcaff | 2010-06-05 00:33:07 +0000 | [diff] [blame] | 1356 | if (!PostIncLoops.empty()) |
| 1357 | InsertedPostIncValues.insert(I); |
| 1358 | else |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1359 | InsertedValues.insert(I); |
| 1360 | |
| 1361 | // If we just claimed an existing instruction and that instruction had |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1362 | // been the insert point, adjust the insert point forward so that |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1363 | // subsequently inserted code will be dominated. |
| 1364 | if (Builder.GetInsertPoint() == I) { |
| 1365 | BasicBlock::iterator It = cast<Instruction>(I); |
Dan Gohman | 6c7ed6b | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1366 | do { ++It; } while (isInsertedInstruction(It) || |
| 1367 | isa<DbgInfoIntrinsic>(It)); |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1368 | Builder.SetInsertPoint(Builder.GetInsertBlock(), It); |
| 1369 | } |
| 1370 | } |
| 1371 | |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1372 | void SCEVExpander::restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I) { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1373 | // If we acquired more instructions since the old insert point was saved, |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1374 | // advance past them. |
Dan Gohman | 6c7ed6b | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1375 | while (isInsertedInstruction(I) || isa<DbgInfoIntrinsic>(I)) ++I; |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1376 | |
| 1377 | Builder.SetInsertPoint(BB, I); |
| 1378 | } |
| 1379 | |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1380 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 1381 | /// canonical induction variable of the specified type for the specified |
| 1382 | /// loop (inserting one if there is none). A canonical induction variable |
| 1383 | /// starts at zero and steps by one on each iteration. |
Dan Gohman | 7c58dbd | 2010-07-20 16:44:52 +0000 | [diff] [blame] | 1384 | PHINode * |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1385 | SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1386 | Type *Ty) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1387 | assert(Ty->isIntegerTy() && "Can only insert integer induction variables!"); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Build a SCEV for {0,+,1}<L>. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1390 | // Conservatively use FlagAnyWrap for now. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1391 | const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1392 | SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Emit code for it. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1395 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 1396 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
Dan Gohman | 7c58dbd | 2010-07-20 16:44:52 +0000 | [diff] [blame] | 1397 | PHINode *V = cast<PHINode>(expandCodeFor(H, 0, L->getHeader()->begin())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1398 | if (SaveInsertBB) |
Dan Gohman | 4559855 | 2010-02-15 00:21:43 +0000 | [diff] [blame] | 1399 | restoreInsertPoint(SaveInsertBB, SaveInsertPt); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1400 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1401 | return V; |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1402 | } |