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