Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 1 | //===-- LoopUtils.cpp - Loop Utility functions -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines common loop utility functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Adam Nemet | 2f2bd8c | 2016-07-26 17:52:02 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Chandler Carruth | 4a00088 | 2017-06-25 22:45:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ScopeExit.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
| 17 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/GlobalsModRef.h" |
Philip Reames | a21d5f1 | 2018-03-15 21:04:28 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/InstructionSimplify.h" |
Adam Nemet | 2f2bd8c | 2016-07-26 17:52:02 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Igor Laevsky | c3ccf5d | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/MustExecute.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScalarEvolution.h" |
Adam Nemet | 2f2bd8c | 2016-07-26 17:52:02 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame^] | 29 | #include "llvm/IR/DomTreeUpdater.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Dominators.h" |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Instructions.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 33 | #include "llvm/IR/PatternMatch.h" |
| 34 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 35 | #include "llvm/Pass.h" |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 37 | #include "llvm/Support/KnownBits.h" |
Chandler Carruth | 4a00088 | 2017-06-25 22:45:31 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::PatternMatch; |
| 42 | |
| 43 | #define DEBUG_TYPE "loop-utils" |
| 44 | |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 45 | bool RecurrenceDescriptor::areAllUsesIn(Instruction *I, |
| 46 | SmallPtrSetImpl<Instruction *> &Set) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 47 | for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use) |
| 48 | if (!Set.count(dyn_cast<Instruction>(*Use))) |
| 49 | return false; |
| 50 | return true; |
| 51 | } |
| 52 | |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 53 | bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurrenceKind Kind) { |
| 54 | switch (Kind) { |
| 55 | default: |
| 56 | break; |
| 57 | case RK_IntegerAdd: |
| 58 | case RK_IntegerMult: |
| 59 | case RK_IntegerOr: |
| 60 | case RK_IntegerAnd: |
| 61 | case RK_IntegerXor: |
| 62 | case RK_IntegerMinMax: |
| 63 | return true; |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurrenceKind Kind) { |
| 69 | return (Kind != RK_NoRecurrence) && !isIntegerRecurrenceKind(Kind); |
| 70 | } |
| 71 | |
| 72 | bool RecurrenceDescriptor::isArithmeticRecurrenceKind(RecurrenceKind Kind) { |
| 73 | switch (Kind) { |
| 74 | default: |
| 75 | break; |
| 76 | case RK_IntegerAdd: |
| 77 | case RK_IntegerMult: |
| 78 | case RK_FloatAdd: |
| 79 | case RK_FloatMult: |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 85 | /// Determines if Phi may have been type-promoted. If Phi has a single user |
| 86 | /// that ANDs the Phi with a type mask, return the user. RT is updated to |
| 87 | /// account for the narrower bit width represented by the mask, and the AND |
| 88 | /// instruction is added to CI. |
| 89 | static Instruction *lookThroughAnd(PHINode *Phi, Type *&RT, |
| 90 | SmallPtrSetImpl<Instruction *> &Visited, |
| 91 | SmallPtrSetImpl<Instruction *> &CI) { |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 92 | if (!Phi->hasOneUse()) |
| 93 | return Phi; |
| 94 | |
| 95 | const APInt *M = nullptr; |
| 96 | Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser()); |
| 97 | |
| 98 | // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT |
| 99 | // with a new integer type of the corresponding bit width. |
Craig Topper | 72ee694 | 2017-06-24 06:24:01 +0000 | [diff] [blame] | 100 | if (match(J, m_c_And(m_Instruction(I), m_APInt(M)))) { |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 101 | int32_t Bits = (*M + 1).exactLogBase2(); |
| 102 | if (Bits > 0) { |
| 103 | RT = IntegerType::get(Phi->getContext(), Bits); |
| 104 | Visited.insert(Phi); |
| 105 | CI.insert(J); |
| 106 | return J; |
| 107 | } |
| 108 | } |
| 109 | return Phi; |
| 110 | } |
| 111 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 112 | /// Compute the minimal bit width needed to represent a reduction whose exit |
| 113 | /// instruction is given by Exit. |
| 114 | static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit, |
| 115 | DemandedBits *DB, |
| 116 | AssumptionCache *AC, |
| 117 | DominatorTree *DT) { |
| 118 | bool IsSigned = false; |
| 119 | const DataLayout &DL = Exit->getModule()->getDataLayout(); |
| 120 | uint64_t MaxBitWidth = DL.getTypeSizeInBits(Exit->getType()); |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 121 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 122 | if (DB) { |
| 123 | // Use the demanded bits analysis to determine the bits that are live out |
| 124 | // of the exit instruction, rounding up to the nearest power of two. If the |
| 125 | // use of demanded bits results in a smaller bit width, we know the value |
| 126 | // must be positive (i.e., IsSigned = false), because if this were not the |
| 127 | // case, the sign bit would have been demanded. |
| 128 | auto Mask = DB->getDemandedBits(Exit); |
| 129 | MaxBitWidth = Mask.getBitWidth() - Mask.countLeadingZeros(); |
| 130 | } |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 131 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 132 | if (MaxBitWidth == DL.getTypeSizeInBits(Exit->getType()) && AC && DT) { |
| 133 | // If demanded bits wasn't able to limit the bit width, we can try to use |
| 134 | // value tracking instead. This can be the case, for example, if the value |
| 135 | // may be negative. |
| 136 | auto NumSignBits = ComputeNumSignBits(Exit, DL, 0, AC, nullptr, DT); |
| 137 | auto NumTypeBits = DL.getTypeSizeInBits(Exit->getType()); |
| 138 | MaxBitWidth = NumTypeBits - NumSignBits; |
| 139 | KnownBits Bits = computeKnownBits(Exit, DL); |
| 140 | if (!Bits.isNonNegative()) { |
| 141 | // If the value is not known to be non-negative, we set IsSigned to true, |
| 142 | // meaning that we will use sext instructions instead of zext |
| 143 | // instructions to restore the original type. |
| 144 | IsSigned = true; |
| 145 | if (!Bits.isNegative()) |
| 146 | // If the value is not known to be negative, we don't known what the |
| 147 | // upper bit is, and therefore, we don't know what kind of extend we |
| 148 | // will need. In this case, just increase the bit width by one bit and |
| 149 | // use sext. |
| 150 | ++MaxBitWidth; |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 153 | if (!isPowerOf2_64(MaxBitWidth)) |
| 154 | MaxBitWidth = NextPowerOf2(MaxBitWidth); |
| 155 | |
| 156 | return std::make_pair(Type::getIntNTy(Exit->getContext(), MaxBitWidth), |
| 157 | IsSigned); |
| 158 | } |
| 159 | |
| 160 | /// Collect cast instructions that can be ignored in the vectorizer's cost |
| 161 | /// model, given a reduction exit value and the minimal type in which the |
| 162 | /// reduction can be represented. |
| 163 | static void collectCastsToIgnore(Loop *TheLoop, Instruction *Exit, |
| 164 | Type *RecurrenceType, |
| 165 | SmallPtrSetImpl<Instruction *> &Casts) { |
| 166 | |
| 167 | SmallVector<Instruction *, 8> Worklist; |
| 168 | SmallPtrSet<Instruction *, 8> Visited; |
| 169 | Worklist.push_back(Exit); |
| 170 | |
| 171 | while (!Worklist.empty()) { |
| 172 | Instruction *Val = Worklist.pop_back_val(); |
| 173 | Visited.insert(Val); |
| 174 | if (auto *Cast = dyn_cast<CastInst>(Val)) |
| 175 | if (Cast->getSrcTy() == RecurrenceType) { |
| 176 | // If the source type of a cast instruction is equal to the recurrence |
| 177 | // type, it will be eliminated, and should be ignored in the vectorizer |
| 178 | // cost model. |
| 179 | Casts.insert(Cast); |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // Add all operands to the work list if they are loop-varying values that |
| 184 | // we haven't yet visited. |
| 185 | for (Value *O : cast<User>(Val)->operands()) |
| 186 | if (auto *I = dyn_cast<Instruction>(O)) |
| 187 | if (TheLoop->contains(I) && !Visited.count(I)) |
| 188 | Worklist.push_back(I); |
| 189 | } |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 192 | bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind, |
| 193 | Loop *TheLoop, bool HasFunNoNaNAttr, |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 194 | RecurrenceDescriptor &RedDes, |
| 195 | DemandedBits *DB, |
| 196 | AssumptionCache *AC, |
| 197 | DominatorTree *DT) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 198 | if (Phi->getNumIncomingValues() != 2) |
| 199 | return false; |
| 200 | |
| 201 | // Reduction variables are only found in the loop header block. |
| 202 | if (Phi->getParent() != TheLoop->getHeader()) |
| 203 | return false; |
| 204 | |
| 205 | // Obtain the reduction start value from the value that comes from the loop |
| 206 | // preheader. |
| 207 | Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()); |
| 208 | |
| 209 | // ExitInstruction is the single value which is used outside the loop. |
| 210 | // We only allow for a single reduction value to be used outside the loop. |
| 211 | // This includes users of the reduction, variables (which form a cycle |
| 212 | // which ends in the phi node). |
| 213 | Instruction *ExitInstruction = nullptr; |
| 214 | // Indicates that we found a reduction operation in our scan. |
| 215 | bool FoundReduxOp = false; |
| 216 | |
| 217 | // We start with the PHI node and scan for all of the users of this |
| 218 | // instruction. All users must be instructions that can be used as reduction |
| 219 | // variables (such as ADD). We must have a single out-of-block user. The cycle |
| 220 | // must include the original PHI. |
| 221 | bool FoundStartPHI = false; |
| 222 | |
| 223 | // To recognize min/max patterns formed by a icmp select sequence, we store |
| 224 | // the number of instruction we saw from the recognized min/max pattern, |
| 225 | // to make sure we only see exactly the two instructions. |
| 226 | unsigned NumCmpSelectPatternInst = 0; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 227 | InstDesc ReduxDesc(false, nullptr); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 228 | |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 229 | // Data used for determining if the recurrence has been type-promoted. |
| 230 | Type *RecurrenceType = Phi->getType(); |
| 231 | SmallPtrSet<Instruction *, 4> CastInsts; |
| 232 | Instruction *Start = Phi; |
| 233 | bool IsSigned = false; |
| 234 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 235 | SmallPtrSet<Instruction *, 8> VisitedInsts; |
| 236 | SmallVector<Instruction *, 8> Worklist; |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 237 | |
| 238 | // Return early if the recurrence kind does not match the type of Phi. If the |
| 239 | // recurrence kind is arithmetic, we attempt to look through AND operations |
| 240 | // resulting from the type promotion performed by InstCombine. Vector |
| 241 | // operations are not limited to the legal integer widths, so we may be able |
| 242 | // to evaluate the reduction in the narrower width. |
| 243 | if (RecurrenceType->isFloatingPointTy()) { |
| 244 | if (!isFloatingPointRecurrenceKind(Kind)) |
| 245 | return false; |
| 246 | } else { |
| 247 | if (!isIntegerRecurrenceKind(Kind)) |
| 248 | return false; |
| 249 | if (isArithmeticRecurrenceKind(Kind)) |
| 250 | Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts); |
| 251 | } |
| 252 | |
| 253 | Worklist.push_back(Start); |
| 254 | VisitedInsts.insert(Start); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 255 | |
| 256 | // A value in the reduction can be used: |
| 257 | // - By the reduction: |
| 258 | // - Reduction operation: |
| 259 | // - One use of reduction value (safe). |
| 260 | // - Multiple use of reduction value (not safe). |
| 261 | // - PHI: |
| 262 | // - All uses of the PHI must be the reduction (safe). |
| 263 | // - Otherwise, not safe. |
Michael Kuperstein | 7cefb40 | 2017-01-18 19:02:52 +0000 | [diff] [blame] | 264 | // - By instructions outside of the loop (safe). |
| 265 | // * One value may have several outside users, but all outside |
| 266 | // uses must be of the same value. |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 267 | // - By an instruction that is not part of the reduction (not safe). |
| 268 | // This is either: |
| 269 | // * An instruction type other than PHI or the reduction operation. |
| 270 | // * A PHI in the header other than the initial PHI. |
| 271 | while (!Worklist.empty()) { |
| 272 | Instruction *Cur = Worklist.back(); |
| 273 | Worklist.pop_back(); |
| 274 | |
| 275 | // No Users. |
| 276 | // If the instruction has no users then this is a broken chain and can't be |
| 277 | // a reduction variable. |
| 278 | if (Cur->use_empty()) |
| 279 | return false; |
| 280 | |
| 281 | bool IsAPhi = isa<PHINode>(Cur); |
| 282 | |
| 283 | // A header PHI use other than the original PHI. |
| 284 | if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent()) |
| 285 | return false; |
| 286 | |
| 287 | // Reductions of instructions such as Div, and Sub is only possible if the |
| 288 | // LHS is the reduction variable. |
| 289 | if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) && |
| 290 | !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) && |
| 291 | !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0)))) |
| 292 | return false; |
| 293 | |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 294 | // Any reduction instruction must be of one of the allowed kinds. We ignore |
| 295 | // the starting value (the Phi or an AND instruction if the Phi has been |
| 296 | // type-promoted). |
| 297 | if (Cur != Start) { |
| 298 | ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr); |
| 299 | if (!ReduxDesc.isRecurrence()) |
| 300 | return false; |
| 301 | } |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 302 | |
| 303 | // A reduction operation must only have one use of the reduction value. |
| 304 | if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax && |
| 305 | hasMultipleUsesOf(Cur, VisitedInsts)) |
| 306 | return false; |
| 307 | |
| 308 | // All inputs to a PHI node must be a reduction value. |
| 309 | if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts)) |
| 310 | return false; |
| 311 | |
| 312 | if (Kind == RK_IntegerMinMax && |
| 313 | (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur))) |
| 314 | ++NumCmpSelectPatternInst; |
| 315 | if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur))) |
| 316 | ++NumCmpSelectPatternInst; |
| 317 | |
| 318 | // Check whether we found a reduction operator. |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 319 | FoundReduxOp |= !IsAPhi && Cur != Start; |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 320 | |
| 321 | // Process users of current instruction. Push non-PHI nodes after PHI nodes |
| 322 | // onto the stack. This way we are going to have seen all inputs to PHI |
| 323 | // nodes once we get to them. |
| 324 | SmallVector<Instruction *, 8> NonPHIs; |
| 325 | SmallVector<Instruction *, 8> PHIs; |
| 326 | for (User *U : Cur->users()) { |
| 327 | Instruction *UI = cast<Instruction>(U); |
| 328 | |
| 329 | // Check if we found the exit user. |
| 330 | BasicBlock *Parent = UI->getParent(); |
| 331 | if (!TheLoop->contains(Parent)) { |
Michael Kuperstein | 7cefb40 | 2017-01-18 19:02:52 +0000 | [diff] [blame] | 332 | // If we already know this instruction is used externally, move on to |
| 333 | // the next user. |
| 334 | if (ExitInstruction == Cur) |
| 335 | continue; |
| 336 | |
| 337 | // Exit if you find multiple values used outside or if the header phi |
| 338 | // node is being used. In this case the user uses the value of the |
| 339 | // previous iteration, in which case we would loose "VF-1" iterations of |
| 340 | // the reduction operation if we vectorize. |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 341 | if (ExitInstruction != nullptr || Cur == Phi) |
| 342 | return false; |
| 343 | |
| 344 | // The instruction used by an outside user must be the last instruction |
| 345 | // before we feed back to the reduction phi. Otherwise, we loose VF-1 |
| 346 | // operations on the value. |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 347 | if (!is_contained(Phi->operands(), Cur)) |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 348 | return false; |
| 349 | |
| 350 | ExitInstruction = Cur; |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | // Process instructions only once (termination). Each reduction cycle |
| 355 | // value must only be used once, except by phi nodes and min/max |
| 356 | // reductions which are represented as a cmp followed by a select. |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 357 | InstDesc IgnoredVal(false, nullptr); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 358 | if (VisitedInsts.insert(UI).second) { |
| 359 | if (isa<PHINode>(UI)) |
| 360 | PHIs.push_back(UI); |
| 361 | else |
| 362 | NonPHIs.push_back(UI); |
| 363 | } else if (!isa<PHINode>(UI) && |
| 364 | ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) && |
| 365 | !isa<SelectInst>(UI)) || |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 366 | !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence())) |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 367 | return false; |
| 368 | |
| 369 | // Remember that we completed the cycle. |
| 370 | if (UI == Phi) |
| 371 | FoundStartPHI = true; |
| 372 | } |
| 373 | Worklist.append(PHIs.begin(), PHIs.end()); |
| 374 | Worklist.append(NonPHIs.begin(), NonPHIs.end()); |
| 375 | } |
| 376 | |
| 377 | // This means we have seen one but not the other instruction of the |
| 378 | // pattern or more than just a select and cmp. |
| 379 | if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) && |
| 380 | NumCmpSelectPatternInst != 2) |
| 381 | return false; |
| 382 | |
| 383 | if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction) |
| 384 | return false; |
| 385 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 386 | if (Start != Phi) { |
| 387 | // If the starting value is not the same as the phi node, we speculatively |
| 388 | // looked through an 'and' instruction when evaluating a potential |
| 389 | // arithmetic reduction to determine if it may have been type-promoted. |
| 390 | // |
| 391 | // We now compute the minimal bit width that is required to represent the |
| 392 | // reduction. If this is the same width that was indicated by the 'and', we |
| 393 | // can represent the reduction in the smaller type. The 'and' instruction |
| 394 | // will be eliminated since it will essentially be a cast instruction that |
| 395 | // can be ignore in the cost model. If we compute a different type than we |
| 396 | // did when evaluating the 'and', the 'and' will not be eliminated, and we |
| 397 | // will end up with different kinds of operations in the recurrence |
| 398 | // expression (e.g., RK_IntegerAND, RK_IntegerADD). We give up if this is |
| 399 | // the case. |
| 400 | // |
| 401 | // The vectorizer relies on InstCombine to perform the actual |
| 402 | // type-shrinking. It does this by inserting instructions to truncate the |
| 403 | // exit value of the reduction to the width indicated by RecurrenceType and |
| 404 | // then extend this value back to the original width. If IsSigned is false, |
| 405 | // a 'zext' instruction will be generated; otherwise, a 'sext' will be |
| 406 | // used. |
| 407 | // |
| 408 | // TODO: We should not rely on InstCombine to rewrite the reduction in the |
| 409 | // smaller type. We should just generate a correctly typed expression |
| 410 | // to begin with. |
| 411 | Type *ComputedType; |
| 412 | std::tie(ComputedType, IsSigned) = |
| 413 | computeRecurrenceType(ExitInstruction, DB, AC, DT); |
| 414 | if (ComputedType != RecurrenceType) |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 415 | return false; |
| 416 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 417 | // The recurrence expression will be represented in a narrower type. If |
| 418 | // there are any cast instructions that will be unnecessary, collect them |
| 419 | // in CastInsts. Note that the 'and' instruction was already included in |
| 420 | // this list. |
| 421 | // |
| 422 | // TODO: A better way to represent this may be to tag in some way all the |
| 423 | // instructions that are a part of the reduction. The vectorizer cost |
| 424 | // model could then apply the recurrence type to these instructions, |
| 425 | // without needing a white list of instructions to ignore. |
| 426 | collectCastsToIgnore(TheLoop, ExitInstruction, RecurrenceType, CastInsts); |
| 427 | } |
| 428 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 429 | // We found a reduction var if we have reached the original phi node and we |
| 430 | // only have a single instruction with out-of-loop users. |
| 431 | |
| 432 | // The ExitInstruction(Instruction which is allowed to have out-of-loop users) |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 433 | // is saved as part of the RecurrenceDescriptor. |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 434 | |
| 435 | // Save the description of this reduction variable. |
Chad Rosier | c94f8e2 | 2015-08-27 14:12:17 +0000 | [diff] [blame] | 436 | RecurrenceDescriptor RD( |
| 437 | RdxStart, ExitInstruction, Kind, ReduxDesc.getMinMaxKind(), |
| 438 | ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType, IsSigned, CastInsts); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 439 | RedDes = RD; |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction |
| 445 | /// pattern corresponding to a min(X, Y) or max(X, Y). |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 446 | RecurrenceDescriptor::InstDesc |
| 447 | RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 448 | |
| 449 | assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) && |
| 450 | "Expect a select instruction"); |
| 451 | Instruction *Cmp = nullptr; |
| 452 | SelectInst *Select = nullptr; |
| 453 | |
| 454 | // We must handle the select(cmp()) as a single instruction. Advance to the |
| 455 | // select. |
| 456 | if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) { |
| 457 | if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin()))) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 458 | return InstDesc(false, I); |
| 459 | return InstDesc(Select, Prev.getMinMaxKind()); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Only handle single use cases for now. |
| 463 | if (!(Select = dyn_cast<SelectInst>(I))) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 464 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 465 | if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) && |
| 466 | !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0)))) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 467 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 468 | if (!Cmp->hasOneUse()) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 469 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 470 | |
| 471 | Value *CmpLeft; |
| 472 | Value *CmpRight; |
| 473 | |
| 474 | // Look for a min/max pattern. |
| 475 | if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 476 | return InstDesc(Select, MRK_UIntMin); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 477 | else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 478 | return InstDesc(Select, MRK_UIntMax); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 479 | else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 480 | return InstDesc(Select, MRK_SIntMax); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 481 | else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 482 | return InstDesc(Select, MRK_SIntMin); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 483 | else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 484 | return InstDesc(Select, MRK_FloatMin); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 485 | else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 486 | return InstDesc(Select, MRK_FloatMax); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 487 | else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 488 | return InstDesc(Select, MRK_FloatMin); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 489 | else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 490 | return InstDesc(Select, MRK_FloatMax); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 491 | |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 492 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 495 | RecurrenceDescriptor::InstDesc |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 496 | RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind, |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 497 | InstDesc &Prev, bool HasFunNoNaNAttr) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 498 | bool FP = I->getType()->isFloatingPointTy(); |
Tyler Nowicki | c1a86f5 | 2015-08-10 19:51:46 +0000 | [diff] [blame] | 499 | Instruction *UAI = Prev.getUnsafeAlgebraInst(); |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 500 | if (!UAI && FP && !I->isFast()) |
Tyler Nowicki | c1a86f5 | 2015-08-10 19:51:46 +0000 | [diff] [blame] | 501 | UAI = I; // Found an unsafe (unvectorizable) algebra instruction. |
| 502 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 503 | switch (I->getOpcode()) { |
| 504 | default: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 505 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 506 | case Instruction::PHI: |
Tim Northover | 10a1e8b | 2016-05-27 16:40:27 +0000 | [diff] [blame] | 507 | return InstDesc(I, Prev.getMinMaxKind(), Prev.getUnsafeAlgebraInst()); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 508 | case Instruction::Sub: |
| 509 | case Instruction::Add: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 510 | return InstDesc(Kind == RK_IntegerAdd, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 511 | case Instruction::Mul: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 512 | return InstDesc(Kind == RK_IntegerMult, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 513 | case Instruction::And: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 514 | return InstDesc(Kind == RK_IntegerAnd, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 515 | case Instruction::Or: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 516 | return InstDesc(Kind == RK_IntegerOr, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 517 | case Instruction::Xor: |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 518 | return InstDesc(Kind == RK_IntegerXor, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 519 | case Instruction::FMul: |
Tyler Nowicki | c1a86f5 | 2015-08-10 19:51:46 +0000 | [diff] [blame] | 520 | return InstDesc(Kind == RK_FloatMult, I, UAI); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 521 | case Instruction::FSub: |
| 522 | case Instruction::FAdd: |
Tyler Nowicki | c1a86f5 | 2015-08-10 19:51:46 +0000 | [diff] [blame] | 523 | return InstDesc(Kind == RK_FloatAdd, I, UAI); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 524 | case Instruction::FCmp: |
| 525 | case Instruction::ICmp: |
| 526 | case Instruction::Select: |
| 527 | if (Kind != RK_IntegerMinMax && |
| 528 | (!HasFunNoNaNAttr || Kind != RK_FloatMinMax)) |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 529 | return InstDesc(false, I); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 530 | return isMinMaxSelectCmpPattern(I, Prev); |
| 531 | } |
| 532 | } |
| 533 | |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 534 | bool RecurrenceDescriptor::hasMultipleUsesOf( |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 535 | Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) { |
| 536 | unsigned NumUses = 0; |
| 537 | for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; |
| 538 | ++Use) { |
| 539 | if (Insts.count(dyn_cast<Instruction>(*Use))) |
| 540 | ++NumUses; |
| 541 | if (NumUses > 1) |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | return false; |
| 546 | } |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 547 | bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop, |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 548 | RecurrenceDescriptor &RedDes, |
| 549 | DemandedBits *DB, AssumptionCache *AC, |
| 550 | DominatorTree *DT) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 551 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 552 | BasicBlock *Header = TheLoop->getHeader(); |
| 553 | Function &F = *Header->getParent(); |
Nirav Dave | 8dd66e5 | 2016-03-30 15:41:12 +0000 | [diff] [blame] | 554 | bool HasFunNoNaNAttr = |
| 555 | F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true"; |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 556 | |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 557 | if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 558 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 559 | LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 560 | return true; |
| 561 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 562 | if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 563 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 564 | LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 565 | return true; |
| 566 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 567 | if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 568 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 569 | LLVM_DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 570 | return true; |
| 571 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 572 | if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 573 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 574 | LLVM_DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 575 | return true; |
| 576 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 577 | if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 578 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 579 | LLVM_DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 580 | return true; |
| 581 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 582 | if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr, RedDes, |
| 583 | DB, AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 584 | LLVM_DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 585 | return true; |
| 586 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 587 | if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 588 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 589 | LLVM_DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 590 | return true; |
| 591 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 592 | if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 593 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 594 | LLVM_DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 595 | return true; |
| 596 | } |
Chad Rosier | a097bc6 | 2018-02-04 15:42:24 +0000 | [diff] [blame] | 597 | if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes, DB, |
| 598 | AC, DT)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 599 | LLVM_DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi |
| 600 | << "\n"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | // Not a reduction of known type. |
| 604 | return false; |
| 605 | } |
| 606 | |
Ayal Zaks | 2ff59d4 | 2017-06-30 21:05:06 +0000 | [diff] [blame] | 607 | bool RecurrenceDescriptor::isFirstOrderRecurrence( |
| 608 | PHINode *Phi, Loop *TheLoop, |
| 609 | DenseMap<Instruction *, Instruction *> &SinkAfter, DominatorTree *DT) { |
Matthew Simpson | 29c997c | 2016-02-19 17:56:08 +0000 | [diff] [blame] | 610 | |
| 611 | // Ensure the phi node is in the loop header and has two incoming values. |
| 612 | if (Phi->getParent() != TheLoop->getHeader() || |
| 613 | Phi->getNumIncomingValues() != 2) |
| 614 | return false; |
| 615 | |
| 616 | // Ensure the loop has a preheader and a single latch block. The loop |
| 617 | // vectorizer will need the latch to set up the next iteration of the loop. |
| 618 | auto *Preheader = TheLoop->getLoopPreheader(); |
| 619 | auto *Latch = TheLoop->getLoopLatch(); |
| 620 | if (!Preheader || !Latch) |
| 621 | return false; |
| 622 | |
| 623 | // Ensure the phi node's incoming blocks are the loop preheader and latch. |
| 624 | if (Phi->getBasicBlockIndex(Preheader) < 0 || |
| 625 | Phi->getBasicBlockIndex(Latch) < 0) |
| 626 | return false; |
| 627 | |
| 628 | // Get the previous value. The previous value comes from the latch edge while |
| 629 | // the initial value comes form the preheader edge. |
| 630 | auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch)); |
Ayal Zaks | 2ff59d4 | 2017-06-30 21:05:06 +0000 | [diff] [blame] | 631 | if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous) || |
| 632 | SinkAfter.count(Previous)) // Cannot rely on dominance due to motion. |
Matthew Simpson | 29c997c | 2016-02-19 17:56:08 +0000 | [diff] [blame] | 633 | return false; |
| 634 | |
Anna Thomas | dcdb325 | 2017-04-13 18:59:25 +0000 | [diff] [blame] | 635 | // Ensure every user of the phi node is dominated by the previous value. |
| 636 | // The dominance requirement ensures the loop vectorizer will not need to |
| 637 | // vectorize the initial value prior to the first iteration of the loop. |
Ayal Zaks | 2ff59d4 | 2017-06-30 21:05:06 +0000 | [diff] [blame] | 638 | // TODO: Consider extending this sinking to handle other kinds of instructions |
| 639 | // and expressions, beyond sinking a single cast past Previous. |
| 640 | if (Phi->hasOneUse()) { |
| 641 | auto *I = Phi->user_back(); |
| 642 | if (I->isCast() && (I->getParent() == Phi->getParent()) && I->hasOneUse() && |
| 643 | DT->dominates(Previous, I->user_back())) { |
Ayal Zaks | 25e2800 | 2017-08-15 08:32:59 +0000 | [diff] [blame] | 644 | if (!DT->dominates(Previous, I)) // Otherwise we're good w/o sinking. |
| 645 | SinkAfter[I] = Previous; |
Ayal Zaks | 2ff59d4 | 2017-06-30 21:05:06 +0000 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | } |
| 649 | |
Matthew Simpson | 29c997c | 2016-02-19 17:56:08 +0000 | [diff] [blame] | 650 | for (User *U : Phi->users()) |
Anna Thomas | 00dc1b7 | 2017-04-11 21:02:00 +0000 | [diff] [blame] | 651 | if (auto *I = dyn_cast<Instruction>(U)) { |
Matthew Simpson | 29c997c | 2016-02-19 17:56:08 +0000 | [diff] [blame] | 652 | if (!DT->dominates(Previous, I)) |
| 653 | return false; |
Anna Thomas | 00dc1b7 | 2017-04-11 21:02:00 +0000 | [diff] [blame] | 654 | } |
Matthew Simpson | 29c997c | 2016-02-19 17:56:08 +0000 | [diff] [blame] | 655 | |
| 656 | return true; |
| 657 | } |
| 658 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 659 | /// This function returns the identity element (or neutral element) for |
| 660 | /// the operation K. |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 661 | Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K, |
| 662 | Type *Tp) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 663 | switch (K) { |
| 664 | case RK_IntegerXor: |
| 665 | case RK_IntegerAdd: |
| 666 | case RK_IntegerOr: |
| 667 | // Adding, Xoring, Oring zero to a number does not change it. |
| 668 | return ConstantInt::get(Tp, 0); |
| 669 | case RK_IntegerMult: |
| 670 | // Multiplying a number by 1 does not change it. |
| 671 | return ConstantInt::get(Tp, 1); |
| 672 | case RK_IntegerAnd: |
| 673 | // AND-ing a number with an all-1 value does not change it. |
| 674 | return ConstantInt::get(Tp, -1, true); |
| 675 | case RK_FloatMult: |
| 676 | // Multiplying a number by 1 does not change it. |
| 677 | return ConstantFP::get(Tp, 1.0L); |
| 678 | case RK_FloatAdd: |
| 679 | // Adding zero to a number does not change it. |
| 680 | return ConstantFP::get(Tp, 0.0L); |
| 681 | default: |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 682 | llvm_unreachable("Unknown recurrence kind"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 686 | /// This function translates the recurrence kind to an LLVM binary operator. |
| 687 | unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 688 | switch (Kind) { |
| 689 | case RK_IntegerAdd: |
| 690 | return Instruction::Add; |
| 691 | case RK_IntegerMult: |
| 692 | return Instruction::Mul; |
| 693 | case RK_IntegerOr: |
| 694 | return Instruction::Or; |
| 695 | case RK_IntegerAnd: |
| 696 | return Instruction::And; |
| 697 | case RK_IntegerXor: |
| 698 | return Instruction::Xor; |
| 699 | case RK_FloatMult: |
| 700 | return Instruction::FMul; |
| 701 | case RK_FloatAdd: |
| 702 | return Instruction::FAdd; |
| 703 | case RK_IntegerMinMax: |
| 704 | return Instruction::ICmp; |
| 705 | case RK_FloatMinMax: |
| 706 | return Instruction::FCmp; |
| 707 | default: |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 708 | llvm_unreachable("Unknown recurrence operation"); |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 712 | Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder, |
| 713 | MinMaxRecurrenceKind RK, |
| 714 | Value *Left, Value *Right) { |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 715 | CmpInst::Predicate P = CmpInst::ICMP_NE; |
| 716 | switch (RK) { |
| 717 | default: |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 718 | llvm_unreachable("Unknown min/max recurrence kind"); |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 719 | case MRK_UIntMin: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 720 | P = CmpInst::ICMP_ULT; |
| 721 | break; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 722 | case MRK_UIntMax: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 723 | P = CmpInst::ICMP_UGT; |
| 724 | break; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 725 | case MRK_SIntMin: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 726 | P = CmpInst::ICMP_SLT; |
| 727 | break; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 728 | case MRK_SIntMax: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 729 | P = CmpInst::ICMP_SGT; |
| 730 | break; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 731 | case MRK_FloatMin: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 732 | P = CmpInst::FCMP_OLT; |
| 733 | break; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 734 | case MRK_FloatMax: |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 735 | P = CmpInst::FCMP_OGT; |
| 736 | break; |
| 737 | } |
| 738 | |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 739 | // We only match FP sequences that are 'fast', so we can unconditionally |
James Molloy | 50a4c27 | 2015-09-21 19:41:19 +0000 | [diff] [blame] | 740 | // set it on any generated instructions. |
| 741 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
| 742 | FastMathFlags FMF; |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 743 | FMF.setFast(); |
Sanjay Patel | a252815 | 2016-01-12 18:03:37 +0000 | [diff] [blame] | 744 | Builder.setFastMathFlags(FMF); |
James Molloy | 50a4c27 | 2015-09-21 19:41:19 +0000 | [diff] [blame] | 745 | |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 746 | Value *Cmp; |
Tyler Nowicki | 27b2c39 | 2015-06-16 22:59:45 +0000 | [diff] [blame] | 747 | if (RK == MRK_FloatMin || RK == MRK_FloatMax) |
Karthik Bhat | 76aa662 | 2015-04-20 04:38:33 +0000 | [diff] [blame] | 748 | Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp"); |
| 749 | else |
| 750 | Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp"); |
| 751 | |
| 752 | Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select"); |
| 753 | return Select; |
| 754 | } |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 755 | |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 756 | InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K, |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 757 | const SCEV *Step, BinaryOperator *BOp, |
| 758 | SmallVectorImpl<Instruction *> *Casts) |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 759 | : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) { |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 760 | assert(IK != IK_NoInduction && "Not an induction"); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 761 | |
| 762 | // Start value type should match the induction kind and the value |
| 763 | // itself should not be null. |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 764 | assert(StartValue && "StartValue is null"); |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 765 | assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) && |
| 766 | "StartValue is not a pointer for pointer induction"); |
| 767 | assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) && |
| 768 | "StartValue is not an integer for integer induction"); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 769 | |
| 770 | // Check the Step Value. It should be non-zero integer value. |
| 771 | assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) && |
| 772 | "Step value is zero"); |
| 773 | |
| 774 | assert((IK != IK_PtrInduction || getConstIntStepValue()) && |
| 775 | "Step value should be constant for pointer induction"); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 776 | assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) && |
| 777 | "StepValue is not an integer"); |
| 778 | |
| 779 | assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) && |
| 780 | "StepValue is not FP for FpInduction"); |
| 781 | assert((IK != IK_FpInduction || (InductionBinOp && |
| 782 | (InductionBinOp->getOpcode() == Instruction::FAdd || |
| 783 | InductionBinOp->getOpcode() == Instruction::FSub))) && |
| 784 | "Binary opcode should be specified for FP induction"); |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 785 | |
| 786 | if (Casts) { |
| 787 | for (auto &Inst : *Casts) { |
| 788 | RedundantCasts.push_back(Inst); |
| 789 | } |
| 790 | } |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | int InductionDescriptor::getConsecutiveDirection() const { |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 794 | ConstantInt *ConstStep = getConstIntStepValue(); |
| 795 | if (ConstStep && (ConstStep->isOne() || ConstStep->isMinusOne())) |
| 796 | return ConstStep->getSExtValue(); |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 800 | ConstantInt *InductionDescriptor::getConstIntStepValue() const { |
| 801 | if (isa<SCEVConstant>(Step)) |
| 802 | return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue()); |
| 803 | return nullptr; |
| 804 | } |
| 805 | |
| 806 | Value *InductionDescriptor::transform(IRBuilder<> &B, Value *Index, |
| 807 | ScalarEvolution *SE, |
| 808 | const DataLayout& DL) const { |
| 809 | |
| 810 | SCEVExpander Exp(*SE, DL, "induction"); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 811 | assert(Index->getType() == Step->getType() && |
| 812 | "Index type does not match StepValue type"); |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 813 | switch (IK) { |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 814 | case IK_IntInduction: { |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 815 | assert(Index->getType() == StartValue->getType() && |
| 816 | "Index type does not match StartValue type"); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 817 | |
| 818 | // FIXME: Theoretically, we can call getAddExpr() of ScalarEvolution |
| 819 | // and calculate (Start + Index * Step) for all cases, without |
| 820 | // special handling for "isOne" and "isMinusOne". |
| 821 | // But in the real life the result code getting worse. We mix SCEV |
| 822 | // expressions and ADD/SUB operations and receive redundant |
| 823 | // intermediate values being calculated in different ways and |
| 824 | // Instcombine is unable to reduce them all. |
| 825 | |
| 826 | if (getConstIntStepValue() && |
| 827 | getConstIntStepValue()->isMinusOne()) |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 828 | return B.CreateSub(StartValue, Index); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 829 | if (getConstIntStepValue() && |
| 830 | getConstIntStepValue()->isOne()) |
| 831 | return B.CreateAdd(StartValue, Index); |
| 832 | const SCEV *S = SE->getAddExpr(SE->getSCEV(StartValue), |
| 833 | SE->getMulExpr(Step, SE->getSCEV(Index))); |
| 834 | return Exp.expandCodeFor(S, StartValue->getType(), &*B.GetInsertPoint()); |
| 835 | } |
| 836 | case IK_PtrInduction: { |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 837 | assert(isa<SCEVConstant>(Step) && |
| 838 | "Expected constant step for pointer induction"); |
| 839 | const SCEV *S = SE->getMulExpr(SE->getSCEV(Index), Step); |
| 840 | Index = Exp.expandCodeFor(S, Index->getType(), &*B.GetInsertPoint()); |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 841 | return B.CreateGEP(nullptr, StartValue, Index); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 842 | } |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 843 | case IK_FpInduction: { |
| 844 | assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value"); |
| 845 | assert(InductionBinOp && |
| 846 | (InductionBinOp->getOpcode() == Instruction::FAdd || |
| 847 | InductionBinOp->getOpcode() == Instruction::FSub) && |
| 848 | "Original bin op should be defined for FP induction"); |
| 849 | |
| 850 | Value *StepValue = cast<SCEVUnknown>(Step)->getValue(); |
| 851 | |
| 852 | // Floating point operations had to be 'fast' to enable the induction. |
| 853 | FastMathFlags Flags; |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 854 | Flags.setFast(); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 855 | |
| 856 | Value *MulExp = B.CreateFMul(StepValue, Index); |
| 857 | if (isa<Instruction>(MulExp)) |
| 858 | // We have to check, the MulExp may be a constant. |
| 859 | cast<Instruction>(MulExp)->setFastMathFlags(Flags); |
| 860 | |
| 861 | Value *BOp = B.CreateBinOp(InductionBinOp->getOpcode() , StartValue, |
| 862 | MulExp, "induction"); |
| 863 | if (isa<Instruction>(BOp)) |
| 864 | cast<Instruction>(BOp)->setFastMathFlags(Flags); |
| 865 | |
| 866 | return BOp; |
| 867 | } |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 868 | case IK_NoInduction: |
| 869 | return nullptr; |
| 870 | } |
| 871 | llvm_unreachable("invalid enum"); |
| 872 | } |
| 873 | |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 874 | bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop, |
| 875 | ScalarEvolution *SE, |
| 876 | InductionDescriptor &D) { |
| 877 | |
| 878 | // Here we only handle FP induction variables. |
| 879 | assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type"); |
| 880 | |
| 881 | if (TheLoop->getHeader() != Phi->getParent()) |
| 882 | return false; |
| 883 | |
| 884 | // The loop may have multiple entrances or multiple exits; we can analyze |
| 885 | // this phi if it has a unique entry value and a unique backedge value. |
| 886 | if (Phi->getNumIncomingValues() != 2) |
| 887 | return false; |
| 888 | Value *BEValue = nullptr, *StartValue = nullptr; |
| 889 | if (TheLoop->contains(Phi->getIncomingBlock(0))) { |
| 890 | BEValue = Phi->getIncomingValue(0); |
| 891 | StartValue = Phi->getIncomingValue(1); |
| 892 | } else { |
| 893 | assert(TheLoop->contains(Phi->getIncomingBlock(1)) && |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 894 | "Unexpected Phi node in the loop"); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 895 | BEValue = Phi->getIncomingValue(1); |
| 896 | StartValue = Phi->getIncomingValue(0); |
| 897 | } |
| 898 | |
| 899 | BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue); |
| 900 | if (!BOp) |
| 901 | return false; |
| 902 | |
| 903 | Value *Addend = nullptr; |
| 904 | if (BOp->getOpcode() == Instruction::FAdd) { |
| 905 | if (BOp->getOperand(0) == Phi) |
| 906 | Addend = BOp->getOperand(1); |
| 907 | else if (BOp->getOperand(1) == Phi) |
| 908 | Addend = BOp->getOperand(0); |
| 909 | } else if (BOp->getOpcode() == Instruction::FSub) |
| 910 | if (BOp->getOperand(0) == Phi) |
| 911 | Addend = BOp->getOperand(1); |
| 912 | |
| 913 | if (!Addend) |
| 914 | return false; |
| 915 | |
| 916 | // The addend should be loop invariant |
| 917 | if (auto *I = dyn_cast<Instruction>(Addend)) |
| 918 | if (TheLoop->contains(I)) |
| 919 | return false; |
| 920 | |
| 921 | // FP Step has unknown SCEV |
| 922 | const SCEV *Step = SE->getUnknown(Addend); |
| 923 | D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp); |
| 924 | return true; |
| 925 | } |
| 926 | |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 927 | /// This function is called when we suspect that the update-chain of a phi node |
Simon Pilgrim | a74f4ae | 2018-04-06 17:01:54 +0000 | [diff] [blame] | 928 | /// (whose symbolic SCEV expression sin \p PhiScev) contains redundant casts, |
| 929 | /// that can be ignored. (This can happen when the PSCEV rewriter adds a runtime |
| 930 | /// predicate P under which the SCEV expression for the phi can be the |
| 931 | /// AddRecurrence \p AR; See createAddRecFromPHIWithCast). We want to find the |
| 932 | /// cast instructions that are involved in the update-chain of this induction. |
| 933 | /// A caller that adds the required runtime predicate can be free to drop these |
| 934 | /// cast instructions, and compute the phi using \p AR (instead of some scev |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 935 | /// expression with casts). |
| 936 | /// |
| 937 | /// For example, without a predicate the scev expression can take the following |
| 938 | /// form: |
| 939 | /// (Ext ix (Trunc iy ( Start + i*Step ) to ix) to iy) |
| 940 | /// |
| 941 | /// It corresponds to the following IR sequence: |
| 942 | /// %for.body: |
| 943 | /// %x = phi i64 [ 0, %ph ], [ %add, %for.body ] |
| 944 | /// %casted_phi = "ExtTrunc i64 %x" |
| 945 | /// %add = add i64 %casted_phi, %step |
| 946 | /// |
| 947 | /// where %x is given in \p PN, |
| 948 | /// PSE.getSCEV(%x) is equal to PSE.getSCEV(%casted_phi) under a predicate, |
| 949 | /// and the IR sequence that "ExtTrunc i64 %x" represents can take one of |
| 950 | /// several forms, for example, such as: |
| 951 | /// ExtTrunc1: %casted_phi = and %x, 2^n-1 |
| 952 | /// or: |
| 953 | /// ExtTrunc2: %t = shl %x, m |
| 954 | /// %casted_phi = ashr %t, m |
| 955 | /// |
| 956 | /// If we are able to find such sequence, we return the instructions |
| 957 | /// we found, namely %casted_phi and the instructions on its use-def chain up |
| 958 | /// to the phi (not including the phi). |
Benjamin Kramer | 802e625 | 2017-12-24 12:46:22 +0000 | [diff] [blame] | 959 | static bool getCastsForInductionPHI(PredicatedScalarEvolution &PSE, |
| 960 | const SCEVUnknown *PhiScev, |
| 961 | const SCEVAddRecExpr *AR, |
| 962 | SmallVectorImpl<Instruction *> &CastInsts) { |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 963 | |
| 964 | assert(CastInsts.empty() && "CastInsts is expected to be empty."); |
| 965 | auto *PN = cast<PHINode>(PhiScev->getValue()); |
| 966 | assert(PSE.getSCEV(PN) == AR && "Unexpected phi node SCEV expression"); |
| 967 | const Loop *L = AR->getLoop(); |
| 968 | |
Simon Pilgrim | a74f4ae | 2018-04-06 17:01:54 +0000 | [diff] [blame] | 969 | // Find any cast instructions that participate in the def-use chain of |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 970 | // PhiScev in the loop. |
| 971 | // FORNOW/TODO: We currently expect the def-use chain to include only |
| 972 | // two-operand instructions, where one of the operands is an invariant. |
| 973 | // createAddRecFromPHIWithCasts() currently does not support anything more |
| 974 | // involved than that, so we keep the search simple. This can be |
| 975 | // extended/generalized as needed. |
| 976 | |
| 977 | auto getDef = [&](const Value *Val) -> Value * { |
| 978 | const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val); |
| 979 | if (!BinOp) |
| 980 | return nullptr; |
| 981 | Value *Op0 = BinOp->getOperand(0); |
| 982 | Value *Op1 = BinOp->getOperand(1); |
| 983 | Value *Def = nullptr; |
| 984 | if (L->isLoopInvariant(Op0)) |
| 985 | Def = Op1; |
| 986 | else if (L->isLoopInvariant(Op1)) |
| 987 | Def = Op0; |
| 988 | return Def; |
| 989 | }; |
| 990 | |
| 991 | // Look for the instruction that defines the induction via the |
| 992 | // loop backedge. |
| 993 | BasicBlock *Latch = L->getLoopLatch(); |
| 994 | if (!Latch) |
| 995 | return false; |
| 996 | Value *Val = PN->getIncomingValueForBlock(Latch); |
| 997 | if (!Val) |
| 998 | return false; |
| 999 | |
| 1000 | // Follow the def-use chain until the induction phi is reached. |
| 1001 | // If on the way we encounter a Value that has the same SCEV Expr as the |
| 1002 | // phi node, we can consider the instructions we visit from that point |
| 1003 | // as part of the cast-sequence that can be ignored. |
| 1004 | bool InCastSequence = false; |
| 1005 | auto *Inst = dyn_cast<Instruction>(Val); |
| 1006 | while (Val != PN) { |
| 1007 | // If we encountered a phi node other than PN, or if we left the loop, |
| 1008 | // we bail out. |
| 1009 | if (!Inst || !L->contains(Inst)) { |
| 1010 | return false; |
| 1011 | } |
| 1012 | auto *AddRec = dyn_cast<SCEVAddRecExpr>(PSE.getSCEV(Val)); |
| 1013 | if (AddRec && PSE.areAddRecsEqualWithPreds(AddRec, AR)) |
| 1014 | InCastSequence = true; |
| 1015 | if (InCastSequence) { |
| 1016 | // Only the last instruction in the cast sequence is expected to have |
| 1017 | // uses outside the induction def-use chain. |
| 1018 | if (!CastInsts.empty()) |
| 1019 | if (!Inst->hasOneUse()) |
| 1020 | return false; |
| 1021 | CastInsts.push_back(Inst); |
| 1022 | } |
| 1023 | Val = getDef(Val); |
| 1024 | if (!Val) |
| 1025 | return false; |
| 1026 | Inst = dyn_cast<Instruction>(Val); |
| 1027 | } |
| 1028 | |
| 1029 | return InCastSequence; |
| 1030 | } |
| 1031 | |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 1032 | bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop, |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1033 | PredicatedScalarEvolution &PSE, |
| 1034 | InductionDescriptor &D, |
| 1035 | bool Assume) { |
| 1036 | Type *PhiTy = Phi->getType(); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Handle integer and pointer inductions variables. |
| 1039 | // Now we handle also FP induction but not trying to make a |
| 1040 | // recurrent expression from the PHI node in-place. |
| 1041 | |
| 1042 | if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() && |
| 1043 | !PhiTy->isFloatTy() && !PhiTy->isDoubleTy() && !PhiTy->isHalfTy()) |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1044 | return false; |
| 1045 | |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 1046 | if (PhiTy->isFloatingPointTy()) |
| 1047 | return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D); |
| 1048 | |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1049 | const SCEV *PhiScev = PSE.getSCEV(Phi); |
| 1050 | const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev); |
| 1051 | |
| 1052 | // We need this expression to be an AddRecExpr. |
| 1053 | if (Assume && !AR) |
| 1054 | AR = PSE.getAsAddRec(Phi); |
| 1055 | |
| 1056 | if (!AR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1057 | LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n"); |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1058 | return false; |
| 1059 | } |
| 1060 | |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 1061 | // Record any Cast instructions that participate in the induction update |
| 1062 | const auto *SymbolicPhi = dyn_cast<SCEVUnknown>(PhiScev); |
| 1063 | // If we started from an UnknownSCEV, and managed to build an addRecurrence |
| 1064 | // only after enabling Assume with PSCEV, this means we may have encountered |
| 1065 | // cast instructions that required adding a runtime check in order to |
| 1066 | // guarantee the correctness of the AddRecurence respresentation of the |
| 1067 | // induction. |
| 1068 | if (PhiScev != AR && SymbolicPhi) { |
| 1069 | SmallVector<Instruction *, 2> Casts; |
| 1070 | if (getCastsForInductionPHI(PSE, SymbolicPhi, AR, Casts)) |
| 1071 | return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR, &Casts); |
| 1072 | } |
| 1073 | |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 1074 | return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR); |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 1077 | bool InductionDescriptor::isInductionPHI( |
| 1078 | PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE, |
| 1079 | InductionDescriptor &D, const SCEV *Expr, |
| 1080 | SmallVectorImpl<Instruction *> *CastsToIgnore) { |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1081 | Type *PhiTy = Phi->getType(); |
| 1082 | // We only handle integer and pointer inductions variables. |
| 1083 | if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy()) |
| 1084 | return false; |
| 1085 | |
| 1086 | // Check that the PHI is consecutive. |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1087 | const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1088 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev); |
Silviu Baranga | c05bab8 | 2016-05-05 15:20:39 +0000 | [diff] [blame] | 1089 | |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1090 | if (!AR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1091 | LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n"); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
| 1094 | |
Michael Kuperstein | ee31cbe | 2017-01-10 19:32:30 +0000 | [diff] [blame] | 1095 | if (AR->getLoop() != TheLoop) { |
| 1096 | // FIXME: We should treat this as a uniform. Unfortunately, we |
| 1097 | // don't currently know how to handled uniform PHIs. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1098 | LLVM_DEBUG( |
| 1099 | dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n"); |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 1100 | return false; |
Michael Kuperstein | ee31cbe | 2017-01-10 19:32:30 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 1103 | Value *StartValue = |
| 1104 | Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader()); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1105 | const SCEV *Step = AR->getStepRecurrence(*SE); |
| 1106 | // Calculate the pointer stride and check if it is consecutive. |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 1107 | // The stride may be a constant or a loop invariant integer value. |
| 1108 | const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 1109 | if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop)) |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1110 | return false; |
| 1111 | |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1112 | if (PhiTy->isIntegerTy()) { |
Dorit Nuzman | 4750c78 | 2017-12-14 07:56:31 +0000 | [diff] [blame] | 1113 | D = InductionDescriptor(StartValue, IK_IntInduction, Step, /*BOp=*/ nullptr, |
| 1114 | CastsToIgnore); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | assert(PhiTy->isPointerTy() && "The PHI must be a pointer"); |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 1119 | // Pointer induction should be a constant. |
| 1120 | if (!ConstStep) |
| 1121 | return false; |
| 1122 | |
| 1123 | ConstantInt *CV = ConstStep->getValue(); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1124 | Type *PointerElementType = PhiTy->getPointerElementType(); |
| 1125 | // The pointer stride cannot be determined if the pointer element type is not |
| 1126 | // sized. |
| 1127 | if (!PointerElementType->isSized()) |
| 1128 | return false; |
| 1129 | |
| 1130 | const DataLayout &DL = Phi->getModule()->getDataLayout(); |
| 1131 | int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType)); |
David Majnemer | b58f32f | 2015-06-05 10:52:40 +0000 | [diff] [blame] | 1132 | if (!Size) |
| 1133 | return false; |
| 1134 | |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1135 | int64_t CVSize = CV->getSExtValue(); |
| 1136 | if (CVSize % Size) |
| 1137 | return false; |
Elena Demikhovsky | c434d09 | 2016-05-10 07:33:35 +0000 | [diff] [blame] | 1138 | auto *StepValue = SE->getConstant(CV->getType(), CVSize / Size, |
| 1139 | true /* signed */); |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 1140 | D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue); |
Karthik Bhat | 24e6cc2 | 2015-04-23 08:29:20 +0000 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
Ashutosh Nema | c5b7b55 | 2015-08-19 05:40:42 +0000 | [diff] [blame] | 1143 | |
Chandler Carruth | 4a00088 | 2017-06-25 22:45:31 +0000 | [diff] [blame] | 1144 | bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, |
| 1145 | bool PreserveLCSSA) { |
| 1146 | bool Changed = false; |
| 1147 | |
| 1148 | // We re-use a vector for the in-loop predecesosrs. |
| 1149 | SmallVector<BasicBlock *, 4> InLoopPredecessors; |
| 1150 | |
| 1151 | auto RewriteExit = [&](BasicBlock *BB) { |
| 1152 | assert(InLoopPredecessors.empty() && |
| 1153 | "Must start with an empty predecessors list!"); |
| 1154 | auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); }); |
| 1155 | |
| 1156 | // See if there are any non-loop predecessors of this exit block and |
| 1157 | // keep track of the in-loop predecessors. |
| 1158 | bool IsDedicatedExit = true; |
| 1159 | for (auto *PredBB : predecessors(BB)) |
| 1160 | if (L->contains(PredBB)) { |
| 1161 | if (isa<IndirectBrInst>(PredBB->getTerminator())) |
| 1162 | // We cannot rewrite exiting edges from an indirectbr. |
| 1163 | return false; |
| 1164 | |
| 1165 | InLoopPredecessors.push_back(PredBB); |
| 1166 | } else { |
| 1167 | IsDedicatedExit = false; |
| 1168 | } |
| 1169 | |
| 1170 | assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!"); |
| 1171 | |
| 1172 | // Nothing to do if this is already a dedicated exit. |
| 1173 | if (IsDedicatedExit) |
| 1174 | return false; |
| 1175 | |
| 1176 | auto *NewExitBB = SplitBlockPredecessors( |
| 1177 | BB, InLoopPredecessors, ".loopexit", DT, LI, PreserveLCSSA); |
| 1178 | |
| 1179 | if (!NewExitBB) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1180 | LLVM_DEBUG( |
| 1181 | dbgs() << "WARNING: Can't create a dedicated exit block for loop: " |
| 1182 | << *L << "\n"); |
Chandler Carruth | 4a00088 | 2017-06-25 22:45:31 +0000 | [diff] [blame] | 1183 | else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1184 | LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block " |
| 1185 | << NewExitBB->getName() << "\n"); |
Chandler Carruth | 4a00088 | 2017-06-25 22:45:31 +0000 | [diff] [blame] | 1186 | return true; |
| 1187 | }; |
| 1188 | |
| 1189 | // Walk the exit blocks directly rather than building up a data structure for |
| 1190 | // them, but only visit each one once. |
| 1191 | SmallPtrSet<BasicBlock *, 4> Visited; |
| 1192 | for (auto *BB : L->blocks()) |
| 1193 | for (auto *SuccBB : successors(BB)) { |
| 1194 | // We're looking for exit blocks so skip in-loop successors. |
| 1195 | if (L->contains(SuccBB)) |
| 1196 | continue; |
| 1197 | |
| 1198 | // Visit each exit block exactly once. |
| 1199 | if (!Visited.insert(SuccBB).second) |
| 1200 | continue; |
| 1201 | |
| 1202 | Changed |= RewriteExit(SuccBB); |
| 1203 | } |
| 1204 | |
| 1205 | return Changed; |
| 1206 | } |
| 1207 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1208 | /// Returns the instructions that use values defined in the loop. |
Ashutosh Nema | c5b7b55 | 2015-08-19 05:40:42 +0000 | [diff] [blame] | 1209 | SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) { |
| 1210 | SmallVector<Instruction *, 8> UsedOutside; |
| 1211 | |
| 1212 | for (auto *Block : L->getBlocks()) |
| 1213 | // FIXME: I believe that this could use copy_if if the Inst reference could |
| 1214 | // be adapted into a pointer. |
| 1215 | for (auto &Inst : *Block) { |
| 1216 | auto Users = Inst.users(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 1217 | if (any_of(Users, [&](User *U) { |
Ashutosh Nema | c5b7b55 | 2015-08-19 05:40:42 +0000 | [diff] [blame] | 1218 | auto *Use = cast<Instruction>(U); |
| 1219 | return !L->contains(Use->getParent()); |
| 1220 | })) |
| 1221 | UsedOutside.push_back(&Inst); |
| 1222 | } |
| 1223 | |
| 1224 | return UsedOutside; |
| 1225 | } |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 1226 | |
| 1227 | void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) { |
| 1228 | // By definition, all loop passes need the LoopInfo analysis and the |
| 1229 | // Dominator tree it depends on. Because they all participate in the loop |
| 1230 | // pass manager, they must also preserve these. |
| 1231 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 1232 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 1233 | AU.addRequired<LoopInfoWrapperPass>(); |
| 1234 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 1235 | |
| 1236 | // We must also preserve LoopSimplify and LCSSA. We locally access their IDs |
| 1237 | // here because users shouldn't directly get them from this header. |
| 1238 | extern char &LoopSimplifyID; |
| 1239 | extern char &LCSSAID; |
| 1240 | AU.addRequiredID(LoopSimplifyID); |
| 1241 | AU.addPreservedID(LoopSimplifyID); |
| 1242 | AU.addRequiredID(LCSSAID); |
| 1243 | AU.addPreservedID(LCSSAID); |
Igor Laevsky | c3ccf5d | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 1244 | // This is used in the LPPassManager to perform LCSSA verification on passes |
| 1245 | // which preserve lcssa form |
| 1246 | AU.addRequired<LCSSAVerificationPass>(); |
| 1247 | AU.addPreserved<LCSSAVerificationPass>(); |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Loop passes are designed to run inside of a loop pass manager which means |
| 1250 | // that any function analyses they require must be required by the first loop |
| 1251 | // pass in the manager (so that it is computed before the loop pass manager |
| 1252 | // runs) and preserved by all loop pasess in the manager. To make this |
| 1253 | // reasonably robust, the set needed for most loop passes is maintained here. |
| 1254 | // If your loop pass requires an analysis not listed here, you will need to |
| 1255 | // carefully audit the loop pass manager nesting structure that results. |
| 1256 | AU.addRequired<AAResultsWrapperPass>(); |
| 1257 | AU.addPreserved<AAResultsWrapperPass>(); |
| 1258 | AU.addPreserved<BasicAAWrapperPass>(); |
| 1259 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 1260 | AU.addPreserved<SCEVAAWrapperPass>(); |
| 1261 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 1262 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
| 1263 | } |
| 1264 | |
| 1265 | /// Manually defined generic "LoopPass" dependency initialization. This is used |
| 1266 | /// to initialize the exact set of passes from above in \c |
| 1267 | /// getLoopAnalysisUsage. It can be used within a loop pass's initialization |
| 1268 | /// with: |
| 1269 | /// |
| 1270 | /// INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 1271 | /// |
| 1272 | /// As-if "LoopPass" were a pass. |
| 1273 | void llvm::initializeLoopPassPass(PassRegistry &Registry) { |
| 1274 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1275 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 1276 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 1277 | INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 1278 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 1279 | INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) |
| 1280 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 1281 | INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) |
| 1282 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| 1283 | } |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1284 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1285 | /// Find string metadata for loop |
Adam Nemet | fe3def7 | 2016-04-22 19:10:05 +0000 | [diff] [blame] | 1286 | /// |
| 1287 | /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an |
| 1288 | /// operand or null otherwise. If the string metadata is not found return |
| 1289 | /// Optional's not-a-value. |
| 1290 | Optional<const MDOperand *> llvm::findStringMetadataForLoop(Loop *TheLoop, |
| 1291 | StringRef Name) { |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1292 | MDNode *LoopID = TheLoop->getLoopID(); |
Adam Nemet | fe3def7 | 2016-04-22 19:10:05 +0000 | [diff] [blame] | 1293 | // Return none if LoopID is false. |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1294 | if (!LoopID) |
Adam Nemet | fe3def7 | 2016-04-22 19:10:05 +0000 | [diff] [blame] | 1295 | return None; |
Adam Nemet | 293be66 | 2016-04-21 17:33:20 +0000 | [diff] [blame] | 1296 | |
| 1297 | // First operand should refer to the loop id itself. |
| 1298 | assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); |
| 1299 | assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); |
| 1300 | |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1301 | // Iterate over LoopID operands and look for MDString Metadata |
| 1302 | for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { |
| 1303 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 1304 | if (!MD) |
| 1305 | continue; |
| 1306 | MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 1307 | if (!S) |
| 1308 | continue; |
| 1309 | // Return true if MDString holds expected MetaData. |
| 1310 | if (Name.equals(S->getString())) |
Adam Nemet | fe3def7 | 2016-04-22 19:10:05 +0000 | [diff] [blame] | 1311 | switch (MD->getNumOperands()) { |
| 1312 | case 1: |
| 1313 | return nullptr; |
| 1314 | case 2: |
| 1315 | return &MD->getOperand(1); |
| 1316 | default: |
| 1317 | llvm_unreachable("loop metadata has 0 or 1 operand"); |
| 1318 | } |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1319 | } |
Adam Nemet | fe3def7 | 2016-04-22 19:10:05 +0000 | [diff] [blame] | 1320 | return None; |
Adam Nemet | 963341c | 2016-04-21 17:33:17 +0000 | [diff] [blame] | 1321 | } |
Evgeniy Stepanov | 122f984 | 2016-06-10 20:03:17 +0000 | [diff] [blame] | 1322 | |
Alina Sbirlea | 7ed5856 | 2017-09-15 00:04:16 +0000 | [diff] [blame] | 1323 | /// Does a BFS from a given node to all of its children inside a given loop. |
| 1324 | /// The returned vector of nodes includes the starting point. |
| 1325 | SmallVector<DomTreeNode *, 16> |
| 1326 | llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) { |
| 1327 | SmallVector<DomTreeNode *, 16> Worklist; |
| 1328 | auto AddRegionToWorklist = [&](DomTreeNode *DTN) { |
| 1329 | // Only include subregions in the top level loop. |
| 1330 | BasicBlock *BB = DTN->getBlock(); |
| 1331 | if (CurLoop->contains(BB)) |
| 1332 | Worklist.push_back(DTN); |
| 1333 | }; |
| 1334 | |
| 1335 | AddRegionToWorklist(N); |
| 1336 | |
| 1337 | for (size_t I = 0; I < Worklist.size(); I++) |
| 1338 | for (DomTreeNode *Child : Worklist[I]->getChildren()) |
| 1339 | AddRegionToWorklist(Child); |
| 1340 | |
| 1341 | return Worklist; |
| 1342 | } |
| 1343 | |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1344 | void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT = nullptr, |
| 1345 | ScalarEvolution *SE = nullptr, |
| 1346 | LoopInfo *LI = nullptr) { |
Hans Wennborg | 899809d | 2017-10-04 21:14:07 +0000 | [diff] [blame] | 1347 | assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!"); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1348 | auto *Preheader = L->getLoopPreheader(); |
| 1349 | assert(Preheader && "Preheader should exist!"); |
| 1350 | |
| 1351 | // Now that we know the removal is safe, remove the loop by changing the |
| 1352 | // branch from the preheader to go to the single exit block. |
| 1353 | // |
| 1354 | // Because we're deleting a large chunk of code at once, the sequence in which |
| 1355 | // we remove things is very important to avoid invalidation issues. |
| 1356 | |
| 1357 | // Tell ScalarEvolution that the loop is deleted. Do this before |
| 1358 | // deleting the loop so that ScalarEvolution can look at the loop |
| 1359 | // to determine what it needs to clean up. |
| 1360 | if (SE) |
| 1361 | SE->forgetLoop(L); |
| 1362 | |
| 1363 | auto *ExitBlock = L->getUniqueExitBlock(); |
| 1364 | assert(ExitBlock && "Should have a unique exit block!"); |
| 1365 | assert(L->hasDedicatedExits() && "Loop should have dedicated exits!"); |
| 1366 | |
| 1367 | auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator()); |
| 1368 | assert(OldBr && "Preheader must end with a branch"); |
| 1369 | assert(OldBr->isUnconditional() && "Preheader must have a single successor"); |
| 1370 | // Connect the preheader to the exit block. Keep the old edge to the header |
| 1371 | // around to perform the dominator tree update in two separate steps |
| 1372 | // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge |
| 1373 | // preheader -> header. |
| 1374 | // |
| 1375 | // |
| 1376 | // 0. Preheader 1. Preheader 2. Preheader |
| 1377 | // | | | | |
| 1378 | // V | V | |
| 1379 | // Header <--\ | Header <--\ | Header <--\ |
| 1380 | // | | | | | | | | | | | |
| 1381 | // | V | | | V | | | V | |
| 1382 | // | Body --/ | | Body --/ | | Body --/ |
| 1383 | // V V V V V |
| 1384 | // Exit Exit Exit |
| 1385 | // |
| 1386 | // By doing this is two separate steps we can perform the dominator tree |
| 1387 | // update without using the batch update API. |
| 1388 | // |
| 1389 | // Even when the loop is never executed, we cannot remove the edge from the |
| 1390 | // source block to the exit block. Consider the case where the unexecuted loop |
| 1391 | // branches back to an outer loop. If we deleted the loop and removed the edge |
| 1392 | // coming to this inner loop, this will break the outer loop structure (by |
| 1393 | // deleting the backedge of the outer loop). If the outer loop is indeed a |
| 1394 | // non-loop, it will be deleted in a future iteration of loop deletion pass. |
| 1395 | IRBuilder<> Builder(OldBr); |
| 1396 | Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock); |
| 1397 | // Remove the old branch. The conditional branch becomes a new terminator. |
| 1398 | OldBr->eraseFromParent(); |
| 1399 | |
| 1400 | // Rewrite phis in the exit block to get their inputs from the Preheader |
| 1401 | // instead of the exiting block. |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1402 | for (PHINode &P : ExitBlock->phis()) { |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1403 | // Set the zero'th element of Phi to be from the preheader and remove all |
| 1404 | // other incoming values. Given the loop has dedicated exits, all other |
| 1405 | // incoming values must be from the exiting blocks. |
| 1406 | int PredIndex = 0; |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1407 | P.setIncomingBlock(PredIndex, Preheader); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1408 | // Removes all incoming values from all other exiting blocks (including |
| 1409 | // duplicate values from an exiting block). |
| 1410 | // Nuke all entries except the zero'th entry which is the preheader entry. |
| 1411 | // NOTE! We need to remove Incoming Values in the reverse order as done |
| 1412 | // below, to keep the indices valid for deletion (removeIncomingValues |
| 1413 | // updates getNumIncomingValues and shifts all values down into the operand |
| 1414 | // being deleted). |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1415 | for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i) |
| 1416 | P.removeIncomingValue(e - i, false); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1417 | |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 1418 | assert((P.getNumIncomingValues() == 1 && |
| 1419 | P.getIncomingBlock(PredIndex) == Preheader) && |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1420 | "Should have exactly one value and that's from the preheader!"); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | // Disconnect the loop body by branching directly to its exit. |
| 1424 | Builder.SetInsertPoint(Preheader->getTerminator()); |
| 1425 | Builder.CreateBr(ExitBlock); |
| 1426 | // Remove the old branch. |
| 1427 | Preheader->getTerminator()->eraseFromParent(); |
| 1428 | |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame^] | 1429 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1430 | if (DT) { |
| 1431 | // Update the dominator tree by informing it about the new edge from the |
| 1432 | // preheader to the exit. |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame^] | 1433 | DTU.insertEdge(Preheader, ExitBlock); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1434 | // Inform the dominator tree about the removed edge. |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame^] | 1435 | DTU.deleteEdge(Preheader, L->getHeader()); |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Serguei Katkov | a757d65 | 2018-01-12 07:24:43 +0000 | [diff] [blame] | 1438 | // Given LCSSA form is satisfied, we should not have users of instructions |
| 1439 | // within the dead loop outside of the loop. However, LCSSA doesn't take |
| 1440 | // unreachable uses into account. We handle them here. |
| 1441 | // We could do it after drop all references (in this case all users in the |
| 1442 | // loop will be already eliminated and we have less work to do but according |
| 1443 | // to API doc of User::dropAllReferences only valid operation after dropping |
| 1444 | // references, is deletion. So let's substitute all usages of |
| 1445 | // instruction from the loop with undef value of corresponding type first. |
| 1446 | for (auto *Block : L->blocks()) |
| 1447 | for (Instruction &I : *Block) { |
| 1448 | auto *Undef = UndefValue::get(I.getType()); |
| 1449 | for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E;) { |
| 1450 | Use &U = *UI; |
| 1451 | ++UI; |
| 1452 | if (auto *Usr = dyn_cast<Instruction>(U.getUser())) |
| 1453 | if (L->contains(Usr->getParent())) |
| 1454 | continue; |
| 1455 | // If we have a DT then we can check that uses outside a loop only in |
| 1456 | // unreachable block. |
| 1457 | if (DT) |
| 1458 | assert(!DT->isReachableFromEntry(U) && |
| 1459 | "Unexpected user in reachable block"); |
| 1460 | U.set(Undef); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Marcello Maggioni | df3e71e | 2017-10-04 20:42:46 +0000 | [diff] [blame] | 1464 | // Remove the block from the reference counting scheme, so that we can |
| 1465 | // delete it freely later. |
| 1466 | for (auto *Block : L->blocks()) |
| 1467 | Block->dropAllReferences(); |
| 1468 | |
| 1469 | if (LI) { |
| 1470 | // Erase the instructions and the blocks without having to worry |
| 1471 | // about ordering because we already dropped the references. |
| 1472 | // NOTE: This iteration is safe because erasing the block does not remove |
| 1473 | // its entry from the loop's block list. We do that in the next section. |
| 1474 | for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end(); |
| 1475 | LpI != LpE; ++LpI) |
| 1476 | (*LpI)->eraseFromParent(); |
| 1477 | |
| 1478 | // Finally, the blocks from loopinfo. This has to happen late because |
| 1479 | // otherwise our loop iterators won't work. |
| 1480 | |
| 1481 | SmallPtrSet<BasicBlock *, 8> blocks; |
| 1482 | blocks.insert(L->block_begin(), L->block_end()); |
| 1483 | for (BasicBlock *BB : blocks) |
| 1484 | LI->removeBlock(BB); |
| 1485 | |
| 1486 | // The last step is to update LoopInfo now that we've eliminated this loop. |
| 1487 | LI->erase(L); |
| 1488 | } |
| 1489 | } |
| 1490 | |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1491 | Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) { |
| 1492 | // Only support loops with a unique exiting block, and a latch. |
| 1493 | if (!L->getExitingBlock()) |
| 1494 | return None; |
| 1495 | |
Hiroshi Inoue | d24ddcd | 2018-01-19 10:55:29 +0000 | [diff] [blame] | 1496 | // Get the branch weights for the loop's backedge. |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1497 | BranchInst *LatchBR = |
| 1498 | dyn_cast<BranchInst>(L->getLoopLatch()->getTerminator()); |
| 1499 | if (!LatchBR || LatchBR->getNumSuccessors() != 2) |
| 1500 | return None; |
| 1501 | |
| 1502 | assert((LatchBR->getSuccessor(0) == L->getHeader() || |
| 1503 | LatchBR->getSuccessor(1) == L->getHeader()) && |
| 1504 | "At least one edge out of the latch must go to the header"); |
| 1505 | |
| 1506 | // To estimate the number of times the loop body was executed, we want to |
| 1507 | // know the number of times the backedge was taken, vs. the number of times |
| 1508 | // we exited the loop. |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1509 | uint64_t TrueVal, FalseVal; |
Michael Kuperstein | b151a64 | 2016-11-30 21:13:57 +0000 | [diff] [blame] | 1510 | if (!LatchBR->extractProfMetadata(TrueVal, FalseVal)) |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1511 | return None; |
| 1512 | |
Michael Kuperstein | b151a64 | 2016-11-30 21:13:57 +0000 | [diff] [blame] | 1513 | if (!TrueVal || !FalseVal) |
| 1514 | return 0; |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1515 | |
Michael Kuperstein | b151a64 | 2016-11-30 21:13:57 +0000 | [diff] [blame] | 1516 | // Divide the count of the backedge by the count of the edge exiting the loop, |
| 1517 | // rounding to nearest. |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1518 | if (LatchBR->getSuccessor(0) == L->getHeader()) |
Michael Kuperstein | b151a64 | 2016-11-30 21:13:57 +0000 | [diff] [blame] | 1519 | return (TrueVal + (FalseVal / 2)) / FalseVal; |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1520 | else |
Michael Kuperstein | b151a64 | 2016-11-30 21:13:57 +0000 | [diff] [blame] | 1521 | return (FalseVal + (TrueVal / 2)) / TrueVal; |
Dehao Chen | 41d72a8 | 2016-11-17 01:17:02 +0000 | [diff] [blame] | 1522 | } |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1523 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1524 | /// Adds a 'fast' flag to floating point operations. |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1525 | static Value *addFastMathFlag(Value *V) { |
| 1526 | if (isa<FPMathOperator>(V)) { |
| 1527 | FastMathFlags Flags; |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 1528 | Flags.setFast(); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1529 | cast<Instruction>(V)->setFastMathFlags(Flags); |
| 1530 | } |
| 1531 | return V; |
| 1532 | } |
| 1533 | |
Simon Pilgrim | 23c2182 | 2018-04-09 15:44:20 +0000 | [diff] [blame] | 1534 | // Helper to generate an ordered reduction. |
| 1535 | Value * |
| 1536 | llvm::getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src, |
| 1537 | unsigned Op, |
| 1538 | RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind, |
| 1539 | ArrayRef<Value *> RedOps) { |
| 1540 | unsigned VF = Src->getType()->getVectorNumElements(); |
| 1541 | |
| 1542 | // Extract and apply reduction ops in ascending order: |
| 1543 | // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1] |
| 1544 | Value *Result = Acc; |
| 1545 | for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) { |
| 1546 | Value *Ext = |
| 1547 | Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx)); |
| 1548 | |
| 1549 | if (Op != Instruction::ICmp && Op != Instruction::FCmp) { |
| 1550 | Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext, |
| 1551 | "bin.rdx"); |
| 1552 | } else { |
| 1553 | assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid && |
| 1554 | "Invalid min/max"); |
| 1555 | Result = RecurrenceDescriptor::createMinMaxOp(Builder, MinMaxKind, Result, |
| 1556 | Ext); |
| 1557 | } |
| 1558 | |
| 1559 | if (!RedOps.empty()) |
| 1560 | propagateIRFlags(Result, RedOps); |
| 1561 | } |
| 1562 | |
| 1563 | return Result; |
| 1564 | } |
| 1565 | |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1566 | // Helper to generate a log2 shuffle reduction. |
Amara Emerson | 836b0f4 | 2017-05-10 09:42:49 +0000 | [diff] [blame] | 1567 | Value * |
| 1568 | llvm::getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, |
| 1569 | RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind, |
| 1570 | ArrayRef<Value *> RedOps) { |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1571 | unsigned VF = Src->getType()->getVectorNumElements(); |
| 1572 | // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles |
| 1573 | // and vector ops, reducing the set of values being computed by half each |
| 1574 | // round. |
| 1575 | assert(isPowerOf2_32(VF) && |
| 1576 | "Reduction emission only supported for pow2 vectors!"); |
| 1577 | Value *TmpVec = Src; |
| 1578 | SmallVector<Constant *, 32> ShuffleMask(VF, nullptr); |
| 1579 | for (unsigned i = VF; i != 1; i >>= 1) { |
| 1580 | // Move the upper half of the vector to the lower half. |
| 1581 | for (unsigned j = 0; j != i / 2; ++j) |
| 1582 | ShuffleMask[j] = Builder.getInt32(i / 2 + j); |
| 1583 | |
| 1584 | // Fill the rest of the mask with undef. |
| 1585 | std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), |
| 1586 | UndefValue::get(Builder.getInt32Ty())); |
| 1587 | |
| 1588 | Value *Shuf = Builder.CreateShuffleVector( |
| 1589 | TmpVec, UndefValue::get(TmpVec->getType()), |
| 1590 | ConstantVector::get(ShuffleMask), "rdx.shuf"); |
| 1591 | |
| 1592 | if (Op != Instruction::ICmp && Op != Instruction::FCmp) { |
| 1593 | // Floating point operations had to be 'fast' to enable the reduction. |
| 1594 | TmpVec = addFastMathFlag(Builder.CreateBinOp((Instruction::BinaryOps)Op, |
| 1595 | TmpVec, Shuf, "bin.rdx")); |
| 1596 | } else { |
| 1597 | assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid && |
| 1598 | "Invalid min/max"); |
| 1599 | TmpVec = RecurrenceDescriptor::createMinMaxOp(Builder, MinMaxKind, TmpVec, |
| 1600 | Shuf); |
| 1601 | } |
| 1602 | if (!RedOps.empty()) |
| 1603 | propagateIRFlags(TmpVec, RedOps); |
| 1604 | } |
| 1605 | // The result is in the first element of the vector. |
| 1606 | return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0)); |
| 1607 | } |
| 1608 | |
| 1609 | /// Create a simple vector reduction specified by an opcode and some |
| 1610 | /// flags (if generating min/max reductions). |
| 1611 | Value *llvm::createSimpleTargetReduction( |
| 1612 | IRBuilder<> &Builder, const TargetTransformInfo *TTI, unsigned Opcode, |
| 1613 | Value *Src, TargetTransformInfo::ReductionFlags Flags, |
| 1614 | ArrayRef<Value *> RedOps) { |
| 1615 | assert(isa<VectorType>(Src->getType()) && "Type must be a vector"); |
| 1616 | |
| 1617 | Value *ScalarUdf = UndefValue::get(Src->getType()->getVectorElementType()); |
| 1618 | std::function<Value*()> BuildFunc; |
| 1619 | using RD = RecurrenceDescriptor; |
| 1620 | RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid; |
| 1621 | // TODO: Support creating ordered reductions. |
Sanjay Patel | 1ea7b6f | 2017-12-06 19:11:23 +0000 | [diff] [blame] | 1622 | FastMathFlags FMFFast; |
| 1623 | FMFFast.setFast(); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1624 | |
| 1625 | switch (Opcode) { |
| 1626 | case Instruction::Add: |
| 1627 | BuildFunc = [&]() { return Builder.CreateAddReduce(Src); }; |
| 1628 | break; |
| 1629 | case Instruction::Mul: |
| 1630 | BuildFunc = [&]() { return Builder.CreateMulReduce(Src); }; |
| 1631 | break; |
| 1632 | case Instruction::And: |
| 1633 | BuildFunc = [&]() { return Builder.CreateAndReduce(Src); }; |
| 1634 | break; |
| 1635 | case Instruction::Or: |
| 1636 | BuildFunc = [&]() { return Builder.CreateOrReduce(Src); }; |
| 1637 | break; |
| 1638 | case Instruction::Xor: |
| 1639 | BuildFunc = [&]() { return Builder.CreateXorReduce(Src); }; |
| 1640 | break; |
| 1641 | case Instruction::FAdd: |
| 1642 | BuildFunc = [&]() { |
| 1643 | auto Rdx = Builder.CreateFAddReduce(ScalarUdf, Src); |
Sanjay Patel | 1ea7b6f | 2017-12-06 19:11:23 +0000 | [diff] [blame] | 1644 | cast<CallInst>(Rdx)->setFastMathFlags(FMFFast); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1645 | return Rdx; |
| 1646 | }; |
| 1647 | break; |
| 1648 | case Instruction::FMul: |
| 1649 | BuildFunc = [&]() { |
| 1650 | auto Rdx = Builder.CreateFMulReduce(ScalarUdf, Src); |
Sanjay Patel | 1ea7b6f | 2017-12-06 19:11:23 +0000 | [diff] [blame] | 1651 | cast<CallInst>(Rdx)->setFastMathFlags(FMFFast); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1652 | return Rdx; |
| 1653 | }; |
| 1654 | break; |
| 1655 | case Instruction::ICmp: |
| 1656 | if (Flags.IsMaxOp) { |
| 1657 | MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax; |
| 1658 | BuildFunc = [&]() { |
| 1659 | return Builder.CreateIntMaxReduce(Src, Flags.IsSigned); |
| 1660 | }; |
| 1661 | } else { |
| 1662 | MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin; |
| 1663 | BuildFunc = [&]() { |
| 1664 | return Builder.CreateIntMinReduce(Src, Flags.IsSigned); |
| 1665 | }; |
| 1666 | } |
| 1667 | break; |
| 1668 | case Instruction::FCmp: |
| 1669 | if (Flags.IsMaxOp) { |
| 1670 | MinMaxKind = RD::MRK_FloatMax; |
| 1671 | BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); }; |
| 1672 | } else { |
| 1673 | MinMaxKind = RD::MRK_FloatMin; |
| 1674 | BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); }; |
| 1675 | } |
| 1676 | break; |
| 1677 | default: |
| 1678 | llvm_unreachable("Unhandled opcode"); |
| 1679 | break; |
| 1680 | } |
| 1681 | if (TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags)) |
| 1682 | return BuildFunc(); |
| 1683 | return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps); |
| 1684 | } |
| 1685 | |
| 1686 | /// Create a vector reduction using a given recurrence descriptor. |
Sanjay Patel | 3e069f5 | 2017-12-06 19:37:00 +0000 | [diff] [blame] | 1687 | Value *llvm::createTargetReduction(IRBuilder<> &B, |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1688 | const TargetTransformInfo *TTI, |
| 1689 | RecurrenceDescriptor &Desc, Value *Src, |
| 1690 | bool NoNaN) { |
| 1691 | // TODO: Support in-order reductions based on the recurrence descriptor. |
Sanjay Patel | 3e069f5 | 2017-12-06 19:37:00 +0000 | [diff] [blame] | 1692 | using RD = RecurrenceDescriptor; |
| 1693 | RD::RecurrenceKind RecKind = Desc.getRecurrenceKind(); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1694 | TargetTransformInfo::ReductionFlags Flags; |
| 1695 | Flags.NoNaN = NoNaN; |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1696 | switch (RecKind) { |
Sanjay Patel | 3e069f5 | 2017-12-06 19:37:00 +0000 | [diff] [blame] | 1697 | case RD::RK_FloatAdd: |
| 1698 | return createSimpleTargetReduction(B, TTI, Instruction::FAdd, Src, Flags); |
| 1699 | case RD::RK_FloatMult: |
| 1700 | return createSimpleTargetReduction(B, TTI, Instruction::FMul, Src, Flags); |
| 1701 | case RD::RK_IntegerAdd: |
| 1702 | return createSimpleTargetReduction(B, TTI, Instruction::Add, Src, Flags); |
| 1703 | case RD::RK_IntegerMult: |
| 1704 | return createSimpleTargetReduction(B, TTI, Instruction::Mul, Src, Flags); |
| 1705 | case RD::RK_IntegerAnd: |
| 1706 | return createSimpleTargetReduction(B, TTI, Instruction::And, Src, Flags); |
| 1707 | case RD::RK_IntegerOr: |
| 1708 | return createSimpleTargetReduction(B, TTI, Instruction::Or, Src, Flags); |
| 1709 | case RD::RK_IntegerXor: |
| 1710 | return createSimpleTargetReduction(B, TTI, Instruction::Xor, Src, Flags); |
| 1711 | case RD::RK_IntegerMinMax: { |
| 1712 | RD::MinMaxRecurrenceKind MMKind = Desc.getMinMaxRecurrenceKind(); |
| 1713 | Flags.IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax); |
| 1714 | Flags.IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin); |
| 1715 | return createSimpleTargetReduction(B, TTI, Instruction::ICmp, Src, Flags); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1716 | } |
Sanjay Patel | 3e069f5 | 2017-12-06 19:37:00 +0000 | [diff] [blame] | 1717 | case RD::RK_FloatMinMax: { |
| 1718 | Flags.IsMaxOp = Desc.getMinMaxRecurrenceKind() == RD::MRK_FloatMax; |
| 1719 | return createSimpleTargetReduction(B, TTI, Instruction::FCmp, Src, Flags); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1720 | } |
| 1721 | default: |
| 1722 | llvm_unreachable("Unhandled RecKind"); |
| 1723 | } |
| 1724 | } |
| 1725 | |
Dinar Temirbulatov | a61f4b8 | 2017-07-19 10:02:07 +0000 | [diff] [blame] | 1726 | void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) { |
| 1727 | auto *VecOp = dyn_cast<Instruction>(I); |
| 1728 | if (!VecOp) |
| 1729 | return; |
| 1730 | auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0]) |
| 1731 | : dyn_cast<Instruction>(OpValue); |
| 1732 | if (!Intersection) |
| 1733 | return; |
| 1734 | const unsigned Opcode = Intersection->getOpcode(); |
| 1735 | VecOp->copyIRFlags(Intersection); |
| 1736 | for (auto *V : VL) { |
| 1737 | auto *Instr = dyn_cast<Instruction>(V); |
| 1738 | if (!Instr) |
| 1739 | continue; |
| 1740 | if (OpValue == nullptr || Opcode == Instr->getOpcode()) |
| 1741 | VecOp->andIRFlags(V); |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |