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