Nate Begeman | 2bca4d9 | 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 | f3ebc3f | 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 | 2bca4d9 | 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 | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IntrinsicInst.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Andrew Trick | 244e2c3 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 26 | |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Gabor Greif | 8e66a42 | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 29 | /// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP, |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 30 | /// reusing an existing cast if a suitable one exists, moving an existing |
| 31 | /// cast if a suitable one exists but isn't in the right place, or |
Gabor Greif | 8e66a42 | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 32 | /// creating a new one. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 33 | Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty, |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 34 | Instruction::CastOps Op, |
| 35 | BasicBlock::iterator IP) { |
Rafael Espindola | cd06b48 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 36 | // This function must be called with the builder having a valid insertion |
| 37 | // point. It doesn't need to be the actual IP where the uses of the returned |
| 38 | // cast will be added, but it must dominate such IP. |
Rafael Espindola | 09a4201 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 39 | // We use this precondition to produce a cast that will dominate all its |
| 40 | // uses. In particular, this is crucial for the case where the builder's |
| 41 | // insertion point *is* the point where we were asked to put the cast. |
Sylvestre Ledru | 35521e2 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 42 | // Since we don't know the builder's insertion point is actually |
Rafael Espindola | cd06b48 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 43 | // where the uses will be added (only that it dominates it), we are |
| 44 | // not allowed to move it. |
| 45 | BasicBlock::iterator BIP = Builder.GetInsertPoint(); |
| 46 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 47 | Instruction *Ret = nullptr; |
Rafael Espindola | 82d9575 | 2012-02-18 17:22:58 +0000 | [diff] [blame] | 48 | |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 49 | // Check to see if there is already a cast! |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 50 | for (User *U : V->users()) |
Gabor Greif | 3b740e9 | 2010-07-09 16:39:02 +0000 | [diff] [blame] | 51 | if (U->getType() == Ty) |
Gabor Greif | 8e66a42 | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 52 | if (CastInst *CI = dyn_cast<CastInst>(U)) |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 53 | if (CI->getOpcode() == Op) { |
Rafael Espindola | 337cfaf | 2012-02-22 03:44:46 +0000 | [diff] [blame] | 54 | // If the cast isn't where we want it, create a new cast at IP. |
| 55 | // Likewise, do not reuse a cast at BIP because it must dominate |
| 56 | // instructions that might be inserted before BIP. |
Rafael Espindola | cd06b48 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 57 | if (BasicBlock::iterator(CI) != IP || BIP == IP) { |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 58 | // Create a new cast, and leave the old cast in place in case |
| 59 | // it is being used as an insert point. Clear its operand |
| 60 | // so that it doesn't hold anything live. |
Rafael Espindola | 09a4201 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 61 | Ret = CastInst::Create(Op, V, Ty, "", IP); |
| 62 | Ret->takeName(CI); |
| 63 | CI->replaceAllUsesWith(Ret); |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 64 | CI->setOperand(0, UndefValue::get(V->getType())); |
Rafael Espindola | 09a4201 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 65 | break; |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 66 | } |
Rafael Espindola | 09a4201 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 67 | Ret = CI; |
| 68 | break; |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Create a new cast. |
Rafael Espindola | 09a4201 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 72 | if (!Ret) |
| 73 | Ret = CastInst::Create(Op, V, Ty, V->getName(), IP); |
| 74 | |
| 75 | // We assert at the end of the function since IP might point to an |
| 76 | // instruction with different dominance properties than a cast |
| 77 | // (an invoke for example) and not dominate BIP (but the cast does). |
| 78 | assert(SE.DT->dominates(Ret, BIP)); |
| 79 | |
| 80 | rememberInstruction(Ret); |
| 81 | return Ret; |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 84 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 85 | /// which must be possible with a noop cast, doing what we can to share |
| 86 | /// the casts. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 87 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) { |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 88 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 89 | assert((Op == Instruction::BitCast || |
| 90 | Op == Instruction::PtrToInt || |
| 91 | Op == Instruction::IntToPtr) && |
| 92 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 93 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 94 | "InsertNoopCastOfTo cannot change sizes!"); |
| 95 | |
Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 96 | // Short-circuit unnecessary bitcasts. |
Andrew Trick | e0ced62 | 2011-12-14 22:07:19 +0000 | [diff] [blame] | 97 | if (Op == Instruction::BitCast) { |
| 98 | if (V->getType() == Ty) |
| 99 | return V; |
| 100 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 101 | if (CI->getOperand(0)->getType() == Ty) |
| 102 | return CI->getOperand(0); |
| 103 | } |
| 104 | } |
Dan Gohman | 66e038a | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 105 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 106 | if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) && |
Dan Gohman | 150b4c3 | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 107 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 108 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 109 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 110 | CI->getOpcode() == Instruction::IntToPtr) && |
| 111 | SE.getTypeSizeInBits(CI->getType()) == |
| 112 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 113 | return CI->getOperand(0); |
Dan Gohman | 150b4c3 | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 114 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 115 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 116 | CE->getOpcode() == Instruction::IntToPtr) && |
| 117 | SE.getTypeSizeInBits(CE->getType()) == |
| 118 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 119 | return CE->getOperand(0); |
| 120 | } |
Dan Gohman | 66e038a | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 121 | |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 122 | // Fold a cast of a constant. |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 123 | if (Constant *C = dyn_cast<Constant>(V)) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 124 | return ConstantExpr::getCast(Op, C, Ty); |
Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 125 | |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 126 | // Cast the argument at the beginning of the entry block, after |
| 127 | // any bitcasts of other arguments. |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 128 | if (Argument *A = dyn_cast<Argument>(V)) { |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 129 | BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin(); |
| 130 | while ((isa<BitCastInst>(IP) && |
| 131 | isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) && |
| 132 | cast<BitCastInst>(IP)->getOperand(0) != A) || |
Bill Wendling | 86c5cbe | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 133 | isa<DbgInfoIntrinsic>(IP) || |
| 134 | isa<LandingPadInst>(IP)) |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 135 | ++IP; |
| 136 | return ReuseOrCreateCast(A, Ty, Op, IP); |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 137 | } |
Wojciech Matyjewicz | 784d071e1 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 138 | |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 139 | // Cast the instruction immediately after the instruction. |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 140 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 141 | BasicBlock::iterator IP = I; ++IP; |
| 142 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 143 | IP = II->getNormalDest()->begin(); |
Rafael Espindola | 82d9575 | 2012-02-18 17:22:58 +0000 | [diff] [blame] | 144 | while (isa<PHINode>(IP) || isa<LandingPadInst>(IP)) |
Bill Wendling | 86c5cbe | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 145 | ++IP; |
Dan Gohman | d277246 | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 146 | return ReuseOrCreateCast(I, Ty, Op, IP); |
Chris Lattner | a6da69c | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | e71f144 | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 149 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 150 | /// of work to avoid inserting an obviously redundant operation. |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 151 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, |
| 152 | Value *LHS, Value *RHS) { |
Dan Gohman | 00cb117 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 153 | // Fold a binop with constant operands. |
| 154 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 155 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 156 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
Dan Gohman | 00cb117 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 157 | |
Chris Lattner | e71f144 | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 158 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 159 | unsigned ScanLimit = 6; |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 160 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 161 | // Scanning starts from the last instruction before the insertion point. |
| 162 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 163 | if (IP != BlockBegin) { |
Wojciech Matyjewicz | ae9753b2 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 164 | --IP; |
| 165 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | f5cc1cd | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 166 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 167 | // generated code. |
| 168 | if (isa<DbgInfoIntrinsic>(IP)) |
| 169 | ScanLimit++; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 170 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
| 171 | IP->getOperand(1) == RHS) |
| 172 | return IP; |
Wojciech Matyjewicz | ae9753b2 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 173 | if (IP == BlockBegin) break; |
| 174 | } |
Chris Lattner | e71f144 | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 175 | } |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 176 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 177 | // Save the original insertion point so we can restore it when we're done. |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 178 | DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); |
| 179 | BuilderType::InsertPointGuard Guard(Builder); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 180 | |
| 181 | // Move the insertion point out of as many loops as we can. |
| 182 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 183 | if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; |
| 184 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 185 | if (!Preheader) break; |
| 186 | |
| 187 | // Ok, move up a level. |
| 188 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 189 | } |
| 190 | |
Wojciech Matyjewicz | ae9753b2 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 191 | // If we haven't found this binop, insert it. |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 192 | Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS)); |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 193 | BO->setDebugLoc(Loc); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 194 | rememberInstruction(BO); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 195 | |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 196 | return BO; |
Chris Lattner | e71f144 | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Dan Gohman | 1789362 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 199 | /// FactorOutConstant - Test if S is divisible by Factor, using signed |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 200 | /// division. If so, update S with Factor divided out and return true. |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 201 | /// S need not be evenly divisible if a reasonable remainder can be |
Dan Gohman | 1789362 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 202 | /// computed. |
Dan Gohman | 291c2e0 | 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 | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 206 | static bool FactorOutConstant(const SCEV *&S, |
| 207 | const SCEV *&Remainder, |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 208 | const SCEV *Factor, |
| 209 | ScalarEvolution &SE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 210 | const DataLayout *DL) { |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 211 | // Everything is divisible by one. |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 212 | if (Factor->isOne()) |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 213 | return true; |
| 214 | |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 215 | // x/x == 1. |
| 216 | if (S == Factor) { |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 217 | S = SE.getConstant(S->getType(), 1); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 221 | // For a Constant, check for a multiple of the given factor. |
Dan Gohman | 1789362 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 222 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 223 | // 0/x == 0. |
| 224 | if (C->isZero()) |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 225 | return true; |
Dan Gohman | bf2a9ae | 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 | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 244 | } |
Dan Gohman | 1789362 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 245 | } |
Dan Gohman | 291c2e0 | 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 | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 249 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 250 | if (DL) { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 251 | // With DataLayout, the size is known. Check if there is a constant |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 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 | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 257 | SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); |
Dan Gohman | bf2a9ae | 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 { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 265 | // Without DataLayout, check if Factor can be factored out of any of the |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 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 | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 269 | const SCEV *Remainder = SE.getConstant(SOp->getType(), 0); |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 270 | if (FactorOutConstant(SOp, Remainder, Factor, SE, DL) && |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 271 | Remainder->isZero()) { |
Dan Gohman | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 272 | SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 273 | NewMulOps[i] = SOp; |
| 274 | S = SE.getMulExpr(NewMulOps); |
| 275 | return true; |
| 276 | } |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 277 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Dan Gohman | 291c2e0 | 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 | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 283 | const SCEV *Step = A->getStepRecurrence(SE); |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 284 | const SCEV *StepRem = SE.getConstant(Step->getType(), 0); |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 285 | if (!FactorOutConstant(Step, StepRem, Factor, SE, DL)) |
Dan Gohman | 1789362 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 286 | return false; |
| 287 | if (!StepRem->isZero()) |
| 288 | return false; |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 289 | const SCEV *Start = A->getStart(); |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 290 | if (!FactorOutConstant(Start, Remainder, Factor, SE, DL)) |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 291 | return false; |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 292 | S = SE.getAddRecExpr(Start, Step, A->getLoop(), |
| 293 | A->getNoWrapFlags(SCEV::FlagNW)); |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
| 297 | return false; |
| 298 | } |
| 299 | |
Dan Gohman | bf2a9ae | 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 | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 305 | Type *Ty, |
Dan Gohman | bf2a9ae | 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 | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 315 | SE.getConstant(Ty, 0) : |
Dan Gohman | bf2a9ae | 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 | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 319 | Ops.clear(); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 320 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum)) |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 321 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 322 | else if (!Sum->isZero()) |
| 323 | Ops.push_back(Sum); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 324 | // Then append the addrecs. |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 325 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | bf2a9ae | 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 | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 334 | Type *Ty, |
Dan Gohman | bf2a9ae | 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 | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 342 | const SCEV *Zero = SE.getConstant(Ty, 0); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 343 | AddRecs.push_back(SE.getAddRecExpr(Zero, |
| 344 | A->getStepRecurrence(SE), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 345 | A->getLoop(), |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 346 | A->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 347 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) { |
| 348 | Ops[i] = Zero; |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 349 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 350 | e += Add->getNumOperands(); |
| 351 | } else { |
| 352 | Ops[i] = Start; |
| 353 | } |
| 354 | } |
| 355 | if (!AddRecs.empty()) { |
| 356 | // Add the addrecs onto the end of the list. |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 357 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 358 | // Resort the operand list, moving any constants to the front. |
| 359 | SimplifyAddOperands(Ops, Ty, SE); |
| 360 | } |
| 361 | } |
| 362 | |
Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 363 | /// expandAddToGEP - Expand an addition expression with a pointer type into |
| 364 | /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 365 | /// BasicAliasAnalysis and other passes analyze the result. See the rules |
| 366 | /// for getelementptr vs. inttoptr in |
| 367 | /// http://llvm.org/docs/LangRef.html#pointeraliasing |
| 368 | /// for details. |
Dan Gohman | 16e96c0 | 2009-07-20 17:44:17 +0000 | [diff] [blame] | 369 | /// |
Dan Gohman | 510bffc | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 370 | /// Design note: The correctness of using getelementptr here depends on |
Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 371 | /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as |
| 372 | /// they may introduce pointer arithmetic which may not be safely converted |
| 373 | /// into getelementptr. |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 374 | /// |
| 375 | /// Design note: It might seem desirable for this function to be more |
| 376 | /// loop-aware. If some of the indices are loop-invariant while others |
| 377 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 378 | /// loop-invariant portions of the overall computation outside the loop. |
| 379 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 380 | /// arithmetic is a low-level optimization that often isn't very |
| 381 | /// important until late in the optimization process. In fact, passes |
| 382 | /// like InstructionCombining will combine GEPs, even if it means |
| 383 | /// pushing loop-invariant computation down into loops, so even if the |
| 384 | /// GEPs were split here, the work would quickly be undone. The |
| 385 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 386 | /// after the last InstructionCombining pass), takes care of hoisting |
| 387 | /// loop-invariant portions of expressions, after considering what |
| 388 | /// can be folded using target addressing modes. |
| 389 | /// |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 390 | Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin, |
| 391 | const SCEV *const *op_end, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 392 | PointerType *PTy, |
| 393 | Type *Ty, |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 394 | Value *V) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 395 | Type *ElTy = PTy->getElementType(); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 396 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 397 | SmallVector<const SCEV *, 8> Ops(op_begin, op_end); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 398 | bool AnyNonZeroIndices = false; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 399 | |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 400 | // Split AddRecs up into parts as either of the parts may be usable |
| 401 | // without the other. |
| 402 | SplitAddRecs(Ops, Ty, SE); |
| 403 | |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 404 | Type *IntPtrTy = SE.DL |
| 405 | ? SE.DL->getIntPtrType(PTy) |
Matt Arsenault | a90a18e | 2013-09-10 19:55:24 +0000 | [diff] [blame] | 406 | : Type::getInt64Ty(PTy->getContext()); |
| 407 | |
Bob Wilson | 2107eb7 | 2009-12-04 01:33:04 +0000 | [diff] [blame] | 408 | // Descend down the pointer's type and attempt to convert the other |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 409 | // operands into GEP indices, at each level. The first index in a GEP |
| 410 | // indexes into the array implied by the pointer operand; the rest of |
| 411 | // the indices index into the element or field type selected by the |
| 412 | // preceding index. |
| 413 | for (;;) { |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 414 | // If the scale size is not 0, attempt to factor out a scale for |
| 415 | // array indexing. |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 416 | SmallVector<const SCEV *, 8> ScaledOps; |
Dan Gohman | 9f4ea22 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 417 | if (ElTy->isSized()) { |
Matt Arsenault | a90a18e | 2013-09-10 19:55:24 +0000 | [diff] [blame] | 418 | const SCEV *ElSize = SE.getSizeOfExpr(IntPtrTy, ElTy); |
Dan Gohman | 9f4ea22 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 419 | if (!ElSize->isZero()) { |
| 420 | SmallVector<const SCEV *, 8> NewOps; |
| 421 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 422 | const SCEV *Op = Ops[i]; |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 423 | const SCEV *Remainder = SE.getConstant(Ty, 0); |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 424 | if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.DL)) { |
Dan Gohman | 9f4ea22 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 425 | // Op now has ElSize factored out. |
| 426 | ScaledOps.push_back(Op); |
| 427 | if (!Remainder->isZero()) |
| 428 | NewOps.push_back(Remainder); |
| 429 | AnyNonZeroIndices = true; |
| 430 | } else { |
| 431 | // The operand was not divisible, so add it to the list of operands |
| 432 | // we'll scan next iteration. |
| 433 | NewOps.push_back(Ops[i]); |
| 434 | } |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 435 | } |
Dan Gohman | 9f4ea22 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 436 | // If we made any changes, update Ops. |
| 437 | if (!ScaledOps.empty()) { |
| 438 | Ops = NewOps; |
| 439 | SimplifyAddOperands(Ops, Ty, SE); |
| 440 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 441 | } |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 442 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 443 | |
| 444 | // Record the scaled array index for this level of the type. If |
| 445 | // we didn't find any operands that could be factored, tentatively |
| 446 | // assume that element zero was selected (since the zero offset |
| 447 | // would obviously be folded away). |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 448 | Value *Scaled = ScaledOps.empty() ? |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 449 | Constant::getNullValue(Ty) : |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 450 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 451 | GepIndices.push_back(Scaled); |
| 452 | |
| 453 | // Collect struct field index operands. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 454 | while (StructType *STy = dyn_cast<StructType>(ElTy)) { |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 455 | bool FoundFieldNo = false; |
| 456 | // An empty struct has no fields. |
| 457 | if (STy->getNumElements() == 0) break; |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 458 | if (SE.DL) { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 459 | // With DataLayout, field offsets are known. See if a constant offset |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 460 | // falls within any of the struct fields. |
| 461 | if (Ops.empty()) break; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 462 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 463 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 464 | const StructLayout &SL = *SE.DL->getStructLayout(STy); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 465 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 466 | if (FullOffset < SL.getSizeInBytes()) { |
| 467 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 468 | GepIndices.push_back( |
| 469 | ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx)); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 470 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 471 | Ops[0] = |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 472 | SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 473 | AnyNonZeroIndices = true; |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 474 | FoundFieldNo = true; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 477 | } else { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 478 | // Without DataLayout, just check for an offsetof expression of the |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 479 | // appropriate struct type. |
| 480 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 481 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 482 | Type *CTy; |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 483 | Constant *FieldNo; |
Dan Gohman | e5e1b7b | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 484 | if (U->isOffsetOf(CTy, FieldNo) && CTy == STy) { |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 485 | GepIndices.push_back(FieldNo); |
| 486 | ElTy = |
| 487 | STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue()); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 488 | Ops[i] = SE.getConstant(Ty, 0); |
| 489 | AnyNonZeroIndices = true; |
| 490 | FoundFieldNo = true; |
| 491 | break; |
| 492 | } |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 493 | } |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 494 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 495 | // If no struct field offsets were found, tentatively assume that |
| 496 | // field zero was selected (since the zero offset would obviously |
| 497 | // be folded away). |
| 498 | if (!FoundFieldNo) { |
| 499 | ElTy = STy->getTypeAtIndex(0u); |
| 500 | GepIndices.push_back( |
| 501 | Constant::getNullValue(Type::getInt32Ty(Ty->getContext()))); |
| 502 | } |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 503 | } |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 505 | if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 506 | ElTy = ATy->getElementType(); |
| 507 | else |
| 508 | break; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 511 | // If none of the operands were convertible to proper GEP indices, cast |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 512 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 513 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 514 | if (!AnyNonZeroIndices) { |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 515 | // Cast the base to i8*. |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 516 | V = InsertNoopCastOfTo(V, |
Duncan Sands | 9ed7b16 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 517 | Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 518 | |
Rafael Espindola | 729e3aa | 2012-02-21 03:51:14 +0000 | [diff] [blame] | 519 | assert(!isa<Instruction>(V) || |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 520 | SE.DT->dominates(cast<Instruction>(V), Builder.GetInsertPoint())); |
Rafael Espindola | 7d445e9 | 2012-02-21 01:19:51 +0000 | [diff] [blame] | 521 | |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 522 | // Expand the operands for a plain byte offset. |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 523 | Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 524 | |
| 525 | // Fold a GEP with constant operands. |
| 526 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 527 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 528 | return ConstantExpr::getGetElementPtr(CLHS, CRHS); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 529 | |
| 530 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 531 | unsigned ScanLimit = 6; |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 532 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 533 | // Scanning starts from the last instruction before the insertion point. |
| 534 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 535 | if (IP != BlockBegin) { |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 536 | --IP; |
| 537 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | f5cc1cd | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 538 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 539 | // generated code. |
| 540 | if (isa<DbgInfoIntrinsic>(IP)) |
| 541 | ScanLimit++; |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 542 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 543 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 544 | return IP; |
| 545 | if (IP == BlockBegin) break; |
| 546 | } |
| 547 | } |
| 548 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 549 | // Save the original insertion point so we can restore it when we're done. |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 550 | BuilderType::InsertPointGuard Guard(Builder); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 551 | |
| 552 | // Move the insertion point out of as many loops as we can. |
| 553 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 554 | if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break; |
| 555 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 556 | if (!Preheader) break; |
| 557 | |
| 558 | // Ok, move up a level. |
| 559 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 560 | } |
| 561 | |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 562 | // Emit a GEP. |
| 563 | Value *GEP = Builder.CreateGEP(V, Idx, "uglygep"); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 564 | rememberInstruction(GEP); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 565 | |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 566 | return GEP; |
| 567 | } |
| 568 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 569 | // Save the original insertion point so we can restore it when we're done. |
Benjamin Kramer | 58f1ced | 2013-10-01 12:17:11 +0000 | [diff] [blame] | 570 | BuilderType::InsertPoint SaveInsertPt = Builder.saveIP(); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 571 | |
| 572 | // Move the insertion point out of as many loops as we can. |
| 573 | while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { |
| 574 | if (!L->isLoopInvariant(V)) break; |
| 575 | |
| 576 | bool AnyIndexNotLoopInvariant = false; |
| 577 | for (SmallVectorImpl<Value *>::const_iterator I = GepIndices.begin(), |
| 578 | E = GepIndices.end(); I != E; ++I) |
| 579 | if (!L->isLoopInvariant(*I)) { |
| 580 | AnyIndexNotLoopInvariant = true; |
| 581 | break; |
| 582 | } |
| 583 | if (AnyIndexNotLoopInvariant) |
| 584 | break; |
| 585 | |
| 586 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 587 | if (!Preheader) break; |
| 588 | |
| 589 | // Ok, move up a level. |
| 590 | Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); |
| 591 | } |
| 592 | |
Dan Gohman | 31a9b98 | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 593 | // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, |
| 594 | // because ScalarEvolution may have changed the address arithmetic to |
| 595 | // compute a value which is beyond the end of the allocated object. |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 596 | Value *Casted = V; |
| 597 | if (V->getType() != PTy) |
| 598 | Casted = InsertNoopCastOfTo(Casted, PTy); |
| 599 | Value *GEP = Builder.CreateGEP(Casted, |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 600 | GepIndices, |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 601 | "scevgep"); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 602 | Ops.push_back(SE.getUnknown(GEP)); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 603 | rememberInstruction(GEP); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 604 | |
Benjamin Kramer | 58f1ced | 2013-10-01 12:17:11 +0000 | [diff] [blame] | 605 | // Restore the original insert point. |
| 606 | Builder.restoreIP(SaveInsertPt); |
| 607 | |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 608 | return expand(SE.getAddExpr(Ops)); |
| 609 | } |
| 610 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 611 | /// PickMostRelevantLoop - Given two loops pick the one that's most relevant for |
| 612 | /// SCEV expansion. If they are nested, this is the most nested. If they are |
| 613 | /// neighboring, pick the later. |
| 614 | static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B, |
| 615 | DominatorTree &DT) { |
| 616 | if (!A) return B; |
| 617 | if (!B) return A; |
| 618 | if (A->contains(B)) return B; |
| 619 | if (B->contains(A)) return A; |
| 620 | if (DT.dominates(A->getHeader(), B->getHeader())) return B; |
| 621 | if (DT.dominates(B->getHeader(), A->getHeader())) return A; |
| 622 | return A; // Arbitrarily break the tie. |
| 623 | } |
| 624 | |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 625 | /// getRelevantLoop - Get the most relevant loop associated with the given |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 626 | /// expression, according to PickMostRelevantLoop. |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 627 | const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) { |
| 628 | // Test whether we've already computed the most relevant loop for this SCEV. |
| 629 | std::pair<DenseMap<const SCEV *, const Loop *>::iterator, bool> Pair = |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 630 | RelevantLoops.insert(std::make_pair(S, nullptr)); |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 631 | if (!Pair.second) |
| 632 | return Pair.first->second; |
| 633 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 634 | if (isa<SCEVConstant>(S)) |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 635 | // A constant has no relevant loops. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 636 | return nullptr; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 637 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 638 | if (const Instruction *I = dyn_cast<Instruction>(U->getValue())) |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 639 | return Pair.first->second = SE.LI->getLoopFor(I->getParent()); |
| 640 | // A non-instruction has no relevant loops. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 641 | return nullptr; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 642 | } |
| 643 | if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 644 | const Loop *L = nullptr; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 645 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 646 | L = AR->getLoop(); |
| 647 | for (SCEVNAryExpr::op_iterator I = N->op_begin(), E = N->op_end(); |
| 648 | I != E; ++I) |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 649 | L = PickMostRelevantLoop(L, getRelevantLoop(*I), *SE.DT); |
| 650 | return RelevantLoops[N] = L; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 651 | } |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 652 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) { |
| 653 | const Loop *Result = getRelevantLoop(C->getOperand()); |
| 654 | return RelevantLoops[C] = Result; |
| 655 | } |
| 656 | if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 657 | const Loop *Result = |
| 658 | PickMostRelevantLoop(getRelevantLoop(D->getLHS()), |
| 659 | getRelevantLoop(D->getRHS()), |
| 660 | *SE.DT); |
| 661 | return RelevantLoops[D] = Result; |
| 662 | } |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 663 | llvm_unreachable("Unexpected SCEV type!"); |
| 664 | } |
| 665 | |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 666 | namespace { |
| 667 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 668 | /// LoopCompare - Compare loops by PickMostRelevantLoop. |
| 669 | class LoopCompare { |
| 670 | DominatorTree &DT; |
| 671 | public: |
| 672 | explicit LoopCompare(DominatorTree &dt) : DT(dt) {} |
| 673 | |
| 674 | bool operator()(std::pair<const Loop *, const SCEV *> LHS, |
| 675 | std::pair<const Loop *, const SCEV *> RHS) const { |
Dan Gohman | fbbdfca | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 676 | // Keep pointer operands sorted at the end. |
| 677 | if (LHS.second->getType()->isPointerTy() != |
| 678 | RHS.second->getType()->isPointerTy()) |
| 679 | return LHS.second->getType()->isPointerTy(); |
| 680 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 681 | // Compare loops with PickMostRelevantLoop. |
| 682 | if (LHS.first != RHS.first) |
| 683 | return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first; |
| 684 | |
| 685 | // If one operand is a non-constant negative and the other is not, |
| 686 | // put the non-constant negative on the right so that a sub can |
| 687 | // be used instead of a negate and add. |
Andrew Trick | 881a776 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 688 | if (LHS.second->isNonConstantNegative()) { |
| 689 | if (!RHS.second->isNonConstantNegative()) |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 690 | return false; |
Andrew Trick | 881a776 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 691 | } else if (RHS.second->isNonConstantNegative()) |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 692 | return true; |
| 693 | |
| 694 | // Otherwise they are equivalent according to this comparison. |
| 695 | return false; |
| 696 | } |
| 697 | }; |
| 698 | |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 701 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 702 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 5bafe38 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 703 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 704 | // Collect all the add operands in a loop, along with their associated loops. |
| 705 | // Iterate in reverse so that constants are emitted last, all else equal, and |
| 706 | // so that pointer operands are inserted first, which the code below relies on |
| 707 | // to form more involved GEPs. |
| 708 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 709 | for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()), |
| 710 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 711 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Dan Gohman | 5bafe38 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 712 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 713 | // Sort by loop. Use a stable sort so that constants follow non-constants and |
| 714 | // pointer operands precede non-pointer operands. |
| 715 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 716 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 717 | // Emit instructions to add all the operands. Hoist as much as possible |
| 718 | // out of loops, and form meaningful getelementptrs where possible. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 719 | Value *Sum = nullptr; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 720 | for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator |
| 721 | I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { |
| 722 | const Loop *CurLoop = I->first; |
| 723 | const SCEV *Op = I->second; |
| 724 | if (!Sum) { |
| 725 | // This is the first operand. Just expand it. |
| 726 | Sum = expand(Op); |
| 727 | ++I; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 728 | } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) { |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 729 | // The running sum expression is a pointer. Try to form a getelementptr |
| 730 | // at this level with that as the base. |
| 731 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | fbbdfca | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 732 | for (; I != E && I->first == CurLoop; ++I) { |
| 733 | // If the operand is SCEVUnknown and not instructions, peek through |
| 734 | // it, to enable more of it to be folded into the GEP. |
| 735 | const SCEV *X = I->second; |
| 736 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X)) |
| 737 | if (!isa<Instruction>(U->getValue())) |
| 738 | X = SE.getSCEV(U->getValue()); |
| 739 | NewOps.push_back(X); |
| 740 | } |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 741 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 742 | } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) { |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 743 | // The running sum is an integer, and there's a pointer at this level. |
Dan Gohman | 3295a6e | 2010-04-09 19:14:31 +0000 | [diff] [blame] | 744 | // Try to form a getelementptr. If the running sum is instructions, |
| 745 | // use a SCEVUnknown to avoid re-analyzing them. |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 746 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | 3295a6e | 2010-04-09 19:14:31 +0000 | [diff] [blame] | 747 | NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) : |
| 748 | SE.getSCEV(Sum)); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 749 | for (++I; I != E && I->first == CurLoop; ++I) |
| 750 | NewOps.push_back(I->second); |
| 751 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op)); |
Andrew Trick | 881a776 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 752 | } else if (Op->isNonConstantNegative()) { |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 753 | // Instead of doing a negate and add, just do a subtract. |
Dan Gohman | 2850b41 | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 754 | Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 755 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 756 | Sum = InsertBinop(Instruction::Sub, Sum, W); |
| 757 | ++I; |
Dan Gohman | 2850b41 | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 758 | } else { |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 759 | // A simple add. |
Dan Gohman | 2850b41 | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 760 | Value *W = expandCodeFor(Op, Ty); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 761 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 762 | // Canonicalize a constant to the RHS. |
| 763 | if (isa<Constant>(Sum)) std::swap(Sum, W); |
| 764 | Sum = InsertBinop(Instruction::Add, Sum, W); |
| 765 | ++I; |
Dan Gohman | 2850b41 | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 768 | |
| 769 | return Sum; |
Dan Gohman | 095ca74 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 770 | } |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 771 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 772 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 773 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 774 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 775 | // Collect all the mul operands in a loop, along with their associated loops. |
| 776 | // Iterate in reverse so that constants are emitted last, all else equal. |
| 777 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 778 | for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()), |
| 779 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 8ea83d8 | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 780 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 781 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 782 | // Sort by loop. Use a stable sort so that constants follow non-constants. |
| 783 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); |
| 784 | |
| 785 | // Emit instructions to mul all the operands. Hoist as much as possible |
| 786 | // out of loops. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 787 | Value *Prod = nullptr; |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 788 | for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator |
| 789 | I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { |
| 790 | const SCEV *Op = I->second; |
| 791 | if (!Prod) { |
| 792 | // This is the first operand. Just expand it. |
| 793 | Prod = expand(Op); |
| 794 | ++I; |
| 795 | } else if (Op->isAllOnesValue()) { |
| 796 | // Instead of doing a multiply by negative one, just do a negate. |
| 797 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 798 | Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod); |
| 799 | ++I; |
| 800 | } else { |
| 801 | // A simple mul. |
| 802 | Value *W = expandCodeFor(Op, Ty); |
| 803 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 804 | // Canonicalize a constant to the RHS. |
| 805 | if (isa<Constant>(Prod)) std::swap(Prod, W); |
| 806 | Prod = InsertBinop(Instruction::Mul, Prod, W); |
| 807 | ++I; |
| 808 | } |
Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 811 | return Prod; |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 814 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 815 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 816 | |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 817 | Value *LHS = expandCodeFor(S->getLHS(), Ty); |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 818 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 3c94704 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 819 | const APInt &RHS = SC->getValue()->getValue(); |
| 820 | if (RHS.isPowerOf2()) |
| 821 | return InsertBinop(Instruction::LShr, LHS, |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 822 | ConstantInt::get(Ty, RHS.logBase2())); |
Nick Lewycky | 3c94704 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 825 | Value *RHS = expandCodeFor(S->getRHS(), Ty); |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 826 | return InsertBinop(Instruction::UDiv, LHS, RHS); |
Nick Lewycky | 3c94704 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 829 | /// Move parts of Base into Rest to leave Base with the minimal |
| 830 | /// expression that provides a pointer operand suitable for a |
| 831 | /// GEP expansion. |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 832 | static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 833 | ScalarEvolution &SE) { |
| 834 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 835 | Base = A->getStart(); |
| 836 | Rest = SE.getAddExpr(Rest, |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 837 | SE.getAddRecExpr(SE.getConstant(A->getType(), 0), |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 838 | A->getStepRecurrence(SE), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 839 | A->getLoop(), |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 840 | A->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 841 | } |
| 842 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 843 | Base = A->getOperand(A->getNumOperands()-1); |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 844 | SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end()); |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 845 | NewAddOps.back() = Rest; |
| 846 | Rest = SE.getAddExpr(NewAddOps); |
| 847 | ExposePointerBase(Base, Rest, SE); |
| 848 | } |
| 849 | } |
| 850 | |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 851 | /// Determine if this is a well-behaved chain of instructions leading back to |
| 852 | /// the PHI. If so, it may be reused by expanded expressions. |
| 853 | bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, |
| 854 | const Loop *L) { |
| 855 | if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) || |
| 856 | (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV))) |
| 857 | return false; |
| 858 | // If any of the operands don't dominate the insert position, bail. |
| 859 | // Addrec operands are always loop-invariant, so this can only happen |
| 860 | // if there are instructions which haven't been hoisted. |
| 861 | if (L == IVIncInsertLoop) { |
| 862 | for (User::op_iterator OI = IncV->op_begin()+1, |
| 863 | OE = IncV->op_end(); OI != OE; ++OI) |
| 864 | if (Instruction *OInst = dyn_cast<Instruction>(OI)) |
| 865 | if (!SE.DT->dominates(OInst, IVIncInsertPos)) |
| 866 | return false; |
| 867 | } |
| 868 | // Advance to the next instruction. |
| 869 | IncV = dyn_cast<Instruction>(IncV->getOperand(0)); |
| 870 | if (!IncV) |
| 871 | return false; |
| 872 | |
| 873 | if (IncV->mayHaveSideEffects()) |
| 874 | return false; |
| 875 | |
| 876 | if (IncV != PN) |
| 877 | return true; |
| 878 | |
| 879 | return isNormalAddRecExprPHI(PN, IncV, L); |
| 880 | } |
| 881 | |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 882 | /// getIVIncOperand returns an induction variable increment's induction |
| 883 | /// variable operand. |
| 884 | /// |
| 885 | /// If allowScale is set, any type of GEP is allowed as long as the nonIV |
| 886 | /// operands dominate InsertPos. |
| 887 | /// |
| 888 | /// If allowScale is not set, ensure that a GEP increment conforms to one of the |
| 889 | /// simple patterns generated by getAddRecExprPHILiterally and |
| 890 | /// expandAddtoGEP. If the pattern isn't recognized, return NULL. |
| 891 | Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV, |
| 892 | Instruction *InsertPos, |
| 893 | bool allowScale) { |
| 894 | if (IncV == InsertPos) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 895 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 896 | |
| 897 | switch (IncV->getOpcode()) { |
| 898 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 899 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 900 | // Check for a simple Add/Sub or GEP of a loop invariant step. |
| 901 | case Instruction::Add: |
| 902 | case Instruction::Sub: { |
| 903 | Instruction *OInst = dyn_cast<Instruction>(IncV->getOperand(1)); |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 904 | if (!OInst || SE.DT->dominates(OInst, InsertPos)) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 905 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 906 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 907 | } |
| 908 | case Instruction::BitCast: |
| 909 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
| 910 | case Instruction::GetElementPtr: |
| 911 | for (Instruction::op_iterator I = IncV->op_begin()+1, E = IncV->op_end(); |
| 912 | I != E; ++I) { |
| 913 | if (isa<Constant>(*I)) |
| 914 | continue; |
| 915 | if (Instruction *OInst = dyn_cast<Instruction>(*I)) { |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 916 | if (!SE.DT->dominates(OInst, InsertPos)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 917 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 918 | } |
| 919 | if (allowScale) { |
| 920 | // allow any kind of GEP as long as it can be hoisted. |
| 921 | continue; |
| 922 | } |
| 923 | // This must be a pointer addition of constants (pretty), which is already |
| 924 | // handled, or some number of address-size elements (ugly). Ugly geps |
| 925 | // have 2 operands. i1* is used by the expander to represent an |
| 926 | // address-size element. |
| 927 | if (IncV->getNumOperands() != 2) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 928 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 929 | unsigned AS = cast<PointerType>(IncV->getType())->getAddressSpace(); |
| 930 | if (IncV->getType() != Type::getInt1PtrTy(SE.getContext(), AS) |
| 931 | && IncV->getType() != Type::getInt8PtrTy(SE.getContext(), AS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 932 | return nullptr; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make |
| 940 | /// it available to other uses in this loop. Recursively hoist any operands, |
| 941 | /// until we reach a value that dominates InsertPos. |
| 942 | bool SCEVExpander::hoistIVInc(Instruction *IncV, Instruction *InsertPos) { |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 943 | if (SE.DT->dominates(IncV, InsertPos)) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 944 | return true; |
| 945 | |
| 946 | // InsertPos must itself dominate IncV so that IncV's new position satisfies |
| 947 | // its existing users. |
Andrew Trick | a7a3de1 | 2012-05-22 17:39:59 +0000 | [diff] [blame] | 948 | if (isa<PHINode>(InsertPos) |
| 949 | || !SE.DT->dominates(InsertPos->getParent(), IncV->getParent())) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 950 | return false; |
| 951 | |
| 952 | // Check that the chain of IV operands leading back to Phi can be hoisted. |
| 953 | SmallVector<Instruction*, 4> IVIncs; |
| 954 | for(;;) { |
| 955 | Instruction *Oper = getIVIncOperand(IncV, InsertPos, /*allowScale*/true); |
| 956 | if (!Oper) |
| 957 | return false; |
| 958 | // IncV is safe to hoist. |
| 959 | IVIncs.push_back(IncV); |
| 960 | IncV = Oper; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 961 | if (SE.DT->dominates(IncV, InsertPos)) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 962 | break; |
| 963 | } |
| 964 | for (SmallVectorImpl<Instruction*>::reverse_iterator I = IVIncs.rbegin(), |
| 965 | E = IVIncs.rend(); I != E; ++I) { |
| 966 | (*I)->moveBefore(InsertPos); |
| 967 | } |
| 968 | return true; |
| 969 | } |
| 970 | |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 971 | /// Determine if this cyclic phi is in a form that would have been generated by |
| 972 | /// LSR. We don't care if the phi was actually expanded in this pass, as long |
| 973 | /// as it is in a low-cost form, for example, no implied multiplication. This |
| 974 | /// should match any patterns generated by getAddRecExprPHILiterally and |
| 975 | /// expandAddtoGEP. |
| 976 | bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, |
Andrew Trick | fd4ca0f | 2011-10-15 06:19:55 +0000 | [diff] [blame] | 977 | const Loop *L) { |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 978 | for(Instruction *IVOper = IncV; |
| 979 | (IVOper = getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(), |
| 980 | /*allowScale=*/false));) { |
| 981 | if (IVOper == PN) |
| 982 | return true; |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 983 | } |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 984 | return false; |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 987 | /// expandIVInc - Expand an IV increment at Builder's current InsertPos. |
| 988 | /// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may |
| 989 | /// need to materialize IV increments elsewhere to handle difficult situations. |
| 990 | Value *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L, |
| 991 | Type *ExpandTy, Type *IntTy, |
| 992 | bool useSubtract) { |
| 993 | Value *IncV; |
| 994 | // If the PHI is a pointer, use a GEP, otherwise use an add or sub. |
| 995 | if (ExpandTy->isPointerTy()) { |
| 996 | PointerType *GEPPtrTy = cast<PointerType>(ExpandTy); |
| 997 | // If the step isn't constant, don't use an implicitly scaled GEP, because |
| 998 | // that would require a multiply inside the loop. |
| 999 | if (!isa<ConstantInt>(StepV)) |
| 1000 | GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()), |
| 1001 | GEPPtrTy->getAddressSpace()); |
| 1002 | const SCEV *const StepArray[1] = { SE.getSCEV(StepV) }; |
| 1003 | IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN); |
| 1004 | if (IncV->getType() != PN->getType()) { |
| 1005 | IncV = Builder.CreateBitCast(IncV, PN->getType()); |
| 1006 | rememberInstruction(IncV); |
| 1007 | } |
| 1008 | } else { |
| 1009 | IncV = useSubtract ? |
| 1010 | Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") : |
| 1011 | Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next"); |
| 1012 | rememberInstruction(IncV); |
| 1013 | } |
| 1014 | return IncV; |
| 1015 | } |
| 1016 | |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1017 | /// \brief Hoist the addrec instruction chain rooted in the loop phi above the |
| 1018 | /// position. This routine assumes that this is possible (has been checked). |
| 1019 | static void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist, |
| 1020 | Instruction *Pos, PHINode *LoopPhi) { |
| 1021 | do { |
| 1022 | if (DT->dominates(InstToHoist, Pos)) |
| 1023 | break; |
| 1024 | // Make sure the increment is where we want it. But don't move it |
| 1025 | // down past a potential existing post-inc user. |
| 1026 | InstToHoist->moveBefore(Pos); |
| 1027 | Pos = InstToHoist; |
| 1028 | InstToHoist = cast<Instruction>(InstToHoist->getOperand(0)); |
| 1029 | } while (InstToHoist != LoopPhi); |
| 1030 | } |
| 1031 | |
| 1032 | /// \brief Check whether we can cheaply express the requested SCEV in terms of |
| 1033 | /// the available PHI SCEV by truncation and/or invertion of the step. |
| 1034 | static bool canBeCheaplyTransformed(ScalarEvolution &SE, |
| 1035 | const SCEVAddRecExpr *Phi, |
| 1036 | const SCEVAddRecExpr *Requested, |
| 1037 | bool &InvertStep) { |
| 1038 | Type *PhiTy = SE.getEffectiveSCEVType(Phi->getType()); |
| 1039 | Type *RequestedTy = SE.getEffectiveSCEVType(Requested->getType()); |
| 1040 | |
| 1041 | if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth()) |
| 1042 | return false; |
| 1043 | |
| 1044 | // Try truncate it if necessary. |
| 1045 | Phi = dyn_cast<SCEVAddRecExpr>(SE.getTruncateOrNoop(Phi, RequestedTy)); |
| 1046 | if (!Phi) |
| 1047 | return false; |
| 1048 | |
| 1049 | // Check whether truncation will help. |
| 1050 | if (Phi == Requested) { |
| 1051 | InvertStep = false; |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
| 1055 | // Check whether inverting will help: {R,+,-1} == R - {0,+,1}. |
| 1056 | if (SE.getAddExpr(Requested->getStart(), |
| 1057 | SE.getNegativeSCEV(Requested)) == Phi) { |
| 1058 | InvertStep = true; |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | return false; |
| 1063 | } |
| 1064 | |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1065 | /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand |
| 1066 | /// the base addrec, which is the addrec without any non-loop-dominating |
| 1067 | /// values, and return the PHI. |
| 1068 | PHINode * |
| 1069 | SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, |
| 1070 | const Loop *L, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1071 | Type *ExpandTy, |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1072 | Type *IntTy, |
| 1073 | Type *&TruncTy, |
| 1074 | bool &InvertStep) { |
Benjamin Kramer | a7606b99 | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 1075 | assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position"); |
Andrew Trick | 244e2c3 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 1076 | |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1077 | // Reuse a previously-inserted PHI, if present. |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1078 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 1079 | if (LatchBlock) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1080 | PHINode *AddRecPhiMatch = nullptr; |
| 1081 | Instruction *IncV = nullptr; |
| 1082 | TruncTy = nullptr; |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1083 | InvertStep = false; |
| 1084 | |
| 1085 | // Only try partially matching scevs that need truncation and/or |
| 1086 | // step-inversion if we know this loop is outside the current loop. |
| 1087 | bool TryNonMatchingSCEV = IVIncInsertLoop && |
| 1088 | SE.DT->properlyDominates(LatchBlock, IVIncInsertLoop->getHeader()); |
| 1089 | |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1090 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 1091 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1092 | if (!SE.isSCEVable(PN->getType())) |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1093 | continue; |
Dan Gohman | 148a972 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 1094 | |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1095 | const SCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(PN)); |
| 1096 | if (!PhiSCEV) |
| 1097 | continue; |
Dan Gohman | 148a972 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 1098 | |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1099 | bool IsMatchingSCEV = PhiSCEV == Normalized; |
| 1100 | // We only handle truncation and inversion of phi recurrences for the |
| 1101 | // expanded expression if the expanded expression's loop dominates the |
| 1102 | // loop we insert to. Check now, so we can bail out early. |
| 1103 | if (!IsMatchingSCEV && !TryNonMatchingSCEV) |
| 1104 | continue; |
| 1105 | |
| 1106 | Instruction *TempIncV = |
| 1107 | cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)); |
| 1108 | |
| 1109 | // Check whether we can reuse this PHI node. |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1110 | if (LSRMode) { |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1111 | if (!isExpandedAddRecExprPHI(PN, TempIncV, L)) |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1112 | continue; |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1113 | if (L == IVIncInsertLoop && !hoistIVInc(TempIncV, IVIncInsertPos)) |
| 1114 | continue; |
| 1115 | } else { |
| 1116 | if (!isNormalAddRecExprPHI(PN, TempIncV, L)) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1117 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1118 | } |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Stop if we have found an exact match SCEV. |
| 1121 | if (IsMatchingSCEV) { |
| 1122 | IncV = TempIncV; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1123 | TruncTy = nullptr; |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1124 | InvertStep = false; |
| 1125 | AddRecPhiMatch = PN; |
| 1126 | break; |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1127 | } |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Try whether the phi can be translated into the requested form |
| 1130 | // (truncated and/or offset by a constant). |
| 1131 | if ((!TruncTy || InvertStep) && |
| 1132 | canBeCheaplyTransformed(SE, PhiSCEV, Normalized, InvertStep)) { |
| 1133 | // Record the phi node. But don't stop we might find an exact match |
| 1134 | // later. |
| 1135 | AddRecPhiMatch = PN; |
| 1136 | IncV = TempIncV; |
| 1137 | TruncTy = SE.getEffectiveSCEVType(Normalized->getType()); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | if (AddRecPhiMatch) { |
| 1142 | // Potentially, move the increment. We have made sure in |
| 1143 | // isExpandedAddRecExprPHI or hoistIVInc that this is possible. |
| 1144 | if (L == IVIncInsertLoop) |
| 1145 | hoistBeforePos(SE.DT, IncV, IVIncInsertPos, AddRecPhiMatch); |
| 1146 | |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1147 | // Ok, the add recurrence looks usable. |
| 1148 | // Remember this PHI, even in post-inc mode. |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1149 | InsertedValues.insert(AddRecPhiMatch); |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1150 | // Remember the increment. |
| 1151 | rememberInstruction(IncV); |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1152 | return AddRecPhiMatch; |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Save the original insertion point so we can restore it when we're done. |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1157 | BuilderType::InsertPointGuard Guard(Builder); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1158 | |
Andrew Trick | b9aa26f | 2011-12-20 01:42:24 +0000 | [diff] [blame] | 1159 | // Another AddRec may need to be recursively expanded below. For example, if |
| 1160 | // this AddRec is quadratic, the StepV may itself be an AddRec in this |
| 1161 | // loop. Remove this loop from the PostIncLoops set before expanding such |
| 1162 | // AddRecs. Otherwise, we cannot find a valid position for the step |
| 1163 | // (i.e. StepV can never dominate its loop header). Ideally, we could do |
| 1164 | // SavedIncLoops.swap(PostIncLoops), but we generally have a single element, |
| 1165 | // so it's not worth implementing SmallPtrSet::swap. |
| 1166 | PostIncLoopSet SavedPostIncLoops = PostIncLoops; |
| 1167 | PostIncLoops.clear(); |
| 1168 | |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1169 | // Expand code for the start value. |
| 1170 | Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, |
| 1171 | L->getHeader()->begin()); |
| 1172 | |
Andrew Trick | 244e2c3 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 1173 | // StartV must be hoisted into L's preheader to dominate the new phi. |
Benjamin Kramer | a7606b99 | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 1174 | assert(!isa<Instruction>(StartV) || |
| 1175 | SE.DT->properlyDominates(cast<Instruction>(StartV)->getParent(), |
| 1176 | L->getHeader())); |
Andrew Trick | 244e2c3 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 1177 | |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1178 | // Expand code for the step value. Do this before creating the PHI so that PHI |
| 1179 | // reuse code doesn't see an incomplete PHI. |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1180 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1181 | // If the stride is negative, insert a sub instead of an add for the increment |
| 1182 | // (unless it's a constant, because subtracts of constants are canonicalized |
| 1183 | // to adds). |
Andrew Trick | 881a776 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 1184 | bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1185 | if (useSubtract) |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1186 | Step = SE.getNegativeSCEV(Step); |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1187 | // Expand the step somewhere that dominates the loop header. |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1188 | Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); |
| 1189 | |
| 1190 | // Create the PHI. |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1191 | BasicBlock *Header = L->getHeader(); |
| 1192 | Builder.SetInsertPoint(Header, Header->begin()); |
| 1193 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Andrew Trick | 411daa5 | 2011-06-28 05:07:32 +0000 | [diff] [blame] | 1194 | PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE), |
Andrew Trick | 154d78a | 2011-06-28 05:41:52 +0000 | [diff] [blame] | 1195 | Twine(IVName) + ".iv"); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1196 | rememberInstruction(PN); |
| 1197 | |
| 1198 | // Create the step instructions and populate the PHI. |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1199 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1200 | BasicBlock *Pred = *HPI; |
| 1201 | |
| 1202 | // Add a start value. |
| 1203 | if (!L->contains(Pred)) { |
| 1204 | PN->addIncoming(StartV, Pred); |
| 1205 | continue; |
| 1206 | } |
| 1207 | |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1208 | // Create a step value and add it to the PHI. |
| 1209 | // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the |
| 1210 | // instructions at IVIncInsertPos. |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1211 | Instruction *InsertPos = L == IVIncInsertLoop ? |
| 1212 | IVIncInsertPos : Pred->getTerminator(); |
Devang Patel | c3239d3 | 2011-07-05 21:48:22 +0000 | [diff] [blame] | 1213 | Builder.SetInsertPoint(InsertPos); |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1214 | Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); |
Andrew Trick | 8eaae28 | 2013-07-14 02:50:07 +0000 | [diff] [blame] | 1215 | if (isa<OverflowingBinaryOperator>(IncV)) { |
| 1216 | if (Normalized->getNoWrapFlags(SCEV::FlagNUW)) |
| 1217 | cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap(); |
| 1218 | if (Normalized->getNoWrapFlags(SCEV::FlagNSW)) |
| 1219 | cast<BinaryOperator>(IncV)->setHasNoSignedWrap(); |
| 1220 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1221 | PN->addIncoming(IncV, Pred); |
| 1222 | } |
| 1223 | |
Andrew Trick | b9aa26f | 2011-12-20 01:42:24 +0000 | [diff] [blame] | 1224 | // After expanding subexpressions, restore the PostIncLoops set so the caller |
| 1225 | // can ensure that IVIncrement dominates the current uses. |
| 1226 | PostIncLoops = SavedPostIncLoops; |
| 1227 | |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1228 | // Remember this PHI, even in post-inc mode. |
| 1229 | InsertedValues.insert(PN); |
| 1230 | |
| 1231 | return PN; |
| 1232 | } |
| 1233 | |
| 1234 | Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1235 | Type *STy = S->getType(); |
| 1236 | Type *IntTy = SE.getEffectiveSCEVType(STy); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1237 | const Loop *L = S->getLoop(); |
| 1238 | |
| 1239 | // Determine a normalized form of this expression, which is the expression |
| 1240 | // before any post-inc adjustment is made. |
| 1241 | const SCEVAddRecExpr *Normalized = S; |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1242 | if (PostIncLoops.count(L)) { |
| 1243 | PostIncLoopSet Loops; |
| 1244 | Loops.insert(L); |
| 1245 | Normalized = |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1246 | cast<SCEVAddRecExpr>(TransformForPostIncUse(Normalize, S, nullptr, |
| 1247 | nullptr, Loops, SE, *SE.DT)); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | // Strip off any non-loop-dominating component from the addrec start. |
| 1251 | const SCEV *Start = Normalized->getStart(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1252 | const SCEV *PostLoopOffset = nullptr; |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1253 | if (!SE.properlyDominates(Start, L->getHeader())) { |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1254 | PostLoopOffset = Start; |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1255 | Start = SE.getConstant(Normalized->getType(), 0); |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1256 | Normalized = cast<SCEVAddRecExpr>( |
| 1257 | SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE), |
| 1258 | Normalized->getLoop(), |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1259 | Normalized->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | // Strip off any non-loop-dominating component from the addrec step. |
| 1263 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1264 | const SCEV *PostLoopScale = nullptr; |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1265 | if (!SE.dominates(Step, L->getHeader())) { |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1266 | PostLoopScale = Step; |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1267 | Step = SE.getConstant(Normalized->getType(), 1); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1268 | Normalized = |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1269 | cast<SCEVAddRecExpr>(SE.getAddRecExpr( |
| 1270 | Start, Step, Normalized->getLoop(), |
| 1271 | Normalized->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | // Expand the core addrec. If we need post-loop scaling, force it to |
| 1275 | // expand to an integer type to avoid the need for additional casting. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1276 | Type *ExpandTy = PostLoopScale ? IntTy : STy; |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1277 | // In some cases, we decide to reuse an existing phi node but need to truncate |
| 1278 | // it and/or invert the step. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1279 | Type *TruncTy = nullptr; |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1280 | bool InvertStep = false; |
| 1281 | PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy, |
| 1282 | TruncTy, InvertStep); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1283 | |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1284 | // Accommodate post-inc mode, if necessary. |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1285 | Value *Result; |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1286 | if (!PostIncLoops.count(L)) |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1287 | Result = PN; |
| 1288 | else { |
| 1289 | // In PostInc mode, use the post-incremented value. |
| 1290 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 1291 | assert(LatchBlock && "PostInc mode requires a unique loop latch!"); |
| 1292 | Result = PN->getIncomingValueForBlock(LatchBlock); |
Andrew Trick | 870c1a3 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1293 | |
| 1294 | // For an expansion to use the postinc form, the client must call |
| 1295 | // expandCodeFor with an InsertPoint that is either outside the PostIncLoop |
| 1296 | // or dominated by IVIncInsertPos. |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1297 | if (isa<Instruction>(Result) |
| 1298 | && !SE.DT->dominates(cast<Instruction>(Result), |
| 1299 | Builder.GetInsertPoint())) { |
| 1300 | // The induction variable's postinc expansion does not dominate this use. |
| 1301 | // IVUsers tries to prevent this case, so it is rare. However, it can |
| 1302 | // happen when an IVUser outside the loop is not dominated by the latch |
| 1303 | // block. Adjusting IVIncInsertPos before expansion begins cannot handle |
| 1304 | // all cases. Consider a phi outide whose operand is replaced during |
| 1305 | // expansion with the value of the postinc user. Without fundamentally |
| 1306 | // changing the way postinc users are tracked, the only remedy is |
| 1307 | // inserting an extra IV increment. StepV might fold into PostLoopOffset, |
| 1308 | // but hopefully expandCodeFor handles that. |
| 1309 | bool useSubtract = |
Andrew Trick | 881a776 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 1310 | !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1311 | if (useSubtract) |
| 1312 | Step = SE.getNegativeSCEV(Step); |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1313 | Value *StepV; |
| 1314 | { |
| 1315 | // Expand the step somewhere that dominates the loop header. |
| 1316 | BuilderType::InsertPointGuard Guard(Builder); |
| 1317 | StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); |
| 1318 | } |
Andrew Trick | ceafa2c | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1319 | Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); |
| 1320 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Arnold Schwaighofer | 26f567d | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1323 | // We have decided to reuse an induction variable of a dominating loop. Apply |
| 1324 | // truncation and/or invertion of the step. |
| 1325 | if (TruncTy) { |
| 1326 | Type *ResTy = Result->getType(); |
| 1327 | // Normalize the result type. |
| 1328 | if (ResTy != SE.getEffectiveSCEVType(ResTy)) |
| 1329 | Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy)); |
| 1330 | // Truncate the result. |
| 1331 | if (TruncTy != Result->getType()) { |
| 1332 | Result = Builder.CreateTrunc(Result, TruncTy); |
| 1333 | rememberInstruction(Result); |
| 1334 | } |
| 1335 | // Invert the result. |
| 1336 | if (InvertStep) { |
| 1337 | Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy), |
| 1338 | Result); |
| 1339 | rememberInstruction(Result); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1343 | // Re-apply any non-loop-dominating scale. |
| 1344 | if (PostLoopScale) { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1345 | assert(S->isAffine() && "Can't linearly scale non-affine recurrences."); |
Dan Gohman | 1a8674e | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1346 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1347 | Result = Builder.CreateMul(Result, |
| 1348 | expandCodeFor(PostLoopScale, IntTy)); |
| 1349 | rememberInstruction(Result); |
| 1350 | } |
| 1351 | |
| 1352 | // Re-apply any non-loop-dominating offset. |
| 1353 | if (PostLoopOffset) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1354 | if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) { |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1355 | const SCEV *const OffsetArray[1] = { PostLoopOffset }; |
| 1356 | Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result); |
| 1357 | } else { |
Dan Gohman | 1a8674e | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1358 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1359 | Result = Builder.CreateAdd(Result, |
| 1360 | expandCodeFor(PostLoopOffset, IntTy)); |
| 1361 | rememberInstruction(Result); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | return Result; |
| 1366 | } |
| 1367 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1368 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1369 | if (!CanonicalMode) return expandAddRecExprLiterally(S); |
| 1370 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1371 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1372 | const Loop *L = S->getLoop(); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1373 | |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1374 | // First check for an existing canonical IV in a suitable type. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1375 | PHINode *CanonicalIV = nullptr; |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1376 | if (PHINode *PN = L->getCanonicalInductionVariable()) |
Dan Gohman | 3115875 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1377 | if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1378 | CanonicalIV = PN; |
| 1379 | |
| 1380 | // Rewrite an AddRec in terms of the canonical induction variable, if |
| 1381 | // its type is more narrow. |
| 1382 | if (CanonicalIV && |
| 1383 | SE.getTypeSizeInBits(CanonicalIV->getType()) > |
| 1384 | SE.getTypeSizeInBits(Ty)) { |
Dan Gohman | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1385 | SmallVector<const SCEV *, 4> NewOps(S->getNumOperands()); |
| 1386 | for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i) |
| 1387 | NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType()); |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1388 | Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(), |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1389 | S->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1390 | BasicBlock::iterator NewInsertPt = |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1391 | std::next(BasicBlock::iterator(cast<Instruction>(V))); |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1392 | BuilderType::InsertPointGuard Guard(Builder); |
Bill Wendling | 86c5cbe | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 1393 | while (isa<PHINode>(NewInsertPt) || isa<DbgInfoIntrinsic>(NewInsertPt) || |
| 1394 | isa<LandingPadInst>(NewInsertPt)) |
Jim Grosbach | fd3b4e7 | 2010-06-16 21:13:38 +0000 | [diff] [blame] | 1395 | ++NewInsertPt; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1396 | V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr, |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1397 | NewInsertPt); |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1398 | return V; |
| 1399 | } |
| 1400 | |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1401 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | be928e3 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1402 | if (!S->getStart()->isZero()) { |
Dan Gohman | 0052449 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1403 | SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end()); |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1404 | NewOps[0] = SE.getConstant(Ty, 0); |
Andrew Trick | aa8ceba | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1405 | const SCEV *Rest = SE.getAddRecExpr(NewOps, L, |
| 1406 | S->getNoWrapFlags(SCEV::FlagNW)); |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 1409 | // comments on expandAddToGEP for details. |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1410 | const SCEV *Base = S->getStart(); |
| 1411 | const SCEV *RestArray[1] = { Rest }; |
| 1412 | // Dig into the expression to find the pointer base for a GEP. |
| 1413 | ExposePointerBase(Base, RestArray[0], SE); |
| 1414 | // If we found a pointer, expand the AddRec with a GEP. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1415 | if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1416 | // Make sure the Base isn't something exotic, such as a multiplied |
| 1417 | // or divided pointer value. In those cases, the result type isn't |
| 1418 | // actually a pointer type. |
| 1419 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 1420 | Value *StartV = expand(Base); |
| 1421 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
| 1422 | return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); |
Dan Gohman | 291c2e0 | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1426 | // Just do a normal add. Pre-expand the operands to suppress folding. |
| 1427 | return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())), |
| 1428 | SE.getUnknown(expand(Rest)))); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1431 | // If we don't yet have a canonical IV, create one. |
| 1432 | if (!CanonicalIV) { |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1433 | // Create and insert the PHI node for the induction variable in the |
| 1434 | // specified loop. |
| 1435 | BasicBlock *Header = L->getHeader(); |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1436 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1437 | CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar", |
| 1438 | Header->begin()); |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1439 | rememberInstruction(CanonicalIV); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1440 | |
Hal Finkel | 3f5279c | 2013-08-18 00:16:23 +0000 | [diff] [blame] | 1441 | SmallSet<BasicBlock *, 4> PredSeen; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1442 | Constant *One = ConstantInt::get(Ty, 1); |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1443 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Gabor Greif | e82532a | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1444 | BasicBlock *HP = *HPI; |
Hal Finkel | 3f5279c | 2013-08-18 00:16:23 +0000 | [diff] [blame] | 1445 | if (!PredSeen.insert(HP)) |
| 1446 | continue; |
| 1447 | |
Gabor Greif | e82532a | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1448 | if (L->contains(HP)) { |
Dan Gohman | 510bffc | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 1449 | // Insert a unit add instruction right before the terminator |
| 1450 | // corresponding to the back-edge. |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1451 | Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One, |
| 1452 | "indvar.next", |
| 1453 | HP->getTerminator()); |
Devang Patel | ccf8dbf | 2011-06-22 20:56:56 +0000 | [diff] [blame] | 1454 | Add->setDebugLoc(HP->getTerminator()->getDebugLoc()); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1455 | rememberInstruction(Add); |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1456 | CanonicalIV->addIncoming(Add, HP); |
Dan Gohman | 2aab867 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1457 | } else { |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1458 | CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP); |
Dan Gohman | 2aab867 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1459 | } |
Gabor Greif | e82532a | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1460 | } |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1463 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 1464 | if (S->isAffine() && S->getOperand(1)->isOne()) { |
| 1465 | assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && |
| 1466 | "IVs with types different from the canonical IV should " |
| 1467 | "already have been handled!"); |
| 1468 | return CanonicalIV; |
| 1469 | } |
| 1470 | |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1471 | // {0,+,F} --> {0,+,1} * F |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | f0b77f9 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 1473 | // If this is a simple linear addrec, emit it now as a special case. |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1474 | if (S->isAffine()) // {0,+,F} --> i*F |
| 1475 | return |
| 1476 | expand(SE.getTruncateOrNoop( |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1477 | SE.getMulExpr(SE.getUnknown(CanonicalIV), |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1478 | SE.getNoopOrAnyExtend(S->getOperand(1), |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1479 | CanonicalIV->getType())), |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1480 | Ty)); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1481 | |
| 1482 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 1483 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 1484 | // simplify the expression without having to build a bunch of special code |
| 1485 | // into this folder. |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1486 | const SCEV *IH = SE.getUnknown(CanonicalIV); // Get I as a "symbolic" SCEV. |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1487 | |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1488 | // Promote S up to the canonical IV type, if the cast is foldable. |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1489 | const SCEV *NewS = S; |
Dan Gohman | cd83870 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1490 | const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType()); |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1491 | if (isa<SCEVAddRecExpr>(Ext)) |
| 1492 | NewS = Ext; |
| 1493 | |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1494 | const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1495 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1496 | |
Dan Gohman | 426901a | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1497 | // Truncate the result down to the original type, if needed. |
Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1498 | const SCEV *T = SE.getTruncateOrNoop(V, Ty); |
Dan Gohman | fd76113 | 2009-06-22 22:08:45 +0000 | [diff] [blame] | 1499 | return expand(T); |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1500 | } |
Anton Korobeynikov | 5849a62 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1501 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1502 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1503 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1504 | Value *V = expandCodeFor(S->getOperand(), |
| 1505 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1506 | Value *I = Builder.CreateTrunc(V, Ty); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1507 | rememberInstruction(I); |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1508 | return I; |
Dan Gohman | 0e4cf89 | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1511 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1512 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1513 | Value *V = expandCodeFor(S->getOperand(), |
| 1514 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1515 | Value *I = Builder.CreateZExt(V, Ty); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1516 | rememberInstruction(I); |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1517 | return I; |
Dan Gohman | 0e4cf89 | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1520 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1521 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1522 | Value *V = expandCodeFor(S->getOperand(), |
| 1523 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1524 | Value *I = Builder.CreateSExt(V, Ty); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1525 | rememberInstruction(I); |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1526 | return I; |
Dan Gohman | 0e4cf89 | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1529 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1530 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1531 | Type *Ty = LHS->getType(); |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1532 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1533 | // In the case of mixed integer and pointer types, do the |
| 1534 | // rest of the comparisons as integer. |
| 1535 | if (S->getOperand(i)->getType() != Ty) { |
| 1536 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1537 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1538 | } |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1539 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1540 | Value *ICmp = Builder.CreateICmpSGT(LHS, RHS); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1541 | rememberInstruction(ICmp); |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1542 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1543 | rememberInstruction(Sel); |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1544 | LHS = Sel; |
Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1545 | } |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1546 | // In the case of mixed integer and pointer types, cast the |
| 1547 | // final result back to the pointer type. |
| 1548 | if (LHS->getType() != S->getType()) |
| 1549 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1550 | return LHS; |
| 1551 | } |
| 1552 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1553 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1554 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1555 | Type *Ty = LHS->getType(); |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1556 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1557 | // In the case of mixed integer and pointer types, do the |
| 1558 | // rest of the comparisons as integer. |
| 1559 | if (S->getOperand(i)->getType() != Ty) { |
| 1560 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1561 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1562 | } |
Dan Gohman | b8597bd | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1563 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1564 | Value *ICmp = Builder.CreateICmpUGT(LHS, RHS); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1565 | rememberInstruction(ICmp); |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1566 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1567 | rememberInstruction(Sel); |
Dan Gohman | d195a22 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1568 | LHS = Sel; |
Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1569 | } |
Dan Gohman | 92b969b | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1570 | // In the case of mixed integer and pointer types, cast the |
| 1571 | // final result back to the pointer type. |
| 1572 | if (LHS->getType() != S->getType()) |
| 1573 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1574 | return LHS; |
| 1575 | } |
| 1576 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1577 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1578 | Instruction *IP) { |
Dan Gohman | 89d4e3c | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1579 | Builder.SetInsertPoint(IP->getParent(), IP); |
| 1580 | return expandCodeFor(SH, Ty); |
| 1581 | } |
| 1582 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1583 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { |
Dan Gohman | 0e4cf89 | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1584 | // Expand the code for this SCEV. |
Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1585 | Value *V = expand(SH); |
Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 1586 | if (Ty) { |
| 1587 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 1588 | "non-trivial casts should be done with the SCEVs directly!"); |
| 1589 | V = InsertNoopCastOfTo(V, Ty); |
| 1590 | } |
| 1591 | return V; |
Dan Gohman | 0e4cf89 | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Dan Gohman | 056857a | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1594 | Value *SCEVExpander::expand(const SCEV *S) { |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1595 | // Compute an insertion point for this SCEV object. Hoist the instructions |
| 1596 | // as far out in the loop nest as possible. |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1597 | Instruction *InsertPt = Builder.GetInsertPoint(); |
| 1598 | for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ; |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1599 | L = L->getParentLoop()) |
Dan Gohman | afd6db9 | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1600 | if (SE.isLoopInvariant(S, L)) { |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1601 | if (!L) break; |
Dan Gohman | dcddd57 | 2010-03-23 21:53:22 +0000 | [diff] [blame] | 1602 | if (BasicBlock *Preheader = L->getLoopPreheader()) |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1603 | InsertPt = Preheader->getTerminator(); |
Andrew Trick | cbcc98f | 2012-01-02 21:25:10 +0000 | [diff] [blame] | 1604 | else { |
| 1605 | // LSR sets the insertion point for AddRec start/step values to the |
| 1606 | // block start to simplify value reuse, even though it's an invalid |
| 1607 | // position. SCEVExpander must correct for this in all cases. |
| 1608 | InsertPt = L->getHeader()->getFirstInsertionPt(); |
| 1609 | } |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | // If the SCEV is computable at this level, insert it into the header |
| 1612 | // after the PHIs (and after any other instructions that we've inserted |
| 1613 | // there) so that it is guaranteed to dominate any user inside the loop. |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1614 | if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) |
| 1615 | InsertPt = L->getHeader()->getFirstInsertionPt(); |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1616 | while (InsertPt != Builder.GetInsertPoint() |
| 1617 | && (isInsertedInstruction(InsertPt) |
| 1618 | || isa<DbgInfoIntrinsic>(InsertPt))) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1619 | InsertPt = std::next(BasicBlock::iterator(InsertPt)); |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1620 | } |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | } |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1623 | |
Dan Gohman | daafbe6 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1624 | // Check to see if we already expanded this here. |
Andrew Trick | d4e1b5e | 2013-01-14 21:00:37 +0000 | [diff] [blame] | 1625 | std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >::iterator |
| 1626 | I = InsertedExpressions.find(std::make_pair(S, InsertPt)); |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1627 | if (I != InsertedExpressions.end()) |
Dan Gohman | daafbe6 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1628 | return I->second; |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1629 | |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1630 | BuilderType::InsertPointGuard Guard(Builder); |
Dan Gohman | 830fd38 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1631 | Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); |
Dan Gohman | daafbe6 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Expand the expression into instructions. |
Anton Korobeynikov | 5849a62 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1634 | Value *V = visit(S); |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1635 | |
Dan Gohman | daafbe6 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1636 | // Remember the expanded value for this SCEV at this location. |
Andrew Trick | 870c1a3 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1637 | // |
| 1638 | // This is independent of PostIncLoops. The mapped value simply materializes |
| 1639 | // the expression at this insertion point. If the mapped value happened to be |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 1640 | // a postinc expansion, it could be reused by a non-postinc user, but only if |
Andrew Trick | 870c1a3 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1641 | // its insertion point was already at the head of the loop. |
| 1642 | InsertedExpressions[std::make_pair(S, InsertPt)] = V; |
Anton Korobeynikov | 5849a62 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1643 | return V; |
| 1644 | } |
Dan Gohman | 63964b5 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1645 | |
Dan Gohman | 6b75173 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1646 | void SCEVExpander::rememberInstruction(Value *I) { |
Dan Gohman | bbfb6ac | 2010-06-05 00:33:07 +0000 | [diff] [blame] | 1647 | if (!PostIncLoops.empty()) |
| 1648 | InsertedPostIncValues.insert(I); |
| 1649 | else |
Dan Gohman | 6b75173 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1650 | InsertedValues.insert(I); |
Dan Gohman | 6b75173 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Dan Gohman | 63964b5 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1653 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 1654 | /// canonical induction variable of the specified type for the specified |
| 1655 | /// loop (inserting one if there is none). A canonical induction variable |
| 1656 | /// starts at zero and steps by one on each iteration. |
Dan Gohman | 4fd9243 | 2010-07-20 16:44:52 +0000 | [diff] [blame] | 1657 | PHINode * |
Dan Gohman | 63964b5 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1658 | SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1659 | Type *Ty) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1660 | assert(Ty->isIntegerTy() && "Can only insert integer induction variables!"); |
Dan Gohman | 3115875 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Build a SCEV for {0,+,1}<L>. |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1663 | // Conservatively use FlagAnyWrap for now. |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1664 | const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1665 | SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap); |
Dan Gohman | 3115875 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Emit code for it. |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1668 | BuilderType::InsertPointGuard Guard(Builder); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame^] | 1669 | PHINode *V = cast<PHINode>(expandCodeFor(H, nullptr, |
| 1670 | L->getHeader()->begin())); |
Dan Gohman | 3115875 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1671 | |
Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1672 | return V; |
Dan Gohman | 63964b5 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1673 | } |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1674 | |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1675 | /// replaceCongruentIVs - Check for congruent phis in this loop header and |
| 1676 | /// replace them with their most canonical representative. Return the number of |
| 1677 | /// phis eliminated. |
| 1678 | /// |
| 1679 | /// This does not depend on any SCEVExpander state but should be used in |
| 1680 | /// the same context that SCEVExpander is used. |
| 1681 | unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT, |
Nadav Rotem | 4dc976f | 2012-10-19 21:28:43 +0000 | [diff] [blame] | 1682 | SmallVectorImpl<WeakVH> &DeadInsts, |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1683 | const TargetTransformInfo *TTI) { |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1684 | // Find integer phis in order of increasing width. |
| 1685 | SmallVector<PHINode*, 8> Phis; |
| 1686 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 1687 | PHINode *Phi = dyn_cast<PHINode>(I); ++I) { |
| 1688 | Phis.push_back(Phi); |
| 1689 | } |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1690 | if (TTI) |
Benjamin Kramer | b0f74b2 | 2014-03-07 21:35:39 +0000 | [diff] [blame] | 1691 | std::sort(Phis.begin(), Phis.end(), [](Value *LHS, Value *RHS) { |
| 1692 | // Put pointers at the back and make sure pointer < pointer = false. |
| 1693 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) |
| 1694 | return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy(); |
| 1695 | return RHS->getType()->getPrimitiveSizeInBits() < |
| 1696 | LHS->getType()->getPrimitiveSizeInBits(); |
| 1697 | }); |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1698 | |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1699 | unsigned NumElim = 0; |
| 1700 | DenseMap<const SCEV *, PHINode *> ExprToIVMap; |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1701 | // Process phis from wide to narrow. Mapping wide phis to the their truncation |
| 1702 | // so narrow phis can reuse them. |
| 1703 | for (SmallVectorImpl<PHINode*>::const_iterator PIter = Phis.begin(), |
| 1704 | PEnd = Phis.end(); PIter != PEnd; ++PIter) { |
| 1705 | PHINode *Phi = *PIter; |
| 1706 | |
Benjamin Kramer | a225ed8 | 2012-10-19 16:37:30 +0000 | [diff] [blame] | 1707 | // Fold constant phis. They may be congruent to other constant phis and |
| 1708 | // would confuse the logic below that expects proper IVs. |
| 1709 | if (Value *V = Phi->hasConstantValue()) { |
| 1710 | Phi->replaceAllUsesWith(V); |
| 1711 | DeadInsts.push_back(Phi); |
| 1712 | ++NumElim; |
| 1713 | DEBUG_WITH_TYPE(DebugType, dbgs() |
| 1714 | << "INDVARS: Eliminated constant iv: " << *Phi << '\n'); |
| 1715 | continue; |
| 1716 | } |
| 1717 | |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1718 | if (!SE.isSCEVable(Phi->getType())) |
| 1719 | continue; |
| 1720 | |
| 1721 | PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)]; |
| 1722 | if (!OrigPhiRef) { |
| 1723 | OrigPhiRef = Phi; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1724 | if (Phi->getType()->isIntegerTy() && TTI |
| 1725 | && TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) { |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1726 | // This phi can be freely truncated to the narrowest phi type. Map the |
| 1727 | // truncated expression to it so it will be reused for narrow types. |
| 1728 | const SCEV *TruncExpr = |
| 1729 | SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType()); |
| 1730 | ExprToIVMap[TruncExpr] = Phi; |
| 1731 | } |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1732 | continue; |
| 1733 | } |
| 1734 | |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1735 | // Replacing a pointer phi with an integer phi or vice-versa doesn't make |
| 1736 | // sense. |
| 1737 | if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy()) |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1738 | continue; |
| 1739 | |
| 1740 | if (BasicBlock *LatchBlock = L->getLoopLatch()) { |
| 1741 | Instruction *OrigInc = |
| 1742 | cast<Instruction>(OrigPhiRef->getIncomingValueForBlock(LatchBlock)); |
| 1743 | Instruction *IsomorphicInc = |
| 1744 | cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock)); |
| 1745 | |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1746 | // If this phi has the same width but is more canonical, replace the |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1747 | // original with it. As part of the "more canonical" determination, |
| 1748 | // respect a prior decision to use an IV chain. |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1749 | if (OrigPhiRef->getType() == Phi->getType() |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1750 | && !(ChainedPhis.count(Phi) |
| 1751 | || isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)) |
| 1752 | && (ChainedPhis.count(Phi) |
| 1753 | || isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) { |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1754 | std::swap(OrigPhiRef, Phi); |
| 1755 | std::swap(OrigInc, IsomorphicInc); |
| 1756 | } |
| 1757 | // Replacing the congruent phi is sufficient because acyclic redundancy |
| 1758 | // elimination, CSE/GVN, should handle the rest. However, once SCEV proves |
| 1759 | // that a phi is congruent, it's often the head of an IV user cycle that |
Andrew Trick | f730f39 | 2012-01-07 01:29:21 +0000 | [diff] [blame] | 1760 | // is isomorphic with the original phi. It's worth eagerly cleaning up the |
| 1761 | // common case of a single IV increment so that DeleteDeadPHIs can remove |
| 1762 | // cycles that had postinc uses. |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1763 | const SCEV *TruncExpr = SE.getTruncateOrNoop(SE.getSCEV(OrigInc), |
| 1764 | IsomorphicInc->getType()); |
| 1765 | if (OrigInc != IsomorphicInc |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 1766 | && TruncExpr == SE.getSCEV(IsomorphicInc) |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1767 | && ((isa<PHINode>(OrigInc) && isa<PHINode>(IsomorphicInc)) |
| 1768 | || hoistIVInc(OrigInc, IsomorphicInc))) { |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1769 | DEBUG_WITH_TYPE(DebugType, dbgs() |
| 1770 | << "INDVARS: Eliminated congruent iv.inc: " |
| 1771 | << *IsomorphicInc << '\n'); |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1772 | Value *NewInc = OrigInc; |
| 1773 | if (OrigInc->getType() != IsomorphicInc->getType()) { |
Andrew Trick | 23ef0d6 | 2012-01-14 03:17:23 +0000 | [diff] [blame] | 1774 | Instruction *IP = isa<PHINode>(OrigInc) |
| 1775 | ? (Instruction*)L->getHeader()->getFirstInsertionPt() |
| 1776 | : OrigInc->getNextNode(); |
| 1777 | IRBuilder<> Builder(IP); |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1778 | Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc()); |
| 1779 | NewInc = Builder. |
| 1780 | CreateTruncOrBitCast(OrigInc, IsomorphicInc->getType(), IVName); |
| 1781 | } |
| 1782 | IsomorphicInc->replaceAllUsesWith(NewInc); |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1783 | DeadInsts.push_back(IsomorphicInc); |
| 1784 | } |
| 1785 | } |
| 1786 | DEBUG_WITH_TYPE(DebugType, dbgs() |
| 1787 | << "INDVARS: Eliminated congruent iv: " << *Phi << '\n'); |
| 1788 | ++NumElim; |
Andrew Trick | 5adedf5 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1789 | Value *NewIV = OrigPhiRef; |
| 1790 | if (OrigPhiRef->getType() != Phi->getType()) { |
| 1791 | IRBuilder<> Builder(L->getHeader()->getFirstInsertionPt()); |
| 1792 | Builder.SetCurrentDebugLocation(Phi->getDebugLoc()); |
| 1793 | NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName); |
| 1794 | } |
| 1795 | Phi->replaceAllUsesWith(NewIV); |
Andrew Trick | f9201c5 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1796 | DeadInsts.push_back(Phi); |
| 1797 | } |
| 1798 | return NumElim; |
| 1799 | } |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1800 | |
| 1801 | namespace { |
| 1802 | // Search for a SCEV subexpression that is not safe to expand. Any expression |
| 1803 | // that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely |
| 1804 | // UDiv expressions. We don't know if the UDiv is derived from an IR divide |
| 1805 | // instruction, but the important thing is that we prove the denominator is |
| 1806 | // nonzero before expansion. |
| 1807 | // |
| 1808 | // IVUsers already checks that IV-derived expressions are safe. So this check is |
| 1809 | // only needed when the expression includes some subexpression that is not IV |
| 1810 | // derived. |
| 1811 | // |
| 1812 | // Currently, we only allow division by a nonzero constant here. If this is |
| 1813 | // inadequate, we could easily allow division by SCEVUnknown by using |
| 1814 | // ValueTracking to check isKnownNonZero(). |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1815 | // |
| 1816 | // We cannot generally expand recurrences unless the step dominates the loop |
| 1817 | // header. The expander handles the special case of affine recurrences by |
| 1818 | // scaling the recurrence outside the loop, but this technique isn't generally |
| 1819 | // applicable. Expanding a nested recurrence outside a loop requires computing |
| 1820 | // binomial coefficients. This could be done, but the recurrence has to be in a |
| 1821 | // perfectly reduced form, which can't be guaranteed. |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1822 | struct SCEVFindUnsafe { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1823 | ScalarEvolution &SE; |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1824 | bool IsUnsafe; |
| 1825 | |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1826 | SCEVFindUnsafe(ScalarEvolution &se): SE(se), IsUnsafe(false) {} |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1827 | |
| 1828 | bool follow(const SCEV *S) { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1829 | if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 1830 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(D->getRHS()); |
| 1831 | if (!SC || SC->getValue()->isZero()) { |
| 1832 | IsUnsafe = true; |
| 1833 | return false; |
| 1834 | } |
| 1835 | } |
| 1836 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 1837 | const SCEV *Step = AR->getStepRecurrence(SE); |
| 1838 | if (!AR->isAffine() && !SE.dominates(Step, AR->getLoop()->getHeader())) { |
| 1839 | IsUnsafe = true; |
| 1840 | return false; |
| 1841 | } |
| 1842 | } |
| 1843 | return true; |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1844 | } |
| 1845 | bool isDone() const { return IsUnsafe; } |
| 1846 | }; |
| 1847 | } |
| 1848 | |
| 1849 | namespace llvm { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1850 | bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE) { |
| 1851 | SCEVFindUnsafe Search(SE); |
Andrew Trick | 653513b | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1852 | visitAll(S, Search); |
| 1853 | return !Search.IsUnsafe; |
| 1854 | } |
| 1855 | } |