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