blob: 0154175cfe8c37dc49284624d35211fd23826f5e [file] [log] [blame]
Vikram TV7e98d692018-09-12 01:59:43 +00001//===- llvm/Analysis/IVDescriptors.cpp - IndVar Descriptors -----*- C++ -*-===//
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 "describes" induction and recurrence variables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/IVDescriptors.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/Analysis/AliasAnalysis.h"
17#include "llvm/Analysis/BasicAliasAnalysis.h"
18#include "llvm/Analysis/GlobalsModRef.h"
19#include "llvm/Analysis/InstructionSimplify.h"
20#include "llvm/Analysis/LoopInfo.h"
21#include "llvm/Analysis/LoopPass.h"
22#include "llvm/Analysis/MustExecute.h"
23#include "llvm/Analysis/ScalarEvolution.h"
24#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
25#include "llvm/Analysis/ScalarEvolutionExpander.h"
26#include "llvm/Analysis/ScalarEvolutionExpressions.h"
27#include "llvm/Analysis/TargetTransformInfo.h"
28#include "llvm/Analysis/ValueTracking.h"
29#include "llvm/IR/DomTreeUpdater.h"
30#include "llvm/IR/Dominators.h"
31#include "llvm/IR/Instructions.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/PatternMatch.h"
34#include "llvm/IR/ValueHandle.h"
35#include "llvm/Pass.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/KnownBits.h"
38#include "llvm/Transforms/Utils/BasicBlockUtils.h"
39
40using namespace llvm;
41using namespace llvm::PatternMatch;
42
43#define DEBUG_TYPE "iv-descriptors"
44
45bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
46 SmallPtrSetImpl<Instruction *> &Set) {
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
53bool 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
68bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurrenceKind Kind) {
69 return (Kind != RK_NoRecurrence) && !isIntegerRecurrenceKind(Kind);
70}
71
72bool 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
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.
89static Instruction *lookThroughAnd(PHINode *Phi, Type *&RT,
90 SmallPtrSetImpl<Instruction *> &Visited,
91 SmallPtrSetImpl<Instruction *> &CI) {
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.
100 if (match(J, m_c_And(m_Instruction(I), m_APInt(M)))) {
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
112/// Compute the minimal bit width needed to represent a reduction whose exit
113/// instruction is given by Exit.
114static 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());
121
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 }
131
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;
151 }
152 }
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.
163static 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 }
190}
191
192bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
193 Loop *TheLoop, bool HasFunNoNaNAttr,
194 RecurrenceDescriptor &RedDes,
195 DemandedBits *DB,
196 AssumptionCache *AC,
197 DominatorTree *DT) {
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;
227 InstDesc ReduxDesc(false, nullptr);
228
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
235 SmallPtrSet<Instruction *, 8> VisitedInsts;
236 SmallVector<Instruction *, 8> Worklist;
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);
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.
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.
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
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 }
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.
319 FoundReduxOp |= !IsAPhi && Cur != Start;
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)) {
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.
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.
347 if (!is_contained(Phi->operands(), Cur))
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.
357 InstDesc IgnoredVal(false, nullptr);
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)) ||
366 !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
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
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)
415 return false;
416
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
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)
433 // is saved as part of the RecurrenceDescriptor.
434
435 // Save the description of this reduction variable.
436 RecurrenceDescriptor RD(
437 RdxStart, ExitInstruction, Kind, ReduxDesc.getMinMaxKind(),
438 ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType, IsSigned, CastInsts);
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).
446RecurrenceDescriptor::InstDesc
447RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) {
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())))
458 return InstDesc(false, I);
459 return InstDesc(Select, Prev.getMinMaxKind());
460 }
461
462 // Only handle single use cases for now.
463 if (!(Select = dyn_cast<SelectInst>(I)))
464 return InstDesc(false, I);
465 if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
466 !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
467 return InstDesc(false, I);
468 if (!Cmp->hasOneUse())
469 return InstDesc(false, I);
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))
476 return InstDesc(Select, MRK_UIntMin);
477 else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
478 return InstDesc(Select, MRK_UIntMax);
479 else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
480 return InstDesc(Select, MRK_SIntMax);
481 else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
482 return InstDesc(Select, MRK_SIntMin);
483 else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
484 return InstDesc(Select, MRK_FloatMin);
485 else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
486 return InstDesc(Select, MRK_FloatMax);
487 else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
488 return InstDesc(Select, MRK_FloatMin);
489 else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
490 return InstDesc(Select, MRK_FloatMax);
491
492 return InstDesc(false, I);
493}
494
495RecurrenceDescriptor::InstDesc
496RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
497 InstDesc &Prev, bool HasFunNoNaNAttr) {
498 bool FP = I->getType()->isFloatingPointTy();
499 Instruction *UAI = Prev.getUnsafeAlgebraInst();
500 if (!UAI && FP && !I->isFast())
501 UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
502
503 switch (I->getOpcode()) {
504 default:
505 return InstDesc(false, I);
506 case Instruction::PHI:
507 return InstDesc(I, Prev.getMinMaxKind(), Prev.getUnsafeAlgebraInst());
508 case Instruction::Sub:
509 case Instruction::Add:
510 return InstDesc(Kind == RK_IntegerAdd, I);
511 case Instruction::Mul:
512 return InstDesc(Kind == RK_IntegerMult, I);
513 case Instruction::And:
514 return InstDesc(Kind == RK_IntegerAnd, I);
515 case Instruction::Or:
516 return InstDesc(Kind == RK_IntegerOr, I);
517 case Instruction::Xor:
518 return InstDesc(Kind == RK_IntegerXor, I);
519 case Instruction::FMul:
520 return InstDesc(Kind == RK_FloatMult, I, UAI);
521 case Instruction::FSub:
522 case Instruction::FAdd:
523 return InstDesc(Kind == RK_FloatAdd, I, UAI);
524 case Instruction::FCmp:
525 case Instruction::ICmp:
526 case Instruction::Select:
527 if (Kind != RK_IntegerMinMax &&
528 (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
529 return InstDesc(false, I);
530 return isMinMaxSelectCmpPattern(I, Prev);
531 }
532}
533
534bool RecurrenceDescriptor::hasMultipleUsesOf(
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}
547bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
548 RecurrenceDescriptor &RedDes,
549 DemandedBits *DB, AssumptionCache *AC,
550 DominatorTree *DT) {
551
552 BasicBlock *Header = TheLoop->getHeader();
553 Function &F = *Header->getParent();
554 bool HasFunNoNaNAttr =
555 F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
556
557 if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes, DB,
558 AC, DT)) {
559 LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
560 return true;
561 }
562 if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes, DB,
563 AC, DT)) {
564 LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
565 return true;
566 }
567 if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes, DB,
568 AC, DT)) {
569 LLVM_DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
570 return true;
571 }
572 if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes, DB,
573 AC, DT)) {
574 LLVM_DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
575 return true;
576 }
577 if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes, DB,
578 AC, DT)) {
579 LLVM_DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
580 return true;
581 }
582 if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr, RedDes,
583 DB, AC, DT)) {
584 LLVM_DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
585 return true;
586 }
587 if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes, DB,
588 AC, DT)) {
589 LLVM_DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
590 return true;
591 }
592 if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes, DB,
593 AC, DT)) {
594 LLVM_DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
595 return true;
596 }
597 if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes, DB,
598 AC, DT)) {
599 LLVM_DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi
600 << "\n");
601 return true;
602 }
603 // Not a reduction of known type.
604 return false;
605}
606
607bool RecurrenceDescriptor::isFirstOrderRecurrence(
608 PHINode *Phi, Loop *TheLoop,
609 DenseMap<Instruction *, Instruction *> &SinkAfter, DominatorTree *DT) {
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));
631 if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous) ||
632 SinkAfter.count(Previous)) // Cannot rely on dominance due to motion.
633 return false;
634
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.
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())) {
644 if (!DT->dominates(Previous, I)) // Otherwise we're good w/o sinking.
645 SinkAfter[I] = Previous;
646 return true;
647 }
648 }
649
650 for (User *U : Phi->users())
651 if (auto *I = dyn_cast<Instruction>(U)) {
652 if (!DT->dominates(Previous, I))
653 return false;
654 }
655
656 return true;
657}
658
659/// This function returns the identity element (or neutral element) for
660/// the operation K.
661Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
662 Type *Tp) {
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:
682 llvm_unreachable("Unknown recurrence kind");
683 }
684}
685
686/// This function translates the recurrence kind to an LLVM binary operator.
687unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
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:
708 llvm_unreachable("Unknown recurrence operation");
709 }
710}
711
712InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K,
713 const SCEV *Step, BinaryOperator *BOp,
714 SmallVectorImpl<Instruction *> *Casts)
715 : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
716 assert(IK != IK_NoInduction && "Not an induction");
717
718 // Start value type should match the induction kind and the value
719 // itself should not be null.
720 assert(StartValue && "StartValue is null");
721 assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) &&
722 "StartValue is not a pointer for pointer induction");
723 assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) &&
724 "StartValue is not an integer for integer induction");
725
726 // Check the Step Value. It should be non-zero integer value.
727 assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
728 "Step value is zero");
729
730 assert((IK != IK_PtrInduction || getConstIntStepValue()) &&
731 "Step value should be constant for pointer induction");
732 assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) &&
733 "StepValue is not an integer");
734
735 assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) &&
736 "StepValue is not FP for FpInduction");
737 assert((IK != IK_FpInduction ||
738 (InductionBinOp &&
739 (InductionBinOp->getOpcode() == Instruction::FAdd ||
740 InductionBinOp->getOpcode() == Instruction::FSub))) &&
741 "Binary opcode should be specified for FP induction");
742
743 if (Casts) {
744 for (auto &Inst : *Casts) {
745 RedundantCasts.push_back(Inst);
746 }
747 }
748}
749
750int InductionDescriptor::getConsecutiveDirection() const {
751 ConstantInt *ConstStep = getConstIntStepValue();
752 if (ConstStep && (ConstStep->isOne() || ConstStep->isMinusOne()))
753 return ConstStep->getSExtValue();
754 return 0;
755}
756
757ConstantInt *InductionDescriptor::getConstIntStepValue() const {
758 if (isa<SCEVConstant>(Step))
759 return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue());
760 return nullptr;
761}
762
763bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop,
764 ScalarEvolution *SE,
765 InductionDescriptor &D) {
766
767 // Here we only handle FP induction variables.
768 assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type");
769
770 if (TheLoop->getHeader() != Phi->getParent())
771 return false;
772
773 // The loop may have multiple entrances or multiple exits; we can analyze
774 // this phi if it has a unique entry value and a unique backedge value.
775 if (Phi->getNumIncomingValues() != 2)
776 return false;
777 Value *BEValue = nullptr, *StartValue = nullptr;
778 if (TheLoop->contains(Phi->getIncomingBlock(0))) {
779 BEValue = Phi->getIncomingValue(0);
780 StartValue = Phi->getIncomingValue(1);
781 } else {
782 assert(TheLoop->contains(Phi->getIncomingBlock(1)) &&
783 "Unexpected Phi node in the loop");
784 BEValue = Phi->getIncomingValue(1);
785 StartValue = Phi->getIncomingValue(0);
786 }
787
788 BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue);
789 if (!BOp)
790 return false;
791
792 Value *Addend = nullptr;
793 if (BOp->getOpcode() == Instruction::FAdd) {
794 if (BOp->getOperand(0) == Phi)
795 Addend = BOp->getOperand(1);
796 else if (BOp->getOperand(1) == Phi)
797 Addend = BOp->getOperand(0);
798 } else if (BOp->getOpcode() == Instruction::FSub)
799 if (BOp->getOperand(0) == Phi)
800 Addend = BOp->getOperand(1);
801
802 if (!Addend)
803 return false;
804
805 // The addend should be loop invariant
806 if (auto *I = dyn_cast<Instruction>(Addend))
807 if (TheLoop->contains(I))
808 return false;
809
810 // FP Step has unknown SCEV
811 const SCEV *Step = SE->getUnknown(Addend);
812 D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp);
813 return true;
814}
815
816/// This function is called when we suspect that the update-chain of a phi node
817/// (whose symbolic SCEV expression sin \p PhiScev) contains redundant casts,
818/// that can be ignored. (This can happen when the PSCEV rewriter adds a runtime
819/// predicate P under which the SCEV expression for the phi can be the
820/// AddRecurrence \p AR; See createAddRecFromPHIWithCast). We want to find the
821/// cast instructions that are involved in the update-chain of this induction.
822/// A caller that adds the required runtime predicate can be free to drop these
823/// cast instructions, and compute the phi using \p AR (instead of some scev
824/// expression with casts).
825///
826/// For example, without a predicate the scev expression can take the following
827/// form:
828/// (Ext ix (Trunc iy ( Start + i*Step ) to ix) to iy)
829///
830/// It corresponds to the following IR sequence:
831/// %for.body:
832/// %x = phi i64 [ 0, %ph ], [ %add, %for.body ]
833/// %casted_phi = "ExtTrunc i64 %x"
834/// %add = add i64 %casted_phi, %step
835///
836/// where %x is given in \p PN,
837/// PSE.getSCEV(%x) is equal to PSE.getSCEV(%casted_phi) under a predicate,
838/// and the IR sequence that "ExtTrunc i64 %x" represents can take one of
839/// several forms, for example, such as:
840/// ExtTrunc1: %casted_phi = and %x, 2^n-1
841/// or:
842/// ExtTrunc2: %t = shl %x, m
843/// %casted_phi = ashr %t, m
844///
845/// If we are able to find such sequence, we return the instructions
846/// we found, namely %casted_phi and the instructions on its use-def chain up
847/// to the phi (not including the phi).
848static bool getCastsForInductionPHI(PredicatedScalarEvolution &PSE,
849 const SCEVUnknown *PhiScev,
850 const SCEVAddRecExpr *AR,
851 SmallVectorImpl<Instruction *> &CastInsts) {
852
853 assert(CastInsts.empty() && "CastInsts is expected to be empty.");
854 auto *PN = cast<PHINode>(PhiScev->getValue());
855 assert(PSE.getSCEV(PN) == AR && "Unexpected phi node SCEV expression");
856 const Loop *L = AR->getLoop();
857
858 // Find any cast instructions that participate in the def-use chain of
859 // PhiScev in the loop.
860 // FORNOW/TODO: We currently expect the def-use chain to include only
861 // two-operand instructions, where one of the operands is an invariant.
862 // createAddRecFromPHIWithCasts() currently does not support anything more
863 // involved than that, so we keep the search simple. This can be
864 // extended/generalized as needed.
865
866 auto getDef = [&](const Value *Val) -> Value * {
867 const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val);
868 if (!BinOp)
869 return nullptr;
870 Value *Op0 = BinOp->getOperand(0);
871 Value *Op1 = BinOp->getOperand(1);
872 Value *Def = nullptr;
873 if (L->isLoopInvariant(Op0))
874 Def = Op1;
875 else if (L->isLoopInvariant(Op1))
876 Def = Op0;
877 return Def;
878 };
879
880 // Look for the instruction that defines the induction via the
881 // loop backedge.
882 BasicBlock *Latch = L->getLoopLatch();
883 if (!Latch)
884 return false;
885 Value *Val = PN->getIncomingValueForBlock(Latch);
886 if (!Val)
887 return false;
888
889 // Follow the def-use chain until the induction phi is reached.
890 // If on the way we encounter a Value that has the same SCEV Expr as the
891 // phi node, we can consider the instructions we visit from that point
892 // as part of the cast-sequence that can be ignored.
893 bool InCastSequence = false;
894 auto *Inst = dyn_cast<Instruction>(Val);
895 while (Val != PN) {
896 // If we encountered a phi node other than PN, or if we left the loop,
897 // we bail out.
898 if (!Inst || !L->contains(Inst)) {
899 return false;
900 }
901 auto *AddRec = dyn_cast<SCEVAddRecExpr>(PSE.getSCEV(Val));
902 if (AddRec && PSE.areAddRecsEqualWithPreds(AddRec, AR))
903 InCastSequence = true;
904 if (InCastSequence) {
905 // Only the last instruction in the cast sequence is expected to have
906 // uses outside the induction def-use chain.
907 if (!CastInsts.empty())
908 if (!Inst->hasOneUse())
909 return false;
910 CastInsts.push_back(Inst);
911 }
912 Val = getDef(Val);
913 if (!Val)
914 return false;
915 Inst = dyn_cast<Instruction>(Val);
916 }
917
918 return InCastSequence;
919}
920
921bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
922 PredicatedScalarEvolution &PSE,
923 InductionDescriptor &D, bool Assume) {
924 Type *PhiTy = Phi->getType();
925
926 // Handle integer and pointer inductions variables.
927 // Now we handle also FP induction but not trying to make a
928 // recurrent expression from the PHI node in-place.
929
930 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() && !PhiTy->isFloatTy() &&
931 !PhiTy->isDoubleTy() && !PhiTy->isHalfTy())
932 return false;
933
934 if (PhiTy->isFloatingPointTy())
935 return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D);
936
937 const SCEV *PhiScev = PSE.getSCEV(Phi);
938 const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
939
940 // We need this expression to be an AddRecExpr.
941 if (Assume && !AR)
942 AR = PSE.getAsAddRec(Phi);
943
944 if (!AR) {
945 LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
946 return false;
947 }
948
949 // Record any Cast instructions that participate in the induction update
950 const auto *SymbolicPhi = dyn_cast<SCEVUnknown>(PhiScev);
951 // If we started from an UnknownSCEV, and managed to build an addRecurrence
952 // only after enabling Assume with PSCEV, this means we may have encountered
953 // cast instructions that required adding a runtime check in order to
954 // guarantee the correctness of the AddRecurence respresentation of the
955 // induction.
956 if (PhiScev != AR && SymbolicPhi) {
957 SmallVector<Instruction *, 2> Casts;
958 if (getCastsForInductionPHI(PSE, SymbolicPhi, AR, Casts))
959 return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR, &Casts);
960 }
961
962 return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR);
963}
964
965bool InductionDescriptor::isInductionPHI(
966 PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE,
967 InductionDescriptor &D, const SCEV *Expr,
968 SmallVectorImpl<Instruction *> *CastsToIgnore) {
969 Type *PhiTy = Phi->getType();
970 // We only handle integer and pointer inductions variables.
971 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
972 return false;
973
974 // Check that the PHI is consecutive.
975 const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
976 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
977
978 if (!AR) {
979 LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
980 return false;
981 }
982
983 if (AR->getLoop() != TheLoop) {
984 // FIXME: We should treat this as a uniform. Unfortunately, we
985 // don't currently know how to handled uniform PHIs.
986 LLVM_DEBUG(
987 dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
988 return false;
989 }
990
991 Value *StartValue =
992 Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader());
993 const SCEV *Step = AR->getStepRecurrence(*SE);
994 // Calculate the pointer stride and check if it is consecutive.
995 // The stride may be a constant or a loop invariant integer value.
996 const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step);
997 if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop))
998 return false;
999
1000 if (PhiTy->isIntegerTy()) {
1001 D = InductionDescriptor(StartValue, IK_IntInduction, Step, /*BOp=*/nullptr,
1002 CastsToIgnore);
1003 return true;
1004 }
1005
1006 assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
1007 // Pointer induction should be a constant.
1008 if (!ConstStep)
1009 return false;
1010
1011 ConstantInt *CV = ConstStep->getValue();
1012 Type *PointerElementType = PhiTy->getPointerElementType();
1013 // The pointer stride cannot be determined if the pointer element type is not
1014 // sized.
1015 if (!PointerElementType->isSized())
1016 return false;
1017
1018 const DataLayout &DL = Phi->getModule()->getDataLayout();
1019 int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
1020 if (!Size)
1021 return false;
1022
1023 int64_t CVSize = CV->getSExtValue();
1024 if (CVSize % Size)
1025 return false;
1026 auto *StepValue =
1027 SE->getConstant(CV->getType(), CVSize / Size, true /* signed */);
1028 D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue);
1029 return true;
1030}