Nick Lewycky | 7be7739 | 2016-04-24 17:55:41 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis ------------===// |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 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 | 36f891bd | 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 | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 974a445 | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | b7f1fb4 | 2014-06-21 11:47:18 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/InstructionSimplify.h" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | e4ba75f | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 56e1394 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
Jingyue Wu | 5d59315 | 2015-06-24 19:28:40 +0000 | [diff] [blame] | 27 | #include "llvm/IR/PatternMatch.h" |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 30 | |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Jingyue Wu | 5d59315 | 2015-06-24 19:28:40 +0000 | [diff] [blame] | 32 | using namespace PatternMatch; |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 33 | |
Gabor Greif | 19e5ada | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 34 | /// 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] | 35 | /// reusing an existing cast if a suitable one exists, moving an existing |
| 36 | /// 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] | 37 | /// creating a new one. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 38 | Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty, |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 39 | Instruction::CastOps Op, |
| 40 | BasicBlock::iterator IP) { |
Rafael Espindola | 919a503 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 41 | // This function must be called with the builder having a valid insertion |
| 42 | // point. It doesn't need to be the actual IP where the uses of the returned |
| 43 | // cast will be added, but it must dominate such IP. |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 44 | // We use this precondition to produce a cast that will dominate all its |
| 45 | // uses. In particular, this is crucial for the case where the builder's |
| 46 | // 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] | 47 | // Since we don't know the builder's insertion point is actually |
Rafael Espindola | 919a503 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 48 | // where the uses will be added (only that it dominates it), we are |
| 49 | // not allowed to move it. |
| 50 | BasicBlock::iterator BIP = Builder.GetInsertPoint(); |
| 51 | |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 52 | Instruction *Ret = nullptr; |
Rafael Espindola | ef4c80e | 2012-02-18 17:22:58 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 54 | // Check to see if there is already a cast! |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 55 | for (User *U : V->users()) |
Gabor Greif | f64f9cf | 2010-07-09 16:39:02 +0000 | [diff] [blame] | 56 | if (U->getType() == Ty) |
Gabor Greif | 19e5ada | 2010-07-09 16:42:04 +0000 | [diff] [blame] | 57 | if (CastInst *CI = dyn_cast<CastInst>(U)) |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 58 | if (CI->getOpcode() == Op) { |
Rafael Espindola | b84d540 | 2012-02-22 03:44:46 +0000 | [diff] [blame] | 59 | // If the cast isn't where we want it, create a new cast at IP. |
| 60 | // Likewise, do not reuse a cast at BIP because it must dominate |
| 61 | // instructions that might be inserted before BIP. |
Rafael Espindola | 919a503 | 2012-02-22 03:21:39 +0000 | [diff] [blame] | 62 | if (BasicBlock::iterator(CI) != IP || BIP == IP) { |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 63 | // Create a new cast, and leave the old cast in place in case |
| 64 | // it is being used as an insert point. Clear its operand |
| 65 | // so that it doesn't hold anything live. |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 66 | Ret = CastInst::Create(Op, V, Ty, "", &*IP); |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 67 | Ret->takeName(CI); |
| 68 | CI->replaceAllUsesWith(Ret); |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 69 | CI->setOperand(0, UndefValue::get(V->getType())); |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 70 | break; |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 71 | } |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 72 | Ret = CI; |
| 73 | break; |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Create a new cast. |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 77 | if (!Ret) |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 78 | Ret = CastInst::Create(Op, V, Ty, V->getName(), &*IP); |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 79 | |
| 80 | // We assert at the end of the function since IP might point to an |
| 81 | // instruction with different dominance properties than a cast |
| 82 | // (an invoke for example) and not dominate BIP (but the cast does). |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 83 | assert(SE.DT.dominates(Ret, &*BIP)); |
Rafael Espindola | 23b6ec9 | 2012-02-27 02:13:03 +0000 | [diff] [blame] | 84 | |
| 85 | rememberInstruction(Ret); |
| 86 | return Ret; |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David Majnemer | 1089dfc | 2015-10-27 07:36:42 +0000 | [diff] [blame] | 89 | static BasicBlock::iterator findInsertPointAfter(Instruction *I, |
David Majnemer | 1089dfc | 2015-10-27 07:36:42 +0000 | [diff] [blame] | 90 | BasicBlock *MustDominate) { |
| 91 | BasicBlock::iterator IP = ++I->getIterator(); |
| 92 | if (auto *II = dyn_cast<InvokeInst>(I)) |
| 93 | IP = II->getNormalDest()->begin(); |
David Majnemer | 1089dfc | 2015-10-27 07:36:42 +0000 | [diff] [blame] | 94 | |
| 95 | while (isa<PHINode>(IP)) |
| 96 | ++IP; |
| 97 | |
David Majnemer | 5b21066 | 2016-02-03 21:30:31 +0000 | [diff] [blame] | 98 | if (isa<FuncletPadInst>(IP) || isa<LandingPadInst>(IP)) { |
| 99 | ++IP; |
| 100 | } else if (isa<CatchSwitchInst>(IP)) { |
| 101 | IP = MustDominate->getFirstInsertionPt(); |
| 102 | } else { |
| 103 | assert(!IP->isEHPad() && "unexpected eh pad!"); |
David Majnemer | 1089dfc | 2015-10-27 07:36:42 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return IP; |
| 107 | } |
| 108 | |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 109 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 110 | /// which must be possible with a noop cast, doing what we can to share |
| 111 | /// the casts. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 112 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) { |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 113 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 114 | assert((Op == Instruction::BitCast || |
| 115 | Op == Instruction::PtrToInt || |
| 116 | Op == Instruction::IntToPtr) && |
| 117 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 118 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 119 | "InsertNoopCastOfTo cannot change sizes!"); |
| 120 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 121 | // Short-circuit unnecessary bitcasts. |
Andrew Trick | 19154f4 | 2011-12-14 22:07:19 +0000 | [diff] [blame] | 122 | if (Op == Instruction::BitCast) { |
| 123 | if (V->getType() == Ty) |
| 124 | return V; |
| 125 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 126 | if (CI->getOperand(0)->getType() == Ty) |
| 127 | return CI->getOperand(0); |
| 128 | } |
| 129 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 130 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 131 | if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) && |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 132 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 133 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 134 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 135 | CI->getOpcode() == Instruction::IntToPtr) && |
| 136 | SE.getTypeSizeInBits(CI->getType()) == |
| 137 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 138 | return CI->getOperand(0); |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 139 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 140 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 141 | CE->getOpcode() == Instruction::IntToPtr) && |
| 142 | SE.getTypeSizeInBits(CE->getType()) == |
| 143 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 144 | return CE->getOperand(0); |
| 145 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 146 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 147 | // Fold a cast of a constant. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 148 | if (Constant *C = dyn_cast<Constant>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 149 | return ConstantExpr::getCast(Op, C, Ty); |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 150 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 151 | // Cast the argument at the beginning of the entry block, after |
| 152 | // any bitcasts of other arguments. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 153 | if (Argument *A = dyn_cast<Argument>(V)) { |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 154 | BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin(); |
| 155 | while ((isa<BitCastInst>(IP) && |
| 156 | isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) && |
| 157 | cast<BitCastInst>(IP)->getOperand(0) != A) || |
David Majnemer | 1089dfc | 2015-10-27 07:36:42 +0000 | [diff] [blame] | 158 | isa<DbgInfoIntrinsic>(IP)) |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 159 | ++IP; |
| 160 | return ReuseOrCreateCast(A, Ty, Op, IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 161 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 162 | |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 163 | // Cast the instruction immediately after the instruction. |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 164 | Instruction *I = cast<Instruction>(V); |
Florian Hahn | 26ac94d | 2018-06-25 20:55:26 +0000 | [diff] [blame] | 165 | BasicBlock::iterator IP = findInsertPointAfter(I, Builder.GetInsertBlock()); |
Dan Gohman | 485c43f | 2010-06-19 13:25:23 +0000 | [diff] [blame] | 166 | return ReuseOrCreateCast(I, Ty, Op, IP); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 169 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 170 | /// of work to avoid inserting an obviously redundant operation. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 171 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, |
| 172 | Value *LHS, Value *RHS) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 173 | // Fold a binop with constant operands. |
| 174 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 175 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 176 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 178 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 179 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 180 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 181 | // Scanning starts from the last instruction before the insertion point. |
| 182 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 183 | if (IP != BlockBegin) { |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 184 | --IP; |
| 185 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | 8d50ea7 | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 186 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 187 | // generated code. |
| 188 | if (isa<DbgInfoIntrinsic>(IP)) |
| 189 | ScanLimit++; |
Serguei Katkov | fe51c81 | 2017-12-27 08:26:22 +0000 | [diff] [blame] | 190 | |
| 191 | // Conservatively, do not use any instruction which has any of wrap/exact |
| 192 | // flags installed. |
| 193 | // TODO: Instead of simply disable poison instructions we can be clever |
| 194 | // here and match SCEV to this instruction. |
| 195 | auto canGeneratePoison = [](Instruction *I) { |
| 196 | if (isa<OverflowingBinaryOperator>(I) && |
| 197 | (I->hasNoSignedWrap() || I->hasNoUnsignedWrap())) |
| 198 | return true; |
| 199 | if (isa<PossiblyExactOperator>(I) && I->isExact()) |
| 200 | return true; |
| 201 | return false; |
| 202 | }; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 203 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
Serguei Katkov | fe51c81 | 2017-12-27 08:26:22 +0000 | [diff] [blame] | 204 | IP->getOperand(1) == RHS && !canGeneratePoison(&*IP)) |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 205 | return &*IP; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 206 | if (IP == BlockBegin) break; |
| 207 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 208 | } |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 209 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 210 | // Save the original insertion point so we can restore it when we're done. |
Benjamin Kramer | d427882 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 211 | DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 212 | SCEVInsertPointGuard Guard(Builder, this); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 213 | |
Reid Kleckner | 5697690 | 2016-12-12 18:52:32 +0000 | [diff] [blame] | 214 | // Move the insertion point out of as many loops as we can. |
| 215 | while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) { |
| 216 | if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; |
| 217 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 218 | if (!Preheader) break; |
Sanjoy Das | b2e86a3 | 2016-11-10 07:56:12 +0000 | [diff] [blame] | 219 | |
Reid Kleckner | 5697690 | 2016-12-12 18:52:32 +0000 | [diff] [blame] | 220 | // Ok, move up a level. |
| 221 | Builder.SetInsertPoint(Preheader->getTerminator()); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 224 | // If we haven't found this binop, insert it. |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 225 | Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS)); |
Benjamin Kramer | d427882 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 226 | BO->setDebugLoc(Loc); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 227 | rememberInstruction(BO); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 228 | |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 229 | return BO; |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 232 | /// FactorOutConstant - Test if S is divisible by Factor, using signed |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 233 | /// division. If so, update S with Factor divided out and return true. |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 234 | /// S need not be evenly divisible if a reasonable remainder can be |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 235 | /// computed. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 236 | /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made |
| 237 | /// unnecessary; in its place, just signed-divide Ops[i] by the scale and |
| 238 | /// check to see if the divide was folded. |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 239 | static bool FactorOutConstant(const SCEV *&S, const SCEV *&Remainder, |
| 240 | const SCEV *Factor, ScalarEvolution &SE, |
| 241 | const DataLayout &DL) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 242 | // Everything is divisible by one. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 243 | if (Factor->isOne()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 244 | return true; |
| 245 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 246 | // x/x == 1. |
| 247 | if (S == Factor) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 248 | S = SE.getConstant(S->getType(), 1); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 252 | // For a Constant, check for a multiple of the given factor. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 253 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 254 | // 0/x == 0. |
| 255 | if (C->isZero()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 256 | return true; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 257 | // Check for divisibility. |
| 258 | if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) { |
| 259 | ConstantInt *CI = |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 260 | ConstantInt::get(SE.getContext(), C->getAPInt().sdiv(FC->getAPInt())); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 261 | // If the quotient is zero and the remainder is non-zero, reject |
| 262 | // the value at this scale. It will be considered for subsequent |
| 263 | // smaller scales. |
| 264 | if (!CI->isZero()) { |
| 265 | const SCEV *Div = SE.getConstant(CI); |
| 266 | S = Div; |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 267 | Remainder = SE.getAddExpr( |
| 268 | Remainder, SE.getConstant(C->getAPInt().srem(FC->getAPInt()))); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 269 | return true; |
| 270 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 271 | } |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 272 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 273 | |
| 274 | // In a Mul, check if there is a constant operand which is a multiple |
| 275 | // of the given factor. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 276 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 277 | // Size is known, check if there is a constant operand which is a multiple |
| 278 | // of the given factor. If so, we can factor it. |
| 279 | const SCEVConstant *FC = cast<SCEVConstant>(Factor); |
| 280 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 281 | if (!C->getAPInt().srem(FC->getAPInt())) { |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 282 | SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 283 | NewMulOps[0] = SE.getConstant(C->getAPInt().sdiv(FC->getAPInt())); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 284 | S = SE.getMulExpr(NewMulOps); |
| 285 | return true; |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 286 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 287 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 288 | |
| 289 | // In an AddRec, check if both start and step are divisible. |
| 290 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 291 | const SCEV *Step = A->getStepRecurrence(SE); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 292 | const SCEV *StepRem = SE.getConstant(Step->getType(), 0); |
Rafael Espindola | 39d8dcb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 293 | if (!FactorOutConstant(Step, StepRem, Factor, SE, DL)) |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 294 | return false; |
| 295 | if (!StepRem->isZero()) |
| 296 | return false; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 297 | const SCEV *Start = A->getStart(); |
Rafael Espindola | 39d8dcb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 298 | if (!FactorOutConstant(Start, Remainder, Factor, SE, DL)) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 299 | return false; |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 300 | S = SE.getAddRecExpr(Start, Step, A->getLoop(), |
| 301 | A->getNoWrapFlags(SCEV::FlagNW)); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 308 | /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs |
| 309 | /// is the number of SCEVAddRecExprs present, which are kept at the end of |
| 310 | /// the list. |
| 311 | /// |
| 312 | static void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 313 | Type *Ty, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 314 | ScalarEvolution &SE) { |
| 315 | unsigned NumAddRecs = 0; |
| 316 | for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i) |
| 317 | ++NumAddRecs; |
| 318 | // Group Ops into non-addrecs and addrecs. |
| 319 | SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs); |
| 320 | SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end()); |
| 321 | // Let ScalarEvolution sort and simplify the non-addrecs list. |
| 322 | const SCEV *Sum = NoAddRecs.empty() ? |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 323 | SE.getConstant(Ty, 0) : |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 324 | SE.getAddExpr(NoAddRecs); |
| 325 | // If it returned an add, use the operands. Otherwise it simplified |
| 326 | // the sum into a single value, so just use that. |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 327 | Ops.clear(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 328 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum)) |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 329 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 330 | else if (!Sum->isZero()) |
| 331 | Ops.push_back(Sum); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 332 | // Then append the addrecs. |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 333 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /// SplitAddRecs - Flatten a list of add operands, moving addrec start values |
| 337 | /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}. |
| 338 | /// This helps expose more opportunities for folding parts of the expressions |
| 339 | /// into GEP indices. |
| 340 | /// |
| 341 | static void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 342 | Type *Ty, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 343 | ScalarEvolution &SE) { |
| 344 | // Find the addrecs. |
| 345 | SmallVector<const SCEV *, 8> AddRecs; |
| 346 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 347 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) { |
| 348 | const SCEV *Start = A->getStart(); |
| 349 | if (Start->isZero()) break; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 350 | const SCEV *Zero = SE.getConstant(Ty, 0); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 351 | AddRecs.push_back(SE.getAddRecExpr(Zero, |
| 352 | A->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 353 | A->getLoop(), |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 354 | A->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 355 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) { |
| 356 | Ops[i] = Zero; |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 357 | Ops.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 358 | e += Add->getNumOperands(); |
| 359 | } else { |
| 360 | Ops[i] = Start; |
| 361 | } |
| 362 | } |
| 363 | if (!AddRecs.empty()) { |
| 364 | // Add the addrecs onto the end of the list. |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 365 | Ops.append(AddRecs.begin(), AddRecs.end()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 366 | // Resort the operand list, moving any constants to the front. |
| 367 | SimplifyAddOperands(Ops, Ty, SE); |
| 368 | } |
| 369 | } |
| 370 | |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 371 | /// expandAddToGEP - Expand an addition expression with a pointer type into |
| 372 | /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 373 | /// BasicAliasAnalysis and other passes analyze the result. See the rules |
| 374 | /// for getelementptr vs. inttoptr in |
| 375 | /// http://llvm.org/docs/LangRef.html#pointeraliasing |
| 376 | /// for details. |
Dan Gohman | 13c5e35 | 2009-07-20 17:44:17 +0000 | [diff] [blame] | 377 | /// |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 378 | /// Design note: The correctness of using getelementptr here depends on |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 379 | /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as |
| 380 | /// they may introduce pointer arithmetic which may not be safely converted |
| 381 | /// into getelementptr. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 382 | /// |
| 383 | /// Design note: It might seem desirable for this function to be more |
| 384 | /// loop-aware. If some of the indices are loop-invariant while others |
| 385 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 386 | /// loop-invariant portions of the overall computation outside the loop. |
| 387 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 388 | /// arithmetic is a low-level optimization that often isn't very |
| 389 | /// important until late in the optimization process. In fact, passes |
| 390 | /// like InstructionCombining will combine GEPs, even if it means |
| 391 | /// pushing loop-invariant computation down into loops, so even if the |
| 392 | /// GEPs were split here, the work would quickly be undone. The |
| 393 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 394 | /// after the last InstructionCombining pass), takes care of hoisting |
| 395 | /// loop-invariant portions of expressions, after considering what |
| 396 | /// can be folded using target addressing modes. |
| 397 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 398 | Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin, |
| 399 | const SCEV *const *op_end, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 400 | PointerType *PTy, |
| 401 | Type *Ty, |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 402 | Value *V) { |
David Blaikie | 4e933df | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 403 | Type *OriginalElTy = PTy->getElementType(); |
| 404 | Type *ElTy = OriginalElTy; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 405 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 406 | SmallVector<const SCEV *, 8> Ops(op_begin, op_end); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 407 | bool AnyNonZeroIndices = false; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 408 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 409 | // Split AddRecs up into parts as either of the parts may be usable |
| 410 | // without the other. |
| 411 | SplitAddRecs(Ops, Ty, SE); |
| 412 | |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 413 | Type *IntPtrTy = DL.getIntPtrType(PTy); |
Matt Arsenault | 14807bd | 2013-09-10 19:55:24 +0000 | [diff] [blame] | 414 | |
Bob Wilson | eb35699 | 2009-12-04 01:33:04 +0000 | [diff] [blame] | 415 | // Descend down the pointer's type and attempt to convert the other |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 416 | // operands into GEP indices, at each level. The first index in a GEP |
| 417 | // indexes into the array implied by the pointer operand; the rest of |
| 418 | // the indices index into the element or field type selected by the |
| 419 | // preceding index. |
| 420 | for (;;) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 421 | // If the scale size is not 0, attempt to factor out a scale for |
| 422 | // array indexing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 423 | SmallVector<const SCEV *, 8> ScaledOps; |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 424 | if (ElTy->isSized()) { |
Matt Arsenault | 14807bd | 2013-09-10 19:55:24 +0000 | [diff] [blame] | 425 | const SCEV *ElSize = SE.getSizeOfExpr(IntPtrTy, ElTy); |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 426 | if (!ElSize->isZero()) { |
| 427 | SmallVector<const SCEV *, 8> NewOps; |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 428 | for (const SCEV *Op : Ops) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 429 | const SCEV *Remainder = SE.getConstant(Ty, 0); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 430 | if (FactorOutConstant(Op, Remainder, ElSize, SE, DL)) { |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 431 | // Op now has ElSize factored out. |
| 432 | ScaledOps.push_back(Op); |
| 433 | if (!Remainder->isZero()) |
| 434 | NewOps.push_back(Remainder); |
| 435 | AnyNonZeroIndices = true; |
| 436 | } else { |
| 437 | // The operand was not divisible, so add it to the list of operands |
| 438 | // we'll scan next iteration. |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 439 | NewOps.push_back(Op); |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 440 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 441 | } |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame] | 442 | // If we made any changes, update Ops. |
| 443 | if (!ScaledOps.empty()) { |
| 444 | Ops = NewOps; |
| 445 | SimplifyAddOperands(Ops, Ty, SE); |
| 446 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 447 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 448 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 449 | |
| 450 | // Record the scaled array index for this level of the type. If |
| 451 | // we didn't find any operands that could be factored, tentatively |
| 452 | // assume that element zero was selected (since the zero offset |
| 453 | // would obviously be folded away). |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 454 | Value *Scaled = ScaledOps.empty() ? |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 455 | Constant::getNullValue(Ty) : |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 456 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 457 | GepIndices.push_back(Scaled); |
| 458 | |
| 459 | // Collect struct field index operands. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 460 | while (StructType *STy = dyn_cast<StructType>(ElTy)) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 461 | bool FoundFieldNo = false; |
| 462 | // An empty struct has no fields. |
| 463 | if (STy->getNumElements() == 0) break; |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 464 | // Field offsets are known. See if a constant offset falls within any of |
| 465 | // the struct fields. |
| 466 | if (Ops.empty()) |
| 467 | break; |
| 468 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 469 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 470 | const StructLayout &SL = *DL.getStructLayout(STy); |
| 471 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 472 | if (FullOffset < SL.getSizeInBytes()) { |
| 473 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
| 474 | GepIndices.push_back( |
| 475 | ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx)); |
| 476 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 477 | Ops[0] = |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 478 | SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 479 | AnyNonZeroIndices = true; |
| 480 | FoundFieldNo = true; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 481 | } |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 482 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 483 | // If no struct field offsets were found, tentatively assume that |
| 484 | // field zero was selected (since the zero offset would obviously |
| 485 | // be folded away). |
| 486 | if (!FoundFieldNo) { |
| 487 | ElTy = STy->getTypeAtIndex(0u); |
| 488 | GepIndices.push_back( |
| 489 | Constant::getNullValue(Type::getInt32Ty(Ty->getContext()))); |
| 490 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 491 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 492 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 493 | if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 494 | ElTy = ATy->getElementType(); |
| 495 | else |
| 496 | break; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 499 | // If none of the operands were convertible to proper GEP indices, cast |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 500 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 501 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 502 | if (!AnyNonZeroIndices) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 503 | // Cast the base to i8*. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 504 | V = InsertNoopCastOfTo(V, |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 505 | Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 506 | |
Rafael Espindola | 705b48d | 2012-02-21 03:51:14 +0000 | [diff] [blame] | 507 | assert(!isa<Instruction>(V) || |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 508 | SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint())); |
Rafael Espindola | 4b04578 | 2012-02-21 01:19:51 +0000 | [diff] [blame] | 509 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 510 | // Expand the operands for a plain byte offset. |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 511 | Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 512 | |
| 513 | // Fold a GEP with constant operands. |
| 514 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 515 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
David Blaikie | 19443c1 | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 516 | return ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ty->getContext()), |
| 517 | CLHS, CRHS); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 518 | |
| 519 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 520 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 521 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 522 | // Scanning starts from the last instruction before the insertion point. |
| 523 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 524 | if (IP != BlockBegin) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 525 | --IP; |
| 526 | for (; ScanLimit; --IP, --ScanLimit) { |
Dale Johannesen | 8d50ea7 | 2010-03-05 21:12:40 +0000 | [diff] [blame] | 527 | // Don't count dbg.value against the ScanLimit, to avoid perturbing the |
| 528 | // generated code. |
| 529 | if (isa<DbgInfoIntrinsic>(IP)) |
| 530 | ScanLimit++; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 531 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 532 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 533 | return &*IP; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 534 | if (IP == BlockBegin) break; |
| 535 | } |
| 536 | } |
| 537 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 538 | // Save the original insertion point so we can restore it when we're done. |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 539 | SCEVInsertPointGuard Guard(Builder, this); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 540 | |
| 541 | // Move the insertion point out of as many loops as we can. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 542 | while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 543 | if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break; |
| 544 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 545 | if (!Preheader) break; |
| 546 | |
| 547 | // Ok, move up a level. |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 548 | Builder.SetInsertPoint(Preheader->getTerminator()); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 551 | // Emit a GEP. |
David Blaikie | cf57d81 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 552 | Value *GEP = Builder.CreateGEP(Builder.getInt8Ty(), V, Idx, "uglygep"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 553 | rememberInstruction(GEP); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 554 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 555 | return GEP; |
| 556 | } |
| 557 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 558 | { |
| 559 | SCEVInsertPointGuard Guard(Builder, this); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 560 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 561 | // Move the insertion point out of as many loops as we can. |
| 562 | while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) { |
| 563 | if (!L->isLoopInvariant(V)) break; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 564 | |
David Majnemer | dc9c737 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 565 | bool AnyIndexNotLoopInvariant = any_of( |
| 566 | GepIndices, [L](Value *Op) { return !L->isLoopInvariant(Op); }); |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 567 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 568 | if (AnyIndexNotLoopInvariant) |
| 569 | break; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 570 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 571 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 572 | if (!Preheader) break; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 573 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 574 | // Ok, move up a level. |
| 575 | Builder.SetInsertPoint(Preheader->getTerminator()); |
| 576 | } |
| 577 | |
| 578 | // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, |
| 579 | // because ScalarEvolution may have changed the address arithmetic to |
| 580 | // compute a value which is beyond the end of the allocated object. |
| 581 | Value *Casted = V; |
| 582 | if (V->getType() != PTy) |
| 583 | Casted = InsertNoopCastOfTo(Casted, PTy); |
| 584 | Value *GEP = Builder.CreateGEP(OriginalElTy, Casted, GepIndices, "scevgep"); |
| 585 | Ops.push_back(SE.getUnknown(GEP)); |
| 586 | rememberInstruction(GEP); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 589 | return expand(SE.getAddExpr(Ops)); |
| 590 | } |
| 591 | |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 592 | Value *SCEVExpander::expandAddToGEP(const SCEV *Op, PointerType *PTy, Type *Ty, |
| 593 | Value *V) { |
| 594 | const SCEV *const Ops[1] = {Op}; |
| 595 | return expandAddToGEP(Ops, Ops + 1, PTy, Ty, V); |
| 596 | } |
| 597 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 598 | /// PickMostRelevantLoop - Given two loops pick the one that's most relevant for |
| 599 | /// SCEV expansion. If they are nested, this is the most nested. If they are |
| 600 | /// neighboring, pick the later. |
| 601 | static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B, |
| 602 | DominatorTree &DT) { |
| 603 | if (!A) return B; |
| 604 | if (!B) return A; |
| 605 | if (A->contains(B)) return B; |
| 606 | if (B->contains(A)) return A; |
| 607 | if (DT.dominates(A->getHeader(), B->getHeader())) return B; |
| 608 | if (DT.dominates(B->getHeader(), A->getHeader())) return A; |
| 609 | return A; // Arbitrarily break the tie. |
| 610 | } |
| 611 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 612 | /// getRelevantLoop - Get the most relevant loop associated with the given |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 613 | /// expression, according to PickMostRelevantLoop. |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 614 | const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) { |
| 615 | // Test whether we've already computed the most relevant loop for this SCEV. |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 616 | auto Pair = RelevantLoops.insert(std::make_pair(S, nullptr)); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 617 | if (!Pair.second) |
| 618 | return Pair.first->second; |
| 619 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 620 | if (isa<SCEVConstant>(S)) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 621 | // A constant has no relevant loops. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 622 | return nullptr; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 623 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 624 | if (const Instruction *I = dyn_cast<Instruction>(U->getValue())) |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 625 | return Pair.first->second = SE.LI.getLoopFor(I->getParent()); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 626 | // A non-instruction has no relevant loops. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 627 | return nullptr; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 628 | } |
| 629 | if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) { |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 630 | const Loop *L = nullptr; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 631 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 632 | L = AR->getLoop(); |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 633 | for (const SCEV *Op : N->operands()) |
| 634 | L = PickMostRelevantLoop(L, getRelevantLoop(Op), SE.DT); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 635 | return RelevantLoops[N] = L; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 636 | } |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 637 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) { |
| 638 | const Loop *Result = getRelevantLoop(C->getOperand()); |
| 639 | return RelevantLoops[C] = Result; |
| 640 | } |
| 641 | if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 642 | const Loop *Result = PickMostRelevantLoop( |
| 643 | getRelevantLoop(D->getLHS()), getRelevantLoop(D->getRHS()), SE.DT); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 644 | return RelevantLoops[D] = Result; |
| 645 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 646 | llvm_unreachable("Unexpected SCEV type!"); |
| 647 | } |
| 648 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 649 | namespace { |
| 650 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 651 | /// LoopCompare - Compare loops by PickMostRelevantLoop. |
| 652 | class LoopCompare { |
| 653 | DominatorTree &DT; |
| 654 | public: |
| 655 | explicit LoopCompare(DominatorTree &dt) : DT(dt) {} |
| 656 | |
| 657 | bool operator()(std::pair<const Loop *, const SCEV *> LHS, |
| 658 | std::pair<const Loop *, const SCEV *> RHS) const { |
Dan Gohman | bb5d927 | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 659 | // Keep pointer operands sorted at the end. |
| 660 | if (LHS.second->getType()->isPointerTy() != |
| 661 | RHS.second->getType()->isPointerTy()) |
| 662 | return LHS.second->getType()->isPointerTy(); |
| 663 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 664 | // Compare loops with PickMostRelevantLoop. |
| 665 | if (LHS.first != RHS.first) |
| 666 | return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first; |
| 667 | |
| 668 | // If one operand is a non-constant negative and the other is not, |
| 669 | // put the non-constant negative on the right so that a sub can |
| 670 | // be used instead of a negate and add. |
Andrew Trick | f8fd841 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 671 | if (LHS.second->isNonConstantNegative()) { |
| 672 | if (!RHS.second->isNonConstantNegative()) |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 673 | return false; |
Andrew Trick | f8fd841 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 674 | } else if (RHS.second->isNonConstantNegative()) |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 675 | return true; |
| 676 | |
| 677 | // Otherwise they are equivalent according to this comparison. |
| 678 | return false; |
| 679 | } |
| 680 | }; |
| 681 | |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 682 | } |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 683 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 684 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 685 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 686 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 687 | // Collect all the add operands in a loop, along with their associated loops. |
| 688 | // Iterate in reverse so that constants are emitted last, all else equal, and |
| 689 | // so that pointer operands are inserted first, which the code below relies on |
| 690 | // to form more involved GEPs. |
| 691 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 692 | for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()), |
| 693 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 694 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 695 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 696 | // Sort by loop. Use a stable sort so that constants follow non-constants and |
| 697 | // pointer operands precede non-pointer operands. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 698 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(SE.DT)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 699 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 700 | // Emit instructions to add all the operands. Hoist as much as possible |
| 701 | // out of loops, and form meaningful getelementptrs where possible. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 702 | Value *Sum = nullptr; |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 703 | for (auto I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E;) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 704 | const Loop *CurLoop = I->first; |
| 705 | const SCEV *Op = I->second; |
| 706 | if (!Sum) { |
| 707 | // This is the first operand. Just expand it. |
| 708 | Sum = expand(Op); |
| 709 | ++I; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 710 | } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 711 | // The running sum expression is a pointer. Try to form a getelementptr |
| 712 | // at this level with that as the base. |
| 713 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | bb5d927 | 2010-07-15 23:38:13 +0000 | [diff] [blame] | 714 | for (; I != E && I->first == CurLoop; ++I) { |
| 715 | // If the operand is SCEVUnknown and not instructions, peek through |
| 716 | // it, to enable more of it to be folded into the GEP. |
| 717 | const SCEV *X = I->second; |
| 718 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X)) |
| 719 | if (!isa<Instruction>(U->getValue())) |
| 720 | X = SE.getSCEV(U->getValue()); |
| 721 | NewOps.push_back(X); |
| 722 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 723 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 724 | } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 725 | // 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] | 726 | // Try to form a getelementptr. If the running sum is instructions, |
| 727 | // use a SCEVUnknown to avoid re-analyzing them. |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 728 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | f8d0578 | 2010-04-09 19:14:31 +0000 | [diff] [blame] | 729 | NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) : |
| 730 | SE.getSCEV(Sum)); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 731 | for (++I; I != E && I->first == CurLoop; ++I) |
| 732 | NewOps.push_back(I->second); |
| 733 | Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op)); |
Andrew Trick | f8fd841 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 734 | } else if (Op->isNonConstantNegative()) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 735 | // Instead of doing a negate and add, just do a subtract. |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 736 | Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 737 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 738 | Sum = InsertBinop(Instruction::Sub, Sum, W); |
| 739 | ++I; |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 740 | } else { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 741 | // A simple add. |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 742 | Value *W = expandCodeFor(Op, Ty); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 743 | Sum = InsertNoopCastOfTo(Sum, Ty); |
| 744 | // Canonicalize a constant to the RHS. |
| 745 | if (isa<Constant>(Sum)) std::swap(Sum, W); |
| 746 | Sum = InsertBinop(Instruction::Add, Sum, W); |
| 747 | ++I; |
Dan Gohman | ed78dba | 2010-03-03 04:36:42 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 750 | |
| 751 | return Sum; |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 752 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 753 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 754 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 755 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 756 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 757 | // Collect all the mul operands in a loop, along with their associated loops. |
| 758 | // Iterate in reverse so that constants are emitted last, all else equal. |
| 759 | SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; |
| 760 | for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()), |
| 761 | E(S->op_begin()); I != E; ++I) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 762 | OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 763 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 764 | // Sort by loop. Use a stable sort so that constants follow non-constants. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 765 | std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(SE.DT)); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 766 | |
| 767 | // Emit instructions to mul all the operands. Hoist as much as possible |
| 768 | // out of loops. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 769 | Value *Prod = nullptr; |
Max Kazantsev | 9026c3d | 2017-06-19 06:24:53 +0000 | [diff] [blame] | 770 | auto I = OpsAndLoops.begin(); |
| 771 | |
| 772 | // Expand the calculation of X pow N in the following manner: |
| 773 | // Let N = P1 + P2 + ... + PK, where all P are powers of 2. Then: |
| 774 | // X pow N = (X pow P1) * (X pow P2) * ... * (X pow PK). |
| 775 | const auto ExpandOpBinPowN = [this, &I, &OpsAndLoops, &Ty]() { |
| 776 | auto E = I; |
| 777 | // Calculate how many times the same operand from the same loop is included |
| 778 | // into this power. |
| 779 | uint64_t Exponent = 0; |
| 780 | const uint64_t MaxExponent = UINT64_MAX >> 1; |
| 781 | // No one sane will ever try to calculate such huge exponents, but if we |
| 782 | // need this, we stop on UINT64_MAX / 2 because we need to exit the loop |
| 783 | // below when the power of 2 exceeds our Exponent, and we want it to be |
| 784 | // 1u << 31 at most to not deal with unsigned overflow. |
| 785 | while (E != OpsAndLoops.end() && *I == *E && Exponent != MaxExponent) { |
| 786 | ++Exponent; |
| 787 | ++E; |
| 788 | } |
| 789 | assert(Exponent > 0 && "Trying to calculate a zeroth exponent of operand?"); |
| 790 | |
| 791 | // Calculate powers with exponents 1, 2, 4, 8 etc. and include those of them |
| 792 | // that are needed into the result. |
| 793 | Value *P = expandCodeFor(I->second, Ty); |
| 794 | Value *Result = nullptr; |
| 795 | if (Exponent & 1) |
| 796 | Result = P; |
| 797 | for (uint64_t BinExp = 2; BinExp <= Exponent; BinExp <<= 1) { |
| 798 | P = InsertBinop(Instruction::Mul, P, P); |
| 799 | if (Exponent & BinExp) |
| 800 | Result = Result ? InsertBinop(Instruction::Mul, Result, P) : P; |
| 801 | } |
| 802 | |
| 803 | I = E; |
| 804 | assert(Result && "Nothing was expanded?"); |
| 805 | return Result; |
| 806 | }; |
| 807 | |
| 808 | while (I != OpsAndLoops.end()) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 809 | if (!Prod) { |
| 810 | // This is the first operand. Just expand it. |
Max Kazantsev | 9026c3d | 2017-06-19 06:24:53 +0000 | [diff] [blame] | 811 | Prod = ExpandOpBinPowN(); |
| 812 | } else if (I->second->isAllOnesValue()) { |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 813 | // Instead of doing a multiply by negative one, just do a negate. |
| 814 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 815 | Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod); |
Max Kazantsev | 9026c3d | 2017-06-19 06:24:53 +0000 | [diff] [blame] | 816 | ++I; |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 817 | } else { |
| 818 | // A simple mul. |
Max Kazantsev | 9026c3d | 2017-06-19 06:24:53 +0000 | [diff] [blame] | 819 | Value *W = ExpandOpBinPowN(); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 820 | Prod = InsertNoopCastOfTo(Prod, Ty); |
| 821 | // Canonicalize a constant to the RHS. |
| 822 | if (isa<Constant>(Prod)) std::swap(Prod, W); |
Jingyue Wu | 5d59315 | 2015-06-24 19:28:40 +0000 | [diff] [blame] | 823 | const APInt *RHS; |
| 824 | if (match(W, m_Power2(RHS))) { |
| 825 | // Canonicalize Prod*(1<<C) to Prod<<C. |
| 826 | assert(!Ty->isVectorTy() && "vector types are not SCEVable"); |
| 827 | Prod = InsertBinop(Instruction::Shl, Prod, |
| 828 | ConstantInt::get(Ty, RHS->logBase2())); |
| 829 | } else { |
| 830 | Prod = InsertBinop(Instruction::Mul, Prod, W); |
| 831 | } |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 832 | } |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 835 | return Prod; |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 838 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 839 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 840 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 841 | Value *LHS = expandCodeFor(S->getLHS(), Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 842 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 843 | const APInt &RHS = SC->getAPInt(); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 844 | if (RHS.isPowerOf2()) |
| 845 | return InsertBinop(Instruction::LShr, LHS, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 846 | ConstantInt::get(Ty, RHS.logBase2())); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 849 | Value *RHS = expandCodeFor(S->getRHS(), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 850 | return InsertBinop(Instruction::UDiv, LHS, RHS); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 853 | /// Move parts of Base into Rest to leave Base with the minimal |
| 854 | /// expression that provides a pointer operand suitable for a |
| 855 | /// GEP expansion. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 856 | static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 857 | ScalarEvolution &SE) { |
| 858 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 859 | Base = A->getStart(); |
| 860 | Rest = SE.getAddExpr(Rest, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 861 | SE.getAddRecExpr(SE.getConstant(A->getType(), 0), |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 862 | A->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 863 | A->getLoop(), |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 864 | A->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 865 | } |
| 866 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 867 | Base = A->getOperand(A->getNumOperands()-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 868 | SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end()); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 869 | NewAddOps.back() = Rest; |
| 870 | Rest = SE.getAddExpr(NewAddOps); |
| 871 | ExposePointerBase(Base, Rest, SE); |
| 872 | } |
| 873 | } |
| 874 | |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 875 | /// Determine if this is a well-behaved chain of instructions leading back to |
| 876 | /// the PHI. If so, it may be reused by expanded expressions. |
| 877 | bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, |
| 878 | const Loop *L) { |
| 879 | if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) || |
| 880 | (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV))) |
| 881 | return false; |
| 882 | // If any of the operands don't dominate the insert position, bail. |
| 883 | // Addrec operands are always loop-invariant, so this can only happen |
| 884 | // if there are instructions which haven't been hoisted. |
| 885 | if (L == IVIncInsertLoop) { |
| 886 | for (User::op_iterator OI = IncV->op_begin()+1, |
| 887 | OE = IncV->op_end(); OI != OE; ++OI) |
| 888 | if (Instruction *OInst = dyn_cast<Instruction>(OI)) |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 889 | if (!SE.DT.dominates(OInst, IVIncInsertPos)) |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 890 | return false; |
| 891 | } |
| 892 | // Advance to the next instruction. |
| 893 | IncV = dyn_cast<Instruction>(IncV->getOperand(0)); |
| 894 | if (!IncV) |
| 895 | return false; |
| 896 | |
| 897 | if (IncV->mayHaveSideEffects()) |
| 898 | return false; |
| 899 | |
Bjorn Pettersson | e7acf09 | 2017-12-14 14:47:52 +0000 | [diff] [blame] | 900 | if (IncV == PN) |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 901 | return true; |
| 902 | |
| 903 | return isNormalAddRecExprPHI(PN, IncV, L); |
| 904 | } |
| 905 | |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 906 | /// getIVIncOperand returns an induction variable increment's induction |
| 907 | /// variable operand. |
| 908 | /// |
| 909 | /// If allowScale is set, any type of GEP is allowed as long as the nonIV |
| 910 | /// operands dominate InsertPos. |
| 911 | /// |
| 912 | /// If allowScale is not set, ensure that a GEP increment conforms to one of the |
| 913 | /// simple patterns generated by getAddRecExprPHILiterally and |
| 914 | /// expandAddtoGEP. If the pattern isn't recognized, return NULL. |
| 915 | Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV, |
| 916 | Instruction *InsertPos, |
| 917 | bool allowScale) { |
| 918 | if (IncV == InsertPos) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 919 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 920 | |
| 921 | switch (IncV->getOpcode()) { |
| 922 | default: |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 923 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 924 | // Check for a simple Add/Sub or GEP of a loop invariant step. |
| 925 | case Instruction::Add: |
| 926 | case Instruction::Sub: { |
| 927 | Instruction *OInst = dyn_cast<Instruction>(IncV->getOperand(1)); |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 928 | if (!OInst || SE.DT.dominates(OInst, InsertPos)) |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 929 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 930 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 931 | } |
| 932 | case Instruction::BitCast: |
| 933 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
| 934 | case Instruction::GetElementPtr: |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 935 | for (auto I = IncV->op_begin() + 1, E = IncV->op_end(); I != E; ++I) { |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 936 | if (isa<Constant>(*I)) |
| 937 | continue; |
| 938 | if (Instruction *OInst = dyn_cast<Instruction>(*I)) { |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 939 | if (!SE.DT.dominates(OInst, InsertPos)) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 940 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 941 | } |
| 942 | if (allowScale) { |
| 943 | // allow any kind of GEP as long as it can be hoisted. |
| 944 | continue; |
| 945 | } |
| 946 | // This must be a pointer addition of constants (pretty), which is already |
| 947 | // handled, or some number of address-size elements (ugly). Ugly geps |
| 948 | // have 2 operands. i1* is used by the expander to represent an |
| 949 | // address-size element. |
| 950 | if (IncV->getNumOperands() != 2) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 951 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 952 | unsigned AS = cast<PointerType>(IncV->getType())->getAddressSpace(); |
| 953 | if (IncV->getType() != Type::getInt1PtrTy(SE.getContext(), AS) |
| 954 | && IncV->getType() != Type::getInt8PtrTy(SE.getContext(), AS)) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 955 | return nullptr; |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 956 | break; |
| 957 | } |
| 958 | return dyn_cast<Instruction>(IncV->getOperand(0)); |
| 959 | } |
| 960 | } |
| 961 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 962 | /// If the insert point of the current builder or any of the builders on the |
| 963 | /// stack of saved builders has 'I' as its insert point, update it to point to |
| 964 | /// the instruction after 'I'. This is intended to be used when the instruction |
| 965 | /// 'I' is being moved. If this fixup is not done and 'I' is moved to a |
| 966 | /// different block, the inconsistent insert point (with a mismatched |
| 967 | /// Instruction and Block) can lead to an instruction being inserted in a block |
| 968 | /// other than its parent. |
| 969 | void SCEVExpander::fixupInsertPoints(Instruction *I) { |
| 970 | BasicBlock::iterator It(*I); |
| 971 | BasicBlock::iterator NewInsertPt = std::next(It); |
| 972 | if (Builder.GetInsertPoint() == It) |
| 973 | Builder.SetInsertPoint(&*NewInsertPt); |
| 974 | for (auto *InsertPtGuard : InsertPointGuards) |
| 975 | if (InsertPtGuard->GetInsertPoint() == It) |
| 976 | InsertPtGuard->SetInsertPoint(NewInsertPt); |
| 977 | } |
| 978 | |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 979 | /// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make |
| 980 | /// it available to other uses in this loop. Recursively hoist any operands, |
| 981 | /// until we reach a value that dominates InsertPos. |
| 982 | bool SCEVExpander::hoistIVInc(Instruction *IncV, Instruction *InsertPos) { |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 983 | if (SE.DT.dominates(IncV, InsertPos)) |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 984 | return true; |
| 985 | |
| 986 | // InsertPos must itself dominate IncV so that IncV's new position satisfies |
| 987 | // its existing users. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 988 | if (isa<PHINode>(InsertPos) || |
| 989 | !SE.DT.dominates(InsertPos->getParent(), IncV->getParent())) |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 990 | return false; |
| 991 | |
Sanjoy Das | 51d40ae | 2015-12-08 00:13:17 +0000 | [diff] [blame] | 992 | if (!SE.LI.movementPreservesLCSSAForm(IncV, InsertPos)) |
| 993 | return false; |
| 994 | |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 995 | // Check that the chain of IV operands leading back to Phi can be hoisted. |
| 996 | SmallVector<Instruction*, 4> IVIncs; |
| 997 | for(;;) { |
| 998 | Instruction *Oper = getIVIncOperand(IncV, InsertPos, /*allowScale*/true); |
| 999 | if (!Oper) |
| 1000 | return false; |
| 1001 | // IncV is safe to hoist. |
| 1002 | IVIncs.push_back(IncV); |
| 1003 | IncV = Oper; |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1004 | if (SE.DT.dominates(IncV, InsertPos)) |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 1007 | for (auto I = IVIncs.rbegin(), E = IVIncs.rend(); I != E; ++I) { |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1008 | fixupInsertPoints(*I); |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1009 | (*I)->moveBefore(InsertPos); |
| 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1014 | /// Determine if this cyclic phi is in a form that would have been generated by |
| 1015 | /// LSR. We don't care if the phi was actually expanded in this pass, as long |
| 1016 | /// as it is in a low-cost form, for example, no implied multiplication. This |
| 1017 | /// should match any patterns generated by getAddRecExprPHILiterally and |
| 1018 | /// expandAddtoGEP. |
| 1019 | bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, |
Andrew Trick | 365c9f1 | 2011-10-15 06:19:55 +0000 | [diff] [blame] | 1020 | const Loop *L) { |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1021 | for(Instruction *IVOper = IncV; |
| 1022 | (IVOper = getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(), |
| 1023 | /*allowScale=*/false));) { |
| 1024 | if (IVOper == PN) |
| 1025 | return true; |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1026 | } |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1027 | return false; |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1030 | /// expandIVInc - Expand an IV increment at Builder's current InsertPos. |
| 1031 | /// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may |
| 1032 | /// need to materialize IV increments elsewhere to handle difficult situations. |
| 1033 | Value *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L, |
| 1034 | Type *ExpandTy, Type *IntTy, |
| 1035 | bool useSubtract) { |
| 1036 | Value *IncV; |
| 1037 | // If the PHI is a pointer, use a GEP, otherwise use an add or sub. |
| 1038 | if (ExpandTy->isPointerTy()) { |
| 1039 | PointerType *GEPPtrTy = cast<PointerType>(ExpandTy); |
| 1040 | // If the step isn't constant, don't use an implicitly scaled GEP, because |
| 1041 | // that would require a multiply inside the loop. |
| 1042 | if (!isa<ConstantInt>(StepV)) |
| 1043 | GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()), |
| 1044 | GEPPtrTy->getAddressSpace()); |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 1045 | IncV = expandAddToGEP(SE.getSCEV(StepV), GEPPtrTy, IntTy, PN); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1046 | if (IncV->getType() != PN->getType()) { |
| 1047 | IncV = Builder.CreateBitCast(IncV, PN->getType()); |
| 1048 | rememberInstruction(IncV); |
| 1049 | } |
| 1050 | } else { |
| 1051 | IncV = useSubtract ? |
| 1052 | Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") : |
| 1053 | Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next"); |
| 1054 | rememberInstruction(IncV); |
| 1055 | } |
| 1056 | return IncV; |
| 1057 | } |
| 1058 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1059 | /// Hoist the addrec instruction chain rooted in the loop phi above the |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1060 | /// position. This routine assumes that this is possible (has been checked). |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1061 | void SCEVExpander::hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist, |
| 1062 | Instruction *Pos, PHINode *LoopPhi) { |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1063 | do { |
| 1064 | if (DT->dominates(InstToHoist, Pos)) |
| 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. |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1068 | fixupInsertPoints(InstToHoist); |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1069 | InstToHoist->moveBefore(Pos); |
| 1070 | Pos = InstToHoist; |
| 1071 | InstToHoist = cast<Instruction>(InstToHoist->getOperand(0)); |
| 1072 | } while (InstToHoist != LoopPhi); |
| 1073 | } |
| 1074 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1075 | /// Check whether we can cheaply express the requested SCEV in terms of |
Benjamin Kramer | d3c712e | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1076 | /// the available PHI SCEV by truncation and/or inversion of the step. |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1077 | static bool canBeCheaplyTransformed(ScalarEvolution &SE, |
| 1078 | const SCEVAddRecExpr *Phi, |
| 1079 | const SCEVAddRecExpr *Requested, |
| 1080 | bool &InvertStep) { |
| 1081 | Type *PhiTy = SE.getEffectiveSCEVType(Phi->getType()); |
| 1082 | Type *RequestedTy = SE.getEffectiveSCEVType(Requested->getType()); |
| 1083 | |
| 1084 | if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth()) |
| 1085 | return false; |
| 1086 | |
| 1087 | // Try truncate it if necessary. |
| 1088 | Phi = dyn_cast<SCEVAddRecExpr>(SE.getTruncateOrNoop(Phi, RequestedTy)); |
| 1089 | if (!Phi) |
| 1090 | return false; |
| 1091 | |
| 1092 | // Check whether truncation will help. |
| 1093 | if (Phi == Requested) { |
| 1094 | InvertStep = false; |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | // Check whether inverting will help: {R,+,-1} == R - {0,+,1}. |
| 1099 | if (SE.getAddExpr(Requested->getStart(), |
| 1100 | SE.getNegativeSCEV(Requested)) == Phi) { |
| 1101 | InvertStep = true; |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Sanjoy Das | a0a0b40 | 2015-02-25 20:02:59 +0000 | [diff] [blame] | 1108 | static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) { |
| 1109 | if (!isa<IntegerType>(AR->getType())) |
| 1110 | return false; |
| 1111 | |
| 1112 | unsigned BitWidth = cast<IntegerType>(AR->getType())->getBitWidth(); |
| 1113 | Type *WideTy = IntegerType::get(AR->getType()->getContext(), BitWidth * 2); |
| 1114 | const SCEV *Step = AR->getStepRecurrence(SE); |
| 1115 | const SCEV *OpAfterExtend = SE.getAddExpr(SE.getSignExtendExpr(Step, WideTy), |
| 1116 | SE.getSignExtendExpr(AR, WideTy)); |
| 1117 | const SCEV *ExtendAfterOp = |
| 1118 | SE.getSignExtendExpr(SE.getAddExpr(AR, Step), WideTy); |
| 1119 | return ExtendAfterOp == OpAfterExtend; |
| 1120 | } |
| 1121 | |
| 1122 | static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) { |
| 1123 | if (!isa<IntegerType>(AR->getType())) |
| 1124 | return false; |
| 1125 | |
| 1126 | unsigned BitWidth = cast<IntegerType>(AR->getType())->getBitWidth(); |
| 1127 | Type *WideTy = IntegerType::get(AR->getType()->getContext(), BitWidth * 2); |
| 1128 | const SCEV *Step = AR->getStepRecurrence(SE); |
| 1129 | const SCEV *OpAfterExtend = SE.getAddExpr(SE.getZeroExtendExpr(Step, WideTy), |
| 1130 | SE.getZeroExtendExpr(AR, WideTy)); |
| 1131 | const SCEV *ExtendAfterOp = |
| 1132 | SE.getZeroExtendExpr(SE.getAddExpr(AR, Step), WideTy); |
| 1133 | return ExtendAfterOp == OpAfterExtend; |
| 1134 | } |
| 1135 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1136 | /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand |
| 1137 | /// the base addrec, which is the addrec without any non-loop-dominating |
| 1138 | /// values, and return the PHI. |
| 1139 | PHINode * |
| 1140 | SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, |
| 1141 | const Loop *L, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1142 | Type *ExpandTy, |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1143 | Type *IntTy, |
| 1144 | Type *&TruncTy, |
| 1145 | bool &InvertStep) { |
Benjamin Kramer | 93a896e | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 1146 | assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position"); |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 1147 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1148 | // Reuse a previously-inserted PHI, if present. |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1149 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 1150 | if (LatchBlock) { |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1151 | PHINode *AddRecPhiMatch = nullptr; |
| 1152 | Instruction *IncV = nullptr; |
| 1153 | TruncTy = nullptr; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1154 | InvertStep = false; |
| 1155 | |
| 1156 | // Only try partially matching scevs that need truncation and/or |
| 1157 | // step-inversion if we know this loop is outside the current loop. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1158 | bool TryNonMatchingSCEV = |
| 1159 | IVIncInsertLoop && |
| 1160 | SE.DT.properlyDominates(LatchBlock, IVIncInsertLoop->getHeader()); |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1161 | |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1162 | for (PHINode &PN : L->getHeader()->phis()) { |
| 1163 | if (!SE.isSCEVable(PN.getType())) |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1164 | continue; |
Dan Gohman | 22e6219 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 1165 | |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1166 | const SCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&PN)); |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1167 | if (!PhiSCEV) |
| 1168 | continue; |
Dan Gohman | 22e6219 | 2010-02-16 00:20:08 +0000 | [diff] [blame] | 1169 | |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1170 | bool IsMatchingSCEV = PhiSCEV == Normalized; |
| 1171 | // We only handle truncation and inversion of phi recurrences for the |
| 1172 | // expanded expression if the expanded expression's loop dominates the |
| 1173 | // loop we insert to. Check now, so we can bail out early. |
| 1174 | if (!IsMatchingSCEV && !TryNonMatchingSCEV) |
| 1175 | continue; |
| 1176 | |
Roman Lebedev | 998f860 | 2018-06-29 07:44:20 +0000 | [diff] [blame] | 1177 | // TODO: this possibly can be reworked to avoid this cast at all. |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1178 | Instruction *TempIncV = |
Roman Lebedev | 998f860 | 2018-06-29 07:44:20 +0000 | [diff] [blame] | 1179 | dyn_cast<Instruction>(PN.getIncomingValueForBlock(LatchBlock)); |
| 1180 | if (!TempIncV) |
| 1181 | continue; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Check whether we can reuse this PHI node. |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1184 | if (LSRMode) { |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1185 | if (!isExpandedAddRecExprPHI(&PN, TempIncV, L)) |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1186 | continue; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1187 | if (L == IVIncInsertLoop && !hoistIVInc(TempIncV, IVIncInsertPos)) |
| 1188 | continue; |
| 1189 | } else { |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1190 | if (!isNormalAddRecExprPHI(&PN, TempIncV, L)) |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1191 | continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1192 | } |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1193 | |
| 1194 | // Stop if we have found an exact match SCEV. |
| 1195 | if (IsMatchingSCEV) { |
| 1196 | IncV = TempIncV; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1197 | TruncTy = nullptr; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1198 | InvertStep = false; |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1199 | AddRecPhiMatch = &PN; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1200 | break; |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1201 | } |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Try whether the phi can be translated into the requested form |
| 1204 | // (truncated and/or offset by a constant). |
| 1205 | if ((!TruncTy || InvertStep) && |
| 1206 | canBeCheaplyTransformed(SE, PhiSCEV, Normalized, InvertStep)) { |
| 1207 | // Record the phi node. But don't stop we might find an exact match |
| 1208 | // later. |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1209 | AddRecPhiMatch = &PN; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1210 | IncV = TempIncV; |
| 1211 | TruncTy = SE.getEffectiveSCEVType(Normalized->getType()); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (AddRecPhiMatch) { |
| 1216 | // Potentially, move the increment. We have made sure in |
| 1217 | // isExpandedAddRecExprPHI or hoistIVInc that this is possible. |
| 1218 | if (L == IVIncInsertLoop) |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1219 | hoistBeforePos(&SE.DT, IncV, IVIncInsertPos, AddRecPhiMatch); |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1220 | |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1221 | // Ok, the add recurrence looks usable. |
| 1222 | // Remember this PHI, even in post-inc mode. |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1223 | InsertedValues.insert(AddRecPhiMatch); |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1224 | // Remember the increment. |
| 1225 | rememberInstruction(IncV); |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1226 | return AddRecPhiMatch; |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1229 | |
| 1230 | // Save the original insertion point so we can restore it when we're done. |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1231 | SCEVInsertPointGuard Guard(Builder, this); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1232 | |
Andrew Trick | ba3c0bc | 2011-12-20 01:42:24 +0000 | [diff] [blame] | 1233 | // Another AddRec may need to be recursively expanded below. For example, if |
| 1234 | // this AddRec is quadratic, the StepV may itself be an AddRec in this |
| 1235 | // loop. Remove this loop from the PostIncLoops set before expanding such |
| 1236 | // AddRecs. Otherwise, we cannot find a valid position for the step |
| 1237 | // (i.e. StepV can never dominate its loop header). Ideally, we could do |
| 1238 | // SavedIncLoops.swap(PostIncLoops), but we generally have a single element, |
| 1239 | // so it's not worth implementing SmallPtrSet::swap. |
| 1240 | PostIncLoopSet SavedPostIncLoops = PostIncLoops; |
| 1241 | PostIncLoops.clear(); |
| 1242 | |
Sanjoy Das | 7af3278 | 2016-12-11 19:02:21 +0000 | [diff] [blame] | 1243 | // Expand code for the start value into the loop preheader. |
| 1244 | assert(L->getLoopPreheader() && |
| 1245 | "Can't expand add recurrences without a loop preheader!"); |
| 1246 | Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, |
| 1247 | L->getLoopPreheader()->getTerminator()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1248 | |
Sanjoy Das | 7af3278 | 2016-12-11 19:02:21 +0000 | [diff] [blame] | 1249 | // StartV must have been be inserted into L's preheader to dominate the new |
| 1250 | // phi. |
Benjamin Kramer | 93a896e | 2011-07-16 22:26:27 +0000 | [diff] [blame] | 1251 | assert(!isa<Instruction>(StartV) || |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1252 | SE.DT.properlyDominates(cast<Instruction>(StartV)->getParent(), |
| 1253 | L->getHeader())); |
Andrew Trick | d152d03 | 2011-07-16 00:59:39 +0000 | [diff] [blame] | 1254 | |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1255 | // Expand code for the step value. Do this before creating the PHI so that PHI |
| 1256 | // reuse code doesn't see an incomplete PHI. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1257 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1258 | // If the stride is negative, insert a sub instead of an add for the increment |
| 1259 | // (unless it's a constant, because subtracts of constants are canonicalized |
| 1260 | // to adds). |
Andrew Trick | f8fd841 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 1261 | bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1262 | if (useSubtract) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1263 | Step = SE.getNegativeSCEV(Step); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1264 | // Expand the step somewhere that dominates the loop header. |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1265 | Value *StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1266 | |
Sanjoy Das | e836f05 | 2015-02-26 19:51:35 +0000 | [diff] [blame] | 1267 | // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if |
| 1268 | // we actually do emit an addition. It does not apply if we emit a |
| 1269 | // subtraction. |
| 1270 | bool IncrementIsNUW = !useSubtract && IsIncrementNUW(SE, Normalized); |
| 1271 | bool IncrementIsNSW = !useSubtract && IsIncrementNSW(SE, Normalized); |
| 1272 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1273 | // Create the PHI. |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1274 | BasicBlock *Header = L->getHeader(); |
| 1275 | Builder.SetInsertPoint(Header, Header->begin()); |
| 1276 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Andrew Trick | 5e7645b | 2011-06-28 05:07:32 +0000 | [diff] [blame] | 1277 | PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE), |
Andrew Trick | dc8e546 | 2011-06-28 05:41:52 +0000 | [diff] [blame] | 1278 | Twine(IVName) + ".iv"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1279 | rememberInstruction(PN); |
| 1280 | |
| 1281 | // Create the step instructions and populate the PHI. |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1282 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1283 | BasicBlock *Pred = *HPI; |
| 1284 | |
| 1285 | // Add a start value. |
| 1286 | if (!L->contains(Pred)) { |
| 1287 | PN->addIncoming(StartV, Pred); |
| 1288 | continue; |
| 1289 | } |
| 1290 | |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1291 | // Create a step value and add it to the PHI. |
| 1292 | // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the |
| 1293 | // instructions at IVIncInsertPos. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1294 | Instruction *InsertPos = L == IVIncInsertLoop ? |
| 1295 | IVIncInsertPos : Pred->getTerminator(); |
Devang Patel | c5ecbdc | 2011-07-05 21:48:22 +0000 | [diff] [blame] | 1296 | Builder.SetInsertPoint(InsertPos); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1297 | Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); |
Sanjoy Das | a0a0b40 | 2015-02-25 20:02:59 +0000 | [diff] [blame] | 1298 | |
Andrew Trick | 409443b | 2013-07-14 02:50:07 +0000 | [diff] [blame] | 1299 | if (isa<OverflowingBinaryOperator>(IncV)) { |
Sanjoy Das | a0a0b40 | 2015-02-25 20:02:59 +0000 | [diff] [blame] | 1300 | if (IncrementIsNUW) |
Andrew Trick | 409443b | 2013-07-14 02:50:07 +0000 | [diff] [blame] | 1301 | cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap(); |
Sanjoy Das | a0a0b40 | 2015-02-25 20:02:59 +0000 | [diff] [blame] | 1302 | if (IncrementIsNSW) |
Andrew Trick | 409443b | 2013-07-14 02:50:07 +0000 | [diff] [blame] | 1303 | cast<BinaryOperator>(IncV)->setHasNoSignedWrap(); |
| 1304 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1305 | PN->addIncoming(IncV, Pred); |
| 1306 | } |
| 1307 | |
Andrew Trick | ba3c0bc | 2011-12-20 01:42:24 +0000 | [diff] [blame] | 1308 | // After expanding subexpressions, restore the PostIncLoops set so the caller |
| 1309 | // can ensure that IVIncrement dominates the current uses. |
| 1310 | PostIncLoops = SavedPostIncLoops; |
| 1311 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1312 | // Remember this PHI, even in post-inc mode. |
| 1313 | InsertedValues.insert(PN); |
| 1314 | |
| 1315 | return PN; |
| 1316 | } |
| 1317 | |
| 1318 | Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1319 | Type *STy = S->getType(); |
| 1320 | Type *IntTy = SE.getEffectiveSCEVType(STy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1321 | const Loop *L = S->getLoop(); |
| 1322 | |
| 1323 | // Determine a normalized form of this expression, which is the expression |
| 1324 | // before any post-inc adjustment is made. |
| 1325 | const SCEVAddRecExpr *Normalized = S; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1326 | if (PostIncLoops.count(L)) { |
| 1327 | PostIncLoopSet Loops; |
| 1328 | Loops.insert(L); |
Sanjoy Das | fe33eb8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 1329 | Normalized = cast<SCEVAddRecExpr>(normalizeForPostIncUse(S, Loops, SE)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | // Strip off any non-loop-dominating component from the addrec start. |
| 1333 | const SCEV *Start = Normalized->getStart(); |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1334 | const SCEV *PostLoopOffset = nullptr; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1335 | if (!SE.properlyDominates(Start, L->getHeader())) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1336 | PostLoopOffset = Start; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1337 | Start = SE.getConstant(Normalized->getType(), 0); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1338 | Normalized = cast<SCEVAddRecExpr>( |
| 1339 | SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE), |
| 1340 | Normalized->getLoop(), |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1341 | Normalized->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | // Strip off any non-loop-dominating component from the addrec step. |
| 1345 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1346 | const SCEV *PostLoopScale = nullptr; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 1347 | if (!SE.dominates(Step, L->getHeader())) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1348 | PostLoopScale = Step; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1349 | Step = SE.getConstant(Normalized->getType(), 1); |
Keno Fischer | c8ce9e5 | 2016-07-13 01:28:12 +0000 | [diff] [blame] | 1350 | if (!Start->isZero()) { |
| 1351 | // The normalization below assumes that Start is constant zero, so if |
| 1352 | // it isn't re-associate Start to PostLoopOffset. |
| 1353 | assert(!PostLoopOffset && "Start not-null but PostLoopOffset set?"); |
| 1354 | PostLoopOffset = Start; |
| 1355 | Start = SE.getConstant(Normalized->getType(), 0); |
| 1356 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1357 | Normalized = |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1358 | cast<SCEVAddRecExpr>(SE.getAddRecExpr( |
| 1359 | Start, Step, Normalized->getLoop(), |
| 1360 | Normalized->getNoWrapFlags(SCEV::FlagNW))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | // Expand the core addrec. If we need post-loop scaling, force it to |
| 1364 | // expand to an integer type to avoid the need for additional casting. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1365 | Type *ExpandTy = PostLoopScale ? IntTy : STy; |
Keno Fischer | 65ac22c | 2017-05-27 03:22:55 +0000 | [diff] [blame] | 1366 | // We can't use a pointer type for the addrec if the pointer type is |
| 1367 | // non-integral. |
| 1368 | Type *AddRecPHIExpandTy = |
| 1369 | DL.isNonIntegralPointerType(STy) ? Normalized->getType() : ExpandTy; |
| 1370 | |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1371 | // In some cases, we decide to reuse an existing phi node but need to truncate |
| 1372 | // it and/or invert the step. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1373 | Type *TruncTy = nullptr; |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1374 | bool InvertStep = false; |
Keno Fischer | 65ac22c | 2017-05-27 03:22:55 +0000 | [diff] [blame] | 1375 | PHINode *PN = getAddRecExprPHILiterally(Normalized, L, AddRecPHIExpandTy, |
| 1376 | IntTy, TruncTy, InvertStep); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1377 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1378 | // Accommodate post-inc mode, if necessary. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1379 | Value *Result; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1380 | if (!PostIncLoops.count(L)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1381 | Result = PN; |
| 1382 | else { |
| 1383 | // In PostInc mode, use the post-incremented value. |
| 1384 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 1385 | assert(LatchBlock && "PostInc mode requires a unique loop latch!"); |
| 1386 | Result = PN->getIncomingValueForBlock(LatchBlock); |
Andrew Trick | 48ba0e4 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1387 | |
| 1388 | // For an expansion to use the postinc form, the client must call |
| 1389 | // expandCodeFor with an InsertPoint that is either outside the PostIncLoop |
| 1390 | // or dominated by IVIncInsertPos. |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1391 | if (isa<Instruction>(Result) && |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1392 | !SE.DT.dominates(cast<Instruction>(Result), |
| 1393 | &*Builder.GetInsertPoint())) { |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1394 | // The induction variable's postinc expansion does not dominate this use. |
| 1395 | // IVUsers tries to prevent this case, so it is rare. However, it can |
| 1396 | // happen when an IVUser outside the loop is not dominated by the latch |
| 1397 | // block. Adjusting IVIncInsertPos before expansion begins cannot handle |
Vedant Kumar | d924442 | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 1398 | // all cases. Consider a phi outside whose operand is replaced during |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1399 | // expansion with the value of the postinc user. Without fundamentally |
| 1400 | // changing the way postinc users are tracked, the only remedy is |
| 1401 | // inserting an extra IV increment. StepV might fold into PostLoopOffset, |
| 1402 | // but hopefully expandCodeFor handles that. |
| 1403 | bool useSubtract = |
Andrew Trick | f8fd841 | 2012-01-07 00:27:31 +0000 | [diff] [blame] | 1404 | !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1405 | if (useSubtract) |
| 1406 | Step = SE.getNegativeSCEV(Step); |
Benjamin Kramer | d427882 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1407 | Value *StepV; |
| 1408 | { |
| 1409 | // Expand the step somewhere that dominates the loop header. |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1410 | SCEVInsertPointGuard Guard(Builder, this); |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1411 | StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front()); |
Benjamin Kramer | d427882 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 1412 | } |
Andrew Trick | 553fe05 | 2011-11-30 06:07:54 +0000 | [diff] [blame] | 1413 | Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); |
| 1414 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1417 | // We have decided to reuse an induction variable of a dominating loop. Apply |
Vedant Kumar | d924442 | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 1418 | // truncation and/or inversion of the step. |
Arnold Schwaighofer | 2ced338 | 2014-02-16 15:49:50 +0000 | [diff] [blame] | 1419 | if (TruncTy) { |
| 1420 | Type *ResTy = Result->getType(); |
| 1421 | // Normalize the result type. |
| 1422 | if (ResTy != SE.getEffectiveSCEVType(ResTy)) |
| 1423 | Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy)); |
| 1424 | // Truncate the result. |
| 1425 | if (TruncTy != Result->getType()) { |
| 1426 | Result = Builder.CreateTrunc(Result, TruncTy); |
| 1427 | rememberInstruction(Result); |
| 1428 | } |
| 1429 | // Invert the result. |
| 1430 | if (InvertStep) { |
| 1431 | Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy), |
| 1432 | Result); |
| 1433 | rememberInstruction(Result); |
| 1434 | } |
| 1435 | } |
| 1436 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1437 | // Re-apply any non-loop-dominating scale. |
| 1438 | if (PostLoopScale) { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1439 | assert(S->isAffine() && "Can't linearly scale non-affine recurrences."); |
Dan Gohman | 0a799ab | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1440 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1441 | Result = Builder.CreateMul(Result, |
| 1442 | expandCodeFor(PostLoopScale, IntTy)); |
| 1443 | rememberInstruction(Result); |
| 1444 | } |
| 1445 | |
| 1446 | // Re-apply any non-loop-dominating offset. |
| 1447 | if (PostLoopOffset) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1448 | if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) { |
Keno Fischer | 65ac22c | 2017-05-27 03:22:55 +0000 | [diff] [blame] | 1449 | if (Result->getType()->isIntegerTy()) { |
| 1450 | Value *Base = expandCodeFor(PostLoopOffset, ExpandTy); |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 1451 | Result = expandAddToGEP(SE.getUnknown(Result), PTy, IntTy, Base); |
Keno Fischer | 65ac22c | 2017-05-27 03:22:55 +0000 | [diff] [blame] | 1452 | } else { |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 1453 | Result = expandAddToGEP(PostLoopOffset, PTy, IntTy, Result); |
Keno Fischer | 65ac22c | 2017-05-27 03:22:55 +0000 | [diff] [blame] | 1454 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1455 | } else { |
Dan Gohman | 0a799ab | 2010-02-12 20:39:25 +0000 | [diff] [blame] | 1456 | Result = InsertNoopCastOfTo(Result, IntTy); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1457 | Result = Builder.CreateAdd(Result, |
| 1458 | expandCodeFor(PostLoopOffset, IntTy)); |
| 1459 | rememberInstruction(Result); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | return Result; |
| 1464 | } |
| 1465 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1466 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1467 | if (!CanonicalMode) return expandAddRecExprLiterally(S); |
| 1468 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1469 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1470 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1471 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1472 | // First check for an existing canonical IV in a suitable type. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1473 | PHINode *CanonicalIV = nullptr; |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1474 | if (PHINode *PN = L->getCanonicalInductionVariable()) |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1475 | if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1476 | CanonicalIV = PN; |
| 1477 | |
| 1478 | // Rewrite an AddRec in terms of the canonical induction variable, if |
| 1479 | // its type is more narrow. |
| 1480 | if (CanonicalIV && |
| 1481 | SE.getTypeSizeInBits(CanonicalIV->getType()) > |
| 1482 | SE.getTypeSizeInBits(Ty)) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1483 | SmallVector<const SCEV *, 4> NewOps(S->getNumOperands()); |
| 1484 | for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i) |
| 1485 | NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType()); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1486 | Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(), |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1487 | S->getNoWrapFlags(SCEV::FlagNW))); |
Florian Hahn | 26ac94d | 2018-06-25 20:55:26 +0000 | [diff] [blame] | 1488 | BasicBlock::iterator NewInsertPt = |
| 1489 | findInsertPointAfter(cast<Instruction>(V), Builder.GetInsertBlock()); |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1490 | V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr, |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1491 | &*NewInsertPt); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1492 | return V; |
| 1493 | } |
| 1494 | |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1495 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1496 | if (!S->getStart()->isZero()) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1497 | SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end()); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1498 | NewOps[0] = SE.getConstant(Ty, 0); |
Andrew Trick | 6f71dd7 | 2013-07-14 03:10:08 +0000 | [diff] [blame] | 1499 | const SCEV *Rest = SE.getAddRecExpr(NewOps, L, |
| 1500 | S->getNoWrapFlags(SCEV::FlagNW)); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 1503 | // comments on expandAddToGEP for details. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1504 | const SCEV *Base = S->getStart(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1505 | // Dig into the expression to find the pointer base for a GEP. |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 1506 | const SCEV *ExposedRest = Rest; |
| 1507 | ExposePointerBase(Base, ExposedRest, SE); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1508 | // If we found a pointer, expand the AddRec with a GEP. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1509 | if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1510 | // Make sure the Base isn't something exotic, such as a multiplied |
| 1511 | // or divided pointer value. In those cases, the result type isn't |
| 1512 | // actually a pointer type. |
| 1513 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 1514 | Value *StartV = expand(Base); |
| 1515 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
Keno Fischer | db695a1 | 2018-07-26 21:55:03 +0000 | [diff] [blame] | 1516 | return expandAddToGEP(ExposedRest, PTy, Ty, StartV); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1520 | // Just do a normal add. Pre-expand the operands to suppress folding. |
Patrik Hagglund | 7293ea5 | 2016-06-20 10:19:04 +0000 | [diff] [blame] | 1521 | // |
| 1522 | // The LHS and RHS values are factored out of the expand call to make the |
| 1523 | // output independent of the argument evaluation order. |
| 1524 | const SCEV *AddExprLHS = SE.getUnknown(expand(S->getStart())); |
| 1525 | const SCEV *AddExprRHS = SE.getUnknown(expand(Rest)); |
| 1526 | return expand(SE.getAddExpr(AddExprLHS, AddExprRHS)); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1529 | // If we don't yet have a canonical IV, create one. |
| 1530 | if (!CanonicalIV) { |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1531 | // Create and insert the PHI node for the induction variable in the |
| 1532 | // specified loop. |
| 1533 | BasicBlock *Header = L->getHeader(); |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1534 | pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1535 | CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar", |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1536 | &Header->front()); |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1537 | rememberInstruction(CanonicalIV); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1538 | |
Hal Finkel | 19046ec | 2013-08-18 00:16:23 +0000 | [diff] [blame] | 1539 | SmallSet<BasicBlock *, 4> PredSeen; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1540 | Constant *One = ConstantInt::get(Ty, 1); |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1541 | for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1542 | BasicBlock *HP = *HPI; |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1543 | if (!PredSeen.insert(HP).second) { |
Hal Finkel | 50c05c9 | 2014-07-31 19:13:38 +0000 | [diff] [blame] | 1544 | // There must be an incoming value for each predecessor, even the |
| 1545 | // duplicates! |
| 1546 | CanonicalIV->addIncoming(CanonicalIV->getIncomingValueForBlock(HP), HP); |
Hal Finkel | 19046ec | 2013-08-18 00:16:23 +0000 | [diff] [blame] | 1547 | continue; |
Hal Finkel | 50c05c9 | 2014-07-31 19:13:38 +0000 | [diff] [blame] | 1548 | } |
Hal Finkel | 19046ec | 2013-08-18 00:16:23 +0000 | [diff] [blame] | 1549 | |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1550 | if (L->contains(HP)) { |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 1551 | // Insert a unit add instruction right before the terminator |
| 1552 | // corresponding to the back-edge. |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1553 | Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One, |
| 1554 | "indvar.next", |
| 1555 | HP->getTerminator()); |
Devang Patel | df3ad66 | 2011-06-22 20:56:56 +0000 | [diff] [blame] | 1556 | Add->setDebugLoc(HP->getTerminator()->getDebugLoc()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1557 | rememberInstruction(Add); |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1558 | CanonicalIV->addIncoming(Add, HP); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1559 | } else { |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1560 | CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 1561 | } |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1562 | } |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1565 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 1566 | if (S->isAffine() && S->getOperand(1)->isOne()) { |
| 1567 | assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && |
| 1568 | "IVs with types different from the canonical IV should " |
| 1569 | "already have been handled!"); |
| 1570 | return CanonicalIV; |
| 1571 | } |
| 1572 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1573 | // {0,+,F} --> {0,+,1} * F |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 1575 | // 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] | 1576 | if (S->isAffine()) // {0,+,F} --> i*F |
| 1577 | return |
| 1578 | expand(SE.getTruncateOrNoop( |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1579 | SE.getMulExpr(SE.getUnknown(CanonicalIV), |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1580 | SE.getNoopOrAnyExtend(S->getOperand(1), |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1581 | CanonicalIV->getType())), |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1582 | Ty)); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1583 | |
| 1584 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 1585 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 1586 | // simplify the expression without having to build a bunch of special code |
| 1587 | // into this folder. |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1588 | const SCEV *IH = SE.getUnknown(CanonicalIV); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1589 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1590 | // 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] | 1591 | const SCEV *NewS = S; |
Dan Gohman | 6ebfd72 | 2010-07-26 18:28:14 +0000 | [diff] [blame] | 1592 | const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType()); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1593 | if (isa<SCEVAddRecExpr>(Ext)) |
| 1594 | NewS = Ext; |
| 1595 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1596 | const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1597 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1598 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1599 | // Truncate the result down to the original type, if needed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1600 | const SCEV *T = SE.getTruncateOrNoop(V, Ty); |
Dan Gohman | 469f3cd | 2009-06-22 22:08:45 +0000 | [diff] [blame] | 1601 | return expand(T); |
Nate Begeman | 36f891bd | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1602 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1603 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1604 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1605 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1606 | Value *V = expandCodeFor(S->getOperand(), |
| 1607 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1608 | Value *I = Builder.CreateTrunc(V, Ty); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1609 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1610 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1613 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1614 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1615 | Value *V = expandCodeFor(S->getOperand(), |
| 1616 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1617 | Value *I = Builder.CreateZExt(V, Ty); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1618 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1619 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1622 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1623 | Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1624 | Value *V = expandCodeFor(S->getOperand(), |
| 1625 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1626 | Value *I = Builder.CreateSExt(V, Ty); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1627 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1628 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1631 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1632 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1633 | Type *Ty = LHS->getType(); |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1634 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1635 | // In the case of mixed integer and pointer types, do the |
| 1636 | // rest of the comparisons as integer. |
| 1637 | if (S->getOperand(i)->getType() != Ty) { |
| 1638 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1639 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1640 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1641 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1642 | Value *ICmp = Builder.CreateICmpSGT(LHS, RHS); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1643 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1644 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1645 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1646 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1647 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1648 | // In the case of mixed integer and pointer types, cast the |
| 1649 | // final result back to the pointer type. |
| 1650 | if (LHS->getType() != S->getType()) |
| 1651 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1652 | return LHS; |
| 1653 | } |
| 1654 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1655 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1656 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1657 | Type *Ty = LHS->getType(); |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1658 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 1659 | // In the case of mixed integer and pointer types, do the |
| 1660 | // rest of the comparisons as integer. |
| 1661 | if (S->getOperand(i)->getType() != Ty) { |
| 1662 | Ty = SE.getEffectiveSCEVType(Ty); |
| 1663 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 1664 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 1665 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1666 | Value *ICmp = Builder.CreateICmpUGT(LHS, RHS); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1667 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1668 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1669 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 1670 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1671 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 1672 | // In the case of mixed integer and pointer types, cast the |
| 1673 | // final result back to the pointer type. |
| 1674 | if (LHS->getType() != S->getType()) |
| 1675 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1676 | return LHS; |
| 1677 | } |
| 1678 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1679 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, |
Andrew Trick | b5c26ef | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1680 | Instruction *IP) { |
Geoff Berry | 510ec20 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 1681 | setInsertPoint(IP); |
Dan Gohman | 6c7ed6b | 2010-03-19 21:51:03 +0000 | [diff] [blame] | 1682 | return expandCodeFor(SH, Ty); |
| 1683 | } |
| 1684 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1685 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1686 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1687 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 1688 | if (Ty) { |
| 1689 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 1690 | "non-trivial casts should be done with the SCEVs directly!"); |
| 1691 | V = InsertNoopCastOfTo(V, Ty); |
| 1692 | } |
| 1693 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1696 | ScalarEvolution::ValueOffsetPair |
| 1697 | SCEVExpander::FindValueInExprValueMap(const SCEV *S, |
| 1698 | const Instruction *InsertPt) { |
| 1699 | SetVector<ScalarEvolution::ValueOffsetPair> *Set = SE.getSCEVValues(S); |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1700 | // If the expansion is not in CanonicalMode, and the SCEV contains any |
| 1701 | // sub scAddRecExpr type SCEV, it is required to expand the SCEV literally. |
| 1702 | if (CanonicalMode || !SE.containsAddRecurrence(S)) { |
| 1703 | // If S is scConstant, it may be worse to reuse an existing Value. |
| 1704 | if (S->getSCEVType() != scConstant && Set) { |
| 1705 | // Choose a Value from the set which dominates the insertPt. |
| 1706 | // insertPt should be inside the Value's parent loop so as not to break |
| 1707 | // the LCSSA form. |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1708 | for (auto const &VOPair : *Set) { |
| 1709 | Value *V = VOPair.first; |
| 1710 | ConstantInt *Offset = VOPair.second; |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1711 | Instruction *EntInst = nullptr; |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1712 | if (V && isa<Instruction>(V) && (EntInst = cast<Instruction>(V)) && |
| 1713 | S->getType() == V->getType() && |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1714 | EntInst->getFunction() == InsertPt->getFunction() && |
| 1715 | SE.DT.dominates(EntInst, InsertPt) && |
| 1716 | (SE.LI.getLoopFor(EntInst->getParent()) == nullptr || |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1717 | SE.LI.getLoopFor(EntInst->getParent())->contains(InsertPt))) |
| 1718 | return {V, Offset}; |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | } |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1722 | return {nullptr, nullptr}; |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Wei Mi | eafb39b | 2016-02-04 01:27:38 +0000 | [diff] [blame] | 1725 | // The expansion of SCEV will either reuse a previous Value in ExprValueMap, |
| 1726 | // or expand the SCEV literally. Specifically, if the expansion is in LSRMode, |
| 1727 | // and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded |
| 1728 | // literally, to prevent LSR's transformed SCEV from being reverted. Otherwise, |
| 1729 | // the expansion will try to reuse Value from ExprValueMap, and only when it |
| 1730 | // fails, expand the SCEV literally. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1731 | Value *SCEVExpander::expand(const SCEV *S) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1732 | // Compute an insertion point for this SCEV object. Hoist the instructions |
| 1733 | // as far out in the loop nest as possible. |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1734 | Instruction *InsertPt = &*Builder.GetInsertPoint(); |
David L. Jones | ad6bed6 | 2018-12-05 23:13:50 +0000 | [diff] [blame] | 1735 | for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());; |
| 1736 | L = L->getParentLoop()) |
| 1737 | if (SE.isLoopInvariant(S, L)) { |
| 1738 | if (!L) break; |
| 1739 | if (BasicBlock *Preheader = L->getLoopPreheader()) |
| 1740 | InsertPt = Preheader->getTerminator(); |
| 1741 | else { |
| 1742 | // LSR sets the insertion point for AddRec start/step values to the |
| 1743 | // block start to simplify value reuse, even though it's an invalid |
| 1744 | // position. SCEVExpander must correct for this in all cases. |
| 1745 | InsertPt = &*L->getHeader()->getFirstInsertionPt(); |
Andrew Trick | 0f8cd56 | 2012-01-02 21:25:10 +0000 | [diff] [blame] | 1746 | } |
David L. Jones | ad6bed6 | 2018-12-05 23:13:50 +0000 | [diff] [blame] | 1747 | } else { |
| 1748 | // We can move insertion point only if there is no div or rem operations |
| 1749 | // otherwise we are risky to move it over the check for zero denominator. |
| 1750 | auto SafeToHoist = [](const SCEV *S) { |
| 1751 | return !SCEVExprContains(S, [](const SCEV *S) { |
| 1752 | if (const auto *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 1753 | if (const auto *SC = dyn_cast<SCEVConstant>(D->getRHS())) |
| 1754 | // Division by non-zero constants can be hoisted. |
| 1755 | return SC->getValue()->isZero(); |
| 1756 | // All other divisions should not be moved as they may be |
| 1757 | // divisions by zero and should be kept within the |
| 1758 | // conditions of the surrounding loops that guard their |
| 1759 | // execution (see PR35406). |
| 1760 | return true; |
| 1761 | } |
| 1762 | return false; |
| 1763 | }); |
| 1764 | }; |
| 1765 | // If the SCEV is computable at this level, insert it into the header |
| 1766 | // after the PHIs (and after any other instructions that we've inserted |
| 1767 | // there) so that it is guaranteed to dominate any user inside the loop. |
| 1768 | if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L) && |
| 1769 | SafeToHoist(S)) |
| 1770 | InsertPt = &*L->getHeader()->getFirstInsertionPt(); |
| 1771 | while (InsertPt->getIterator() != Builder.GetInsertPoint() && |
| 1772 | (isInsertedInstruction(InsertPt) || |
| 1773 | isa<DbgInfoIntrinsic>(InsertPt))) { |
| 1774 | InsertPt = &*std::next(InsertPt->getIterator()); |
| 1775 | } |
| 1776 | break; |
Reid Kleckner | 5697690 | 2016-12-12 18:52:32 +0000 | [diff] [blame] | 1777 | } |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1778 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1779 | // Check to see if we already expanded this here. |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 1780 | auto I = InsertedExpressions.find(std::make_pair(S, InsertPt)); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1781 | if (I != InsertedExpressions.end()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1782 | return I->second; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1783 | |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1784 | SCEVInsertPointGuard Guard(Builder, this); |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1785 | Builder.SetInsertPoint(InsertPt); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Expand the expression into instructions. |
Wei Mi | 8b93225 | 2016-08-09 20:37:50 +0000 | [diff] [blame] | 1788 | ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, InsertPt); |
| 1789 | Value *V = VO.first; |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 1790 | |
Wei Mi | eafb39b | 2016-02-04 01:27:38 +0000 | [diff] [blame] | 1791 | if (!V) |
| 1792 | V = visit(S); |
Wei Mi | 81a2b89 | 2016-09-14 04:39:50 +0000 | [diff] [blame] | 1793 | else if (VO.second) { |
| 1794 | if (PointerType *Vty = dyn_cast<PointerType>(V->getType())) { |
| 1795 | Type *Ety = Vty->getPointerElementType(); |
| 1796 | int64_t Offset = VO.second->getSExtValue(); |
| 1797 | int64_t ESize = SE.getTypeSizeInBits(Ety); |
| 1798 | if ((Offset * 8) % ESize == 0) { |
| 1799 | ConstantInt *Idx = |
| 1800 | ConstantInt::getSigned(VO.second->getType(), -(Offset * 8) / ESize); |
| 1801 | V = Builder.CreateGEP(Ety, V, Idx, "scevgep"); |
| 1802 | } else { |
| 1803 | ConstantInt *Idx = |
| 1804 | ConstantInt::getSigned(VO.second->getType(), -Offset); |
| 1805 | unsigned AS = Vty->getAddressSpace(); |
| 1806 | V = Builder.CreateBitCast(V, Type::getInt8PtrTy(SE.getContext(), AS)); |
| 1807 | V = Builder.CreateGEP(Type::getInt8Ty(SE.getContext()), V, Idx, |
| 1808 | "uglygep"); |
| 1809 | V = Builder.CreateBitCast(V, Vty); |
| 1810 | } |
| 1811 | } else { |
| 1812 | V = Builder.CreateSub(V, VO.second); |
| 1813 | } |
| 1814 | } |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1815 | // Remember the expanded value for this SCEV at this location. |
Andrew Trick | 48ba0e4 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1816 | // |
| 1817 | // This is independent of PostIncLoops. The mapped value simply materializes |
| 1818 | // the expression at this insertion point. If the mapped value happened to be |
Alp Toker | 087ab61 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 1819 | // a postinc expansion, it could be reused by a non-postinc user, but only if |
Andrew Trick | 48ba0e4 | 2011-10-13 21:55:29 +0000 | [diff] [blame] | 1820 | // its insertion point was already at the head of the loop. |
| 1821 | InsertedExpressions[std::make_pair(S, InsertPt)] = V; |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1822 | return V; |
| 1823 | } |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1824 | |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1825 | void SCEVExpander::rememberInstruction(Value *I) { |
Dan Gohman | 25fcaff | 2010-06-05 00:33:07 +0000 | [diff] [blame] | 1826 | if (!PostIncLoops.empty()) |
| 1827 | InsertedPostIncValues.insert(I); |
| 1828 | else |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1829 | InsertedValues.insert(I); |
Dan Gohman | 1d826a7 | 2010-02-14 03:12:47 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1832 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 1833 | /// canonical induction variable of the specified type for the specified |
| 1834 | /// loop (inserting one if there is none). A canonical induction variable |
| 1835 | /// starts at zero and steps by one on each iteration. |
Dan Gohman | 7c58dbd | 2010-07-20 16:44:52 +0000 | [diff] [blame] | 1836 | PHINode * |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1837 | SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1838 | Type *Ty) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1839 | assert(Ty->isIntegerTy() && "Can only insert integer induction variables!"); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1840 | |
| 1841 | // Build a SCEV for {0,+,1}<L>. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1842 | // Conservatively use FlagAnyWrap for now. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1843 | const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1844 | SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1845 | |
| 1846 | // Emit code for it. |
Geoff Berry | e81e803 | 2016-06-01 20:03:09 +0000 | [diff] [blame] | 1847 | SCEVInsertPointGuard Guard(Builder, this); |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1848 | PHINode *V = |
| 1849 | cast<PHINode>(expandCodeFor(H, nullptr, &L->getHeader()->front())); |
Dan Gohman | 133e295 | 2010-07-20 16:46:58 +0000 | [diff] [blame] | 1850 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1851 | return V; |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1852 | } |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1853 | |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1854 | /// replaceCongruentIVs - Check for congruent phis in this loop header and |
| 1855 | /// replace them with their most canonical representative. Return the number of |
| 1856 | /// phis eliminated. |
| 1857 | /// |
| 1858 | /// This does not depend on any SCEVExpander state but should be used in |
| 1859 | /// the same context that SCEVExpander is used. |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 1860 | unsigned |
| 1861 | SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT, |
| 1862 | SmallVectorImpl<WeakTrackingVH> &DeadInsts, |
| 1863 | const TargetTransformInfo *TTI) { |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1864 | // Find integer phis in order of increasing width. |
| 1865 | SmallVector<PHINode*, 8> Phis; |
Benjamin Kramer | 66f3fb9 | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1866 | for (PHINode &PN : L->getHeader()->phis()) |
| 1867 | Phis.push_back(&PN); |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 1868 | |
Chandler Carruth | e4ba75f | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1869 | if (TTI) |
Fangrui Song | 3b35e17 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1870 | llvm::sort(Phis, [](Value *LHS, Value *RHS) { |
Benjamin Kramer | e1362f1 | 2014-03-07 21:35:39 +0000 | [diff] [blame] | 1871 | // Put pointers at the back and make sure pointer < pointer = false. |
| 1872 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) |
| 1873 | return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy(); |
| 1874 | return RHS->getType()->getPrimitiveSizeInBits() < |
| 1875 | LHS->getType()->getPrimitiveSizeInBits(); |
| 1876 | }); |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1877 | |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1878 | unsigned NumElim = 0; |
| 1879 | DenseMap<const SCEV *, PHINode *> ExprToIVMap; |
Eric Christopher | 933d2bd | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 1880 | // Process phis from wide to narrow. Map wide phis to their truncation |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1881 | // so narrow phis can reuse them. |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 1882 | for (PHINode *Phi : Phis) { |
Silviu Baranga | 94652ab | 2015-11-03 16:27:04 +0000 | [diff] [blame] | 1883 | auto SimplifyPHINode = [&](PHINode *PN) -> Value * { |
Daniel Berlin | e2c5126 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 1884 | if (Value *V = SimplifyInstruction(PN, {DL, &SE.TLI, &SE.DT, &SE.AC})) |
Silviu Baranga | 94652ab | 2015-11-03 16:27:04 +0000 | [diff] [blame] | 1885 | return V; |
| 1886 | if (!SE.isSCEVable(PN->getType())) |
| 1887 | return nullptr; |
| 1888 | auto *Const = dyn_cast<SCEVConstant>(SE.getSCEV(PN)); |
| 1889 | if (!Const) |
| 1890 | return nullptr; |
| 1891 | return Const->getValue(); |
| 1892 | }; |
| 1893 | |
Benjamin Kramer | 239fd44 | 2012-10-19 16:37:30 +0000 | [diff] [blame] | 1894 | // Fold constant phis. They may be congruent to other constant phis and |
| 1895 | // would confuse the logic below that expects proper IVs. |
Silviu Baranga | 94652ab | 2015-11-03 16:27:04 +0000 | [diff] [blame] | 1896 | if (Value *V = SimplifyPHINode(Phi)) { |
| 1897 | if (V->getType() != Phi->getType()) |
| 1898 | continue; |
Benjamin Kramer | 239fd44 | 2012-10-19 16:37:30 +0000 | [diff] [blame] | 1899 | Phi->replaceAllUsesWith(V); |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 1900 | DeadInsts.emplace_back(Phi); |
Benjamin Kramer | 239fd44 | 2012-10-19 16:37:30 +0000 | [diff] [blame] | 1901 | ++NumElim; |
| 1902 | DEBUG_WITH_TYPE(DebugType, dbgs() |
| 1903 | << "INDVARS: Eliminated constant iv: " << *Phi << '\n'); |
| 1904 | continue; |
| 1905 | } |
| 1906 | |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1907 | if (!SE.isSCEVable(Phi->getType())) |
| 1908 | continue; |
| 1909 | |
| 1910 | PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)]; |
| 1911 | if (!OrigPhiRef) { |
| 1912 | OrigPhiRef = Phi; |
Sanjoy Das | b03e0b7 | 2016-05-10 00:32:31 +0000 | [diff] [blame] | 1913 | if (Phi->getType()->isIntegerTy() && TTI && |
| 1914 | TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) { |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1915 | // This phi can be freely truncated to the narrowest phi type. Map the |
| 1916 | // truncated expression to it so it will be reused for narrow types. |
| 1917 | const SCEV *TruncExpr = |
| 1918 | SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType()); |
| 1919 | ExprToIVMap[TruncExpr] = Phi; |
| 1920 | } |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1921 | continue; |
| 1922 | } |
| 1923 | |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1924 | // Replacing a pointer phi with an integer phi or vice-versa doesn't make |
| 1925 | // sense. |
| 1926 | if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy()) |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1927 | continue; |
| 1928 | |
| 1929 | if (BasicBlock *LatchBlock = L->getLoopLatch()) { |
Sanjoy Das | 2731c7a | 2016-05-11 17:41:41 +0000 | [diff] [blame] | 1930 | Instruction *OrigInc = dyn_cast<Instruction>( |
| 1931 | OrigPhiRef->getIncomingValueForBlock(LatchBlock)); |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1932 | Instruction *IsomorphicInc = |
Sanjoy Das | 2731c7a | 2016-05-11 17:41:41 +0000 | [diff] [blame] | 1933 | dyn_cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock)); |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1934 | |
Sanjoy Das | 2731c7a | 2016-05-11 17:41:41 +0000 | [diff] [blame] | 1935 | if (OrigInc && IsomorphicInc) { |
| 1936 | // If this phi has the same width but is more canonical, replace the |
| 1937 | // original with it. As part of the "more canonical" determination, |
| 1938 | // respect a prior decision to use an IV chain. |
| 1939 | if (OrigPhiRef->getType() == Phi->getType() && |
| 1940 | !(ChainedPhis.count(Phi) || |
| 1941 | isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)) && |
| 1942 | (ChainedPhis.count(Phi) || |
| 1943 | isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) { |
| 1944 | std::swap(OrigPhiRef, Phi); |
| 1945 | std::swap(OrigInc, IsomorphicInc); |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1946 | } |
Sanjoy Das | 2731c7a | 2016-05-11 17:41:41 +0000 | [diff] [blame] | 1947 | // Replacing the congruent phi is sufficient because acyclic |
| 1948 | // redundancy elimination, CSE/GVN, should handle the |
| 1949 | // rest. However, once SCEV proves that a phi is congruent, |
| 1950 | // it's often the head of an IV user cycle that is isomorphic |
| 1951 | // with the original phi. It's worth eagerly cleaning up the |
| 1952 | // common case of a single IV increment so that DeleteDeadPHIs |
| 1953 | // can remove cycles that had postinc uses. |
| 1954 | const SCEV *TruncExpr = |
| 1955 | SE.getTruncateOrNoop(SE.getSCEV(OrigInc), IsomorphicInc->getType()); |
| 1956 | if (OrigInc != IsomorphicInc && |
| 1957 | TruncExpr == SE.getSCEV(IsomorphicInc) && |
| 1958 | SE.LI.replacementPreservesLCSSAForm(IsomorphicInc, OrigInc) && |
| 1959 | hoistIVInc(OrigInc, IsomorphicInc)) { |
| 1960 | DEBUG_WITH_TYPE(DebugType, |
| 1961 | dbgs() << "INDVARS: Eliminated congruent iv.inc: " |
| 1962 | << *IsomorphicInc << '\n'); |
| 1963 | Value *NewInc = OrigInc; |
| 1964 | if (OrigInc->getType() != IsomorphicInc->getType()) { |
| 1965 | Instruction *IP = nullptr; |
| 1966 | if (PHINode *PN = dyn_cast<PHINode>(OrigInc)) |
| 1967 | IP = &*PN->getParent()->getFirstInsertionPt(); |
| 1968 | else |
| 1969 | IP = OrigInc->getNextNode(); |
| 1970 | |
| 1971 | IRBuilder<> Builder(IP); |
| 1972 | Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc()); |
| 1973 | NewInc = Builder.CreateTruncOrBitCast( |
| 1974 | OrigInc, IsomorphicInc->getType(), IVName); |
| 1975 | } |
| 1976 | IsomorphicInc->replaceAllUsesWith(NewInc); |
| 1977 | DeadInsts.emplace_back(IsomorphicInc); |
| 1978 | } |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
Sanjoy Das | b03e0b7 | 2016-05-10 00:32:31 +0000 | [diff] [blame] | 1981 | DEBUG_WITH_TYPE(DebugType, dbgs() << "INDVARS: Eliminated congruent iv: " |
| 1982 | << *Phi << '\n'); |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1983 | ++NumElim; |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1984 | Value *NewIV = OrigPhiRef; |
| 1985 | if (OrigPhiRef->getType() != Phi->getType()) { |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1986 | IRBuilder<> Builder(&*L->getHeader()->getFirstInsertionPt()); |
Andrew Trick | ee98aa8 | 2012-01-07 01:12:09 +0000 | [diff] [blame] | 1987 | Builder.SetCurrentDebugLocation(Phi->getDebugLoc()); |
| 1988 | NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName); |
| 1989 | } |
| 1990 | Phi->replaceAllUsesWith(NewIV); |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 1991 | DeadInsts.emplace_back(Phi); |
Andrew Trick | 2044941 | 2011-10-11 02:28:51 +0000 | [diff] [blame] | 1992 | } |
| 1993 | return NumElim; |
| 1994 | } |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 1995 | |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 1996 | Value *SCEVExpander::getExactExistingExpansion(const SCEV *S, |
| 1997 | const Instruction *At, Loop *L) { |
| 1998 | Optional<ScalarEvolution::ValueOffsetPair> VO = |
| 1999 | getRelatedExistingExpansion(S, At, L); |
| 2000 | if (VO && VO.getValue().second == nullptr) |
| 2001 | return VO.getValue().first; |
| 2002 | return nullptr; |
| 2003 | } |
| 2004 | |
| 2005 | Optional<ScalarEvolution::ValueOffsetPair> |
| 2006 | SCEVExpander::getRelatedExistingExpansion(const SCEV *S, const Instruction *At, |
| 2007 | Loop *L) { |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2008 | using namespace llvm::PatternMatch; |
| 2009 | |
Igor Laevsky | dcf7488 | 2015-08-17 16:37:04 +0000 | [diff] [blame] | 2010 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 2011 | L->getExitingBlocks(ExitingBlocks); |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2012 | |
Igor Laevsky | dcf7488 | 2015-08-17 16:37:04 +0000 | [diff] [blame] | 2013 | // Look for suitable value in simple conditions at the loop exits. |
| 2014 | for (BasicBlock *BB : ExitingBlocks) { |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2015 | ICmpInst::Predicate Pred; |
| 2016 | Instruction *LHS, *RHS; |
| 2017 | BasicBlock *TrueBB, *FalseBB; |
| 2018 | |
| 2019 | if (!match(BB->getTerminator(), |
| 2020 | m_Br(m_ICmp(Pred, m_Instruction(LHS), m_Instruction(RHS)), |
| 2021 | TrueBB, FalseBB))) |
| 2022 | continue; |
| 2023 | |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 2024 | if (SE.getSCEV(LHS) == S && SE.DT.dominates(LHS, At)) |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2025 | return ScalarEvolution::ValueOffsetPair(LHS, nullptr); |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2026 | |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 2027 | if (SE.getSCEV(RHS) == S && SE.DT.dominates(RHS, At)) |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2028 | return ScalarEvolution::ValueOffsetPair(RHS, nullptr); |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 2031 | // Use expand's logic which is used for reusing a previous Value in |
| 2032 | // ExprValueMap. |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2033 | ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, At); |
| 2034 | if (VO.first) |
| 2035 | return VO; |
Junmo Park | 4ee5ab9 | 2016-02-16 06:46:58 +0000 | [diff] [blame] | 2036 | |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2037 | // There is potential to make this significantly smarter, but this simple |
| 2038 | // heuristic already gets some interesting cases. |
| 2039 | |
| 2040 | // Can not find suitable value. |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2041 | return None; |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2044 | bool SCEVExpander::isHighCostExpansionHelper( |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2045 | const SCEV *S, Loop *L, const Instruction *At, |
| 2046 | SmallPtrSetImpl<const SCEV *> &Processed) { |
| 2047 | |
Simon Pilgrim | 84d1e91 | 2016-11-20 13:47:59 +0000 | [diff] [blame] | 2048 | // If we can find an existing value for this scev available at the point "At" |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2049 | // then consider the expression cheap. |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2050 | if (At && getRelatedExistingExpansion(S, At, L)) |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2051 | return false; |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Zero/One operand expressions |
| 2054 | switch (S->getSCEVType()) { |
| 2055 | case scUnknown: |
| 2056 | case scConstant: |
| 2057 | return false; |
| 2058 | case scTruncate: |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2059 | return isHighCostExpansionHelper(cast<SCEVTruncateExpr>(S)->getOperand(), |
| 2060 | L, At, Processed); |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2061 | case scZeroExtend: |
| 2062 | return isHighCostExpansionHelper(cast<SCEVZeroExtendExpr>(S)->getOperand(), |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2063 | L, At, Processed); |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2064 | case scSignExtend: |
| 2065 | return isHighCostExpansionHelper(cast<SCEVSignExtendExpr>(S)->getOperand(), |
Igor Laevsky | b1b95a5 | 2015-08-10 18:23:58 +0000 | [diff] [blame] | 2066 | L, At, Processed); |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2069 | if (!Processed.insert(S).second) |
| 2070 | return false; |
| 2071 | |
Sanjoy Das | 17e08f5 | 2015-04-14 03:20:32 +0000 | [diff] [blame] | 2072 | if (auto *UDivExpr = dyn_cast<SCEVUDivExpr>(S)) { |
| 2073 | // If the divisor is a power of two and the SCEV type fits in a native |
Benjamin Kramer | d3c712e | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2074 | // integer, consider the division cheap irrespective of whether it occurs in |
Sanjoy Das | 17e08f5 | 2015-04-14 03:20:32 +0000 | [diff] [blame] | 2075 | // the user code since it can be lowered into a right shift. |
| 2076 | if (auto *SC = dyn_cast<SCEVConstant>(UDivExpr->getRHS())) |
Sanjoy Das | 4b89241 | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 2077 | if (SC->getAPInt().isPowerOf2()) { |
Sanjoy Das | 17e08f5 | 2015-04-14 03:20:32 +0000 | [diff] [blame] | 2078 | const DataLayout &DL = |
| 2079 | L->getHeader()->getParent()->getParent()->getDataLayout(); |
| 2080 | unsigned Width = cast<IntegerType>(UDivExpr->getType())->getBitWidth(); |
| 2081 | return DL.isIllegalInteger(Width); |
| 2082 | } |
| 2083 | |
| 2084 | // UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or |
| 2085 | // HowManyLessThans produced to compute a precise expression, rather than a |
| 2086 | // UDiv from the user's code. If we can't find a UDiv in the code with some |
| 2087 | // simple searching, assume the former consider UDivExpr expensive to |
| 2088 | // compute. |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2089 | BasicBlock *ExitingBB = L->getExitingBlock(); |
| 2090 | if (!ExitingBB) |
| 2091 | return true; |
| 2092 | |
Igor Laevsky | dcf7488 | 2015-08-17 16:37:04 +0000 | [diff] [blame] | 2093 | // At the beginning of this function we already tried to find existing value |
| 2094 | // for plain 'S'. Now try to lookup 'S + 1' since it is common pattern |
| 2095 | // involving division. This is just a simple search heuristic. |
| 2096 | if (!At) |
| 2097 | At = &ExitingBB->back(); |
Wei Mi | fc25cfb | 2016-08-09 20:40:03 +0000 | [diff] [blame] | 2098 | if (!getRelatedExistingExpansion( |
Igor Laevsky | dcf7488 | 2015-08-17 16:37:04 +0000 | [diff] [blame] | 2099 | SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), At, L)) |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2100 | return true; |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2103 | // HowManyLessThans uses a Max expression whenever the loop is not guarded by |
| 2104 | // the exit condition. |
| 2105 | if (isa<SCEVSMaxExpr>(S) || isa<SCEVUMaxExpr>(S)) |
| 2106 | return true; |
| 2107 | |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2108 | // Recurse past nary expressions, which commonly occur in the |
| 2109 | // BackedgeTakenCount. They may already exist in program code, and if not, |
| 2110 | // they are not too expensive rematerialize. |
| 2111 | if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(S)) { |
Sanjoy Das | 108f8f3 | 2015-11-21 23:20:10 +0000 | [diff] [blame] | 2112 | for (auto *Op : NAry->operands()) |
| 2113 | if (isHighCostExpansionHelper(Op, L, At, Processed)) |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2114 | return true; |
Wei Mi | 61897e8 | 2015-05-28 21:49:07 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Sanjoy Das | dee1e82 | 2015-04-14 03:20:28 +0000 | [diff] [blame] | 2117 | // If we haven't recognized an expensive SCEV pattern, assume it's an |
| 2118 | // expression produced by program code. |
| 2119 | return false; |
| 2120 | } |
| 2121 | |
Silviu Baranga | a0b73c2 | 2015-11-02 14:41:02 +0000 | [diff] [blame] | 2122 | Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred, |
| 2123 | Instruction *IP) { |
| 2124 | assert(IP); |
| 2125 | switch (Pred->getKind()) { |
| 2126 | case SCEVPredicate::P_Union: |
| 2127 | return expandUnionPredicate(cast<SCEVUnionPredicate>(Pred), IP); |
| 2128 | case SCEVPredicate::P_Equal: |
| 2129 | return expandEqualPredicate(cast<SCEVEqualPredicate>(Pred), IP); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2130 | case SCEVPredicate::P_Wrap: { |
| 2131 | auto *AddRecPred = cast<SCEVWrapPredicate>(Pred); |
| 2132 | return expandWrapPredicate(AddRecPred, IP); |
| 2133 | } |
Silviu Baranga | a0b73c2 | 2015-11-02 14:41:02 +0000 | [diff] [blame] | 2134 | } |
| 2135 | llvm_unreachable("Unknown SCEV predicate type"); |
| 2136 | } |
| 2137 | |
| 2138 | Value *SCEVExpander::expandEqualPredicate(const SCEVEqualPredicate *Pred, |
| 2139 | Instruction *IP) { |
| 2140 | Value *Expr0 = expandCodeFor(Pred->getLHS(), Pred->getLHS()->getType(), IP); |
| 2141 | Value *Expr1 = expandCodeFor(Pred->getRHS(), Pred->getRHS()->getType(), IP); |
| 2142 | |
| 2143 | Builder.SetInsertPoint(IP); |
| 2144 | auto *I = Builder.CreateICmpNE(Expr0, Expr1, "ident.check"); |
| 2145 | return I; |
| 2146 | } |
| 2147 | |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2148 | Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR, |
| 2149 | Instruction *Loc, bool Signed) { |
| 2150 | assert(AR->isAffine() && "Cannot generate RT check for " |
| 2151 | "non-affine expression"); |
| 2152 | |
Silviu Baranga | d8cc816 | 2016-04-08 14:29:09 +0000 | [diff] [blame] | 2153 | SCEVUnionPredicate Pred; |
| 2154 | const SCEV *ExitCount = |
| 2155 | SE.getPredicatedBackedgeTakenCount(AR->getLoop(), Pred); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2156 | |
| 2157 | assert(ExitCount != SE.getCouldNotCompute() && "Invalid loop count"); |
| 2158 | |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2159 | const SCEV *Step = AR->getStepRecurrence(SE); |
| 2160 | const SCEV *Start = AR->getStart(); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2161 | |
Keno Fischer | 3cb83a5 | 2018-07-26 21:55:06 +0000 | [diff] [blame] | 2162 | Type *ARTy = AR->getType(); |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2163 | unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->getType()); |
Keno Fischer | 3cb83a5 | 2018-07-26 21:55:06 +0000 | [diff] [blame] | 2164 | unsigned DstBits = SE.getTypeSizeInBits(ARTy); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2165 | |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2166 | // The expression {Start,+,Step} has nusw/nssw if |
| 2167 | // Step < 0, Start - |Step| * Backedge <= Start |
| 2168 | // Step >= 0, Start + |Step| * Backedge > Start |
| 2169 | // and |Step| * Backedge doesn't unsigned overflow. |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2170 | |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2171 | IntegerType *CountTy = IntegerType::get(Loc->getContext(), SrcBits); |
| 2172 | Builder.SetInsertPoint(Loc); |
| 2173 | Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc); |
| 2174 | |
| 2175 | IntegerType *Ty = |
Keno Fischer | 3cb83a5 | 2018-07-26 21:55:06 +0000 | [diff] [blame] | 2176 | IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(ARTy)); |
| 2177 | Type *ARExpandTy = DL.isNonIntegralPointerType(ARTy) ? ARTy : Ty; |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2178 | |
| 2179 | Value *StepValue = expandCodeFor(Step, Ty, Loc); |
| 2180 | Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc); |
Keno Fischer | 3cb83a5 | 2018-07-26 21:55:06 +0000 | [diff] [blame] | 2181 | Value *StartValue = expandCodeFor(Start, ARExpandTy, Loc); |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2182 | |
| 2183 | ConstantInt *Zero = |
| 2184 | ConstantInt::get(Loc->getContext(), APInt::getNullValue(DstBits)); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2185 | |
| 2186 | Builder.SetInsertPoint(Loc); |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2187 | // Compute |Step| |
| 2188 | Value *StepCompare = Builder.CreateICmp(ICmpInst::ICMP_SLT, StepValue, Zero); |
| 2189 | Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue); |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2190 | |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2191 | // Get the backedge taken count and truncate or extended to the AR type. |
| 2192 | Value *TruncTripCount = Builder.CreateZExtOrTrunc(TripCountVal, Ty); |
| 2193 | auto *MulF = Intrinsic::getDeclaration(Loc->getModule(), |
| 2194 | Intrinsic::umul_with_overflow, Ty); |
| 2195 | |
| 2196 | // Compute |Step| * Backedge |
| 2197 | CallInst *Mul = Builder.CreateCall(MulF, {AbsStep, TruncTripCount}, "mul"); |
| 2198 | Value *MulV = Builder.CreateExtractValue(Mul, 0, "mul.result"); |
| 2199 | Value *OfMul = Builder.CreateExtractValue(Mul, 1, "mul.overflow"); |
| 2200 | |
| 2201 | // Compute: |
| 2202 | // Start + |Step| * Backedge < Start |
| 2203 | // Start - |Step| * Backedge > Start |
Keno Fischer | 3cb83a5 | 2018-07-26 21:55:06 +0000 | [diff] [blame] | 2204 | Value *Add = nullptr, *Sub = nullptr; |
| 2205 | if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARExpandTy)) { |
| 2206 | const SCEV *MulS = SE.getSCEV(MulV); |
| 2207 | const SCEV *NegMulS = SE.getNegativeSCEV(MulS); |
| 2208 | Add = Builder.CreateBitCast(expandAddToGEP(MulS, ARPtrTy, Ty, StartValue), |
| 2209 | ARPtrTy); |
| 2210 | Sub = Builder.CreateBitCast( |
| 2211 | expandAddToGEP(NegMulS, ARPtrTy, Ty, StartValue), ARPtrTy); |
| 2212 | } else { |
| 2213 | Add = Builder.CreateAdd(StartValue, MulV); |
| 2214 | Sub = Builder.CreateSub(StartValue, MulV); |
| 2215 | } |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2216 | |
| 2217 | Value *EndCompareGT = Builder.CreateICmp( |
| 2218 | Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, Sub, StartValue); |
| 2219 | |
| 2220 | Value *EndCompareLT = Builder.CreateICmp( |
| 2221 | Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, Add, StartValue); |
| 2222 | |
| 2223 | // Select the answer based on the sign of Step. |
| 2224 | Value *EndCheck = |
| 2225 | Builder.CreateSelect(StepCompare, EndCompareGT, EndCompareLT); |
| 2226 | |
| 2227 | // If the backedge taken count type is larger than the AR type, |
| 2228 | // check that we don't drop any bits by truncating it. If we are |
Vedant Kumar | d924442 | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 2229 | // dropping bits, then we have overflow (unless the step is zero). |
Silviu Baranga | 5a39727 | 2016-04-25 09:27:16 +0000 | [diff] [blame] | 2230 | if (SE.getTypeSizeInBits(CountTy) > SE.getTypeSizeInBits(Ty)) { |
| 2231 | auto MaxVal = APInt::getMaxValue(DstBits).zext(SrcBits); |
| 2232 | auto *BackedgeCheck = |
| 2233 | Builder.CreateICmp(ICmpInst::ICMP_UGT, TripCountVal, |
| 2234 | ConstantInt::get(Loc->getContext(), MaxVal)); |
| 2235 | BackedgeCheck = Builder.CreateAnd( |
| 2236 | BackedgeCheck, Builder.CreateICmp(ICmpInst::ICMP_NE, StepValue, Zero)); |
| 2237 | |
| 2238 | EndCheck = Builder.CreateOr(EndCheck, BackedgeCheck); |
| 2239 | } |
| 2240 | |
| 2241 | EndCheck = Builder.CreateOr(EndCheck, OfMul); |
| 2242 | return EndCheck; |
Silviu Baranga | e942cf8 | 2016-02-08 17:02:45 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | Value *SCEVExpander::expandWrapPredicate(const SCEVWrapPredicate *Pred, |
| 2246 | Instruction *IP) { |
| 2247 | const auto *A = cast<SCEVAddRecExpr>(Pred->getExpr()); |
| 2248 | Value *NSSWCheck = nullptr, *NUSWCheck = nullptr; |
| 2249 | |
| 2250 | // Add a check for NUSW |
| 2251 | if (Pred->getFlags() & SCEVWrapPredicate::IncrementNUSW) |
| 2252 | NUSWCheck = generateOverflowCheck(A, IP, false); |
| 2253 | |
| 2254 | // Add a check for NSSW |
| 2255 | if (Pred->getFlags() & SCEVWrapPredicate::IncrementNSSW) |
| 2256 | NSSWCheck = generateOverflowCheck(A, IP, true); |
| 2257 | |
| 2258 | if (NUSWCheck && NSSWCheck) |
| 2259 | return Builder.CreateOr(NUSWCheck, NSSWCheck); |
| 2260 | |
| 2261 | if (NUSWCheck) |
| 2262 | return NUSWCheck; |
| 2263 | |
| 2264 | if (NSSWCheck) |
| 2265 | return NSSWCheck; |
| 2266 | |
| 2267 | return ConstantInt::getFalse(IP->getContext()); |
| 2268 | } |
| 2269 | |
Silviu Baranga | a0b73c2 | 2015-11-02 14:41:02 +0000 | [diff] [blame] | 2270 | Value *SCEVExpander::expandUnionPredicate(const SCEVUnionPredicate *Union, |
| 2271 | Instruction *IP) { |
| 2272 | auto *BoolType = IntegerType::get(IP->getContext(), 1); |
| 2273 | Value *Check = ConstantInt::getNullValue(BoolType); |
| 2274 | |
| 2275 | // Loop over all checks in this set. |
| 2276 | for (auto Pred : Union->getPredicates()) { |
| 2277 | auto *NextCheck = expandCodeForPredicate(Pred, IP); |
| 2278 | Builder.SetInsertPoint(IP); |
| 2279 | Check = Builder.CreateOr(Check, NextCheck); |
| 2280 | } |
| 2281 | |
| 2282 | return Check; |
| 2283 | } |
| 2284 | |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2285 | namespace { |
| 2286 | // Search for a SCEV subexpression that is not safe to expand. Any expression |
| 2287 | // that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely |
| 2288 | // UDiv expressions. We don't know if the UDiv is derived from an IR divide |
| 2289 | // instruction, but the important thing is that we prove the denominator is |
| 2290 | // nonzero before expansion. |
| 2291 | // |
| 2292 | // IVUsers already checks that IV-derived expressions are safe. So this check is |
| 2293 | // only needed when the expression includes some subexpression that is not IV |
| 2294 | // derived. |
| 2295 | // |
Max Kazantsev | 11fa8e3 | 2017-10-27 04:17:44 +0000 | [diff] [blame] | 2296 | // Currently, we only allow division by a nonzero constant here. If this is |
| 2297 | // inadequate, we could easily allow division by SCEVUnknown by using |
| 2298 | // ValueTracking to check isKnownNonZero(). |
| 2299 | // |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2300 | // We cannot generally expand recurrences unless the step dominates the loop |
| 2301 | // header. The expander handles the special case of affine recurrences by |
| 2302 | // scaling the recurrence outside the loop, but this technique isn't generally |
| 2303 | // applicable. Expanding a nested recurrence outside a loop requires computing |
| 2304 | // binomial coefficients. This could be done, but the recurrence has to be in a |
| 2305 | // perfectly reduced form, which can't be guaranteed. |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2306 | struct SCEVFindUnsafe { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2307 | ScalarEvolution &SE; |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2308 | bool IsUnsafe; |
| 2309 | |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2310 | SCEVFindUnsafe(ScalarEvolution &se): SE(se), IsUnsafe(false) {} |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2311 | |
| 2312 | bool follow(const SCEV *S) { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2313 | if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
Max Kazantsev | 11fa8e3 | 2017-10-27 04:17:44 +0000 | [diff] [blame] | 2314 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(D->getRHS()); |
| 2315 | if (!SC || SC->getValue()->isZero()) { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2316 | IsUnsafe = true; |
| 2317 | return false; |
| 2318 | } |
Max Kazantsev | 11fa8e3 | 2017-10-27 04:17:44 +0000 | [diff] [blame] | 2319 | } |
| 2320 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2321 | const SCEV *Step = AR->getStepRecurrence(SE); |
| 2322 | if (!AR->isAffine() && !SE.dominates(Step, AR->getLoop()->getHeader())) { |
| 2323 | IsUnsafe = true; |
| 2324 | return false; |
| 2325 | } |
| 2326 | } |
| 2327 | return true; |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2328 | } |
| 2329 | bool isDone() const { return IsUnsafe; } |
| 2330 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 2331 | } |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2332 | |
| 2333 | namespace llvm { |
Andrew Trick | 4d4bbaf | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 2334 | bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE) { |
| 2335 | SCEVFindUnsafe Search(SE); |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2336 | visitAll(S, Search); |
| 2337 | return !Search.IsUnsafe; |
| 2338 | } |
Max Kazantsev | cb0def4 | 2017-11-16 05:10:56 +0000 | [diff] [blame] | 2339 | |
| 2340 | bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint, |
| 2341 | ScalarEvolution &SE) { |
| 2342 | return isSafeToExpand(S, SE) && SE.dominates(S, InsertionPoint->getParent()); |
| 2343 | } |
Andrew Trick | e08c322 | 2012-07-13 23:33:10 +0000 | [diff] [blame] | 2344 | } |