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