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