LLVM 24.0.0git
IVDescriptors.cpp
Go to the documentation of this file.
1//===- llvm/Analysis/IVDescriptors.cpp - IndVar Descriptors -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file "describes" induction and recurrence variables.
10//
11//===----------------------------------------------------------------------===//
12
20#include "llvm/IR/Dominators.h"
23#include "llvm/IR/ValueHandle.h"
24#include "llvm/Support/Debug.h"
26
27using namespace llvm;
28using namespace llvm::PatternMatch;
29using namespace llvm::SCEVPatternMatch;
30
31#define DEBUG_TYPE "iv-descriptors"
32
35 for (const Use &Use : I->operands())
36 if (!Set.count(dyn_cast<Instruction>(Use)))
37 return false;
38 return true;
39}
40
42 switch (Kind) {
43 default:
44 break;
46 case RecurKind::Sub:
47 case RecurKind::Add:
48 case RecurKind::Mul:
49 case RecurKind::Or:
50 case RecurKind::And:
51 case RecurKind::Xor:
52 case RecurKind::SMax:
53 case RecurKind::SMin:
54 case RecurKind::UMax:
55 case RecurKind::UMin:
59 return true;
60 }
61 return false;
62}
63
67
68/// Determines if Phi may have been type-promoted. If Phi has a single user
69/// that ANDs the Phi with a type mask, return the user. RT is updated to
70/// account for the narrower bit width represented by the mask, and the AND
71/// instruction is added to CI.
75 if (!Phi->hasOneUse())
76 return Phi;
77
78 const APInt *M = nullptr;
79 Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser());
80
81 // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT
82 // with a new integer type of the corresponding bit width.
83 if (match(J, m_And(m_Instruction(I), m_APInt(M)))) {
84 int32_t Bits = (*M + 1).exactLogBase2();
85 if (Bits > 0) {
86 RT = IntegerType::get(Phi->getContext(), Bits);
87 Visited.insert(Phi);
88 CI.insert(J);
89 return J;
90 }
91 }
92 return Phi;
93}
94
98
99/// Compute the minimal bit width needed to represent a reduction whose exit
100/// instruction is given by Exit.
101static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
102 DemandedBits *DB,
103 AssumptionCache *AC,
104 DominatorTree *DT) {
105 bool IsSigned = false;
106 const DataLayout &DL = Exit->getDataLayout();
107 uint64_t MaxBitWidth = DL.getTypeSizeInBits(Exit->getType());
108
109 if (DB) {
110 // Use the demanded bits analysis to determine the bits that are live out
111 // of the exit instruction, rounding up to the nearest power of two. If the
112 // use of demanded bits results in a smaller bit width, we know the value
113 // must be positive (i.e., IsSigned = false), because if this were not the
114 // case, the sign bit would have been demanded.
115 auto Mask = DB->getDemandedBits(Exit);
116 MaxBitWidth = Mask.getBitWidth() - Mask.countl_zero();
117 }
118
119 if (MaxBitWidth == DL.getTypeSizeInBits(Exit->getType()) && AC && DT) {
120 // If demanded bits wasn't able to limit the bit width, we can try to use
121 // value tracking instead. This can be the case, for example, if the value
122 // may be negative.
123 auto NumSignBits = ComputeNumSignBits(Exit, DL, AC, nullptr, DT);
124 auto NumTypeBits = DL.getTypeSizeInBits(Exit->getType());
125 MaxBitWidth = NumTypeBits - NumSignBits;
126 KnownBits Bits = computeKnownBits(Exit, DL);
127 if (!Bits.isNonNegative()) {
128 // If the value is not known to be non-negative, we set IsSigned to true,
129 // meaning that we will use sext instructions instead of zext
130 // instructions to restore the original type.
131 IsSigned = true;
132 // Make sure at least one sign bit is included in the result, so it
133 // will get properly sign-extended.
134 ++MaxBitWidth;
135 }
136 }
137 MaxBitWidth = llvm::bit_ceil(MaxBitWidth);
138
139 return std::make_pair(Type::getIntNTy(Exit->getContext(), MaxBitWidth),
140 IsSigned);
141}
142
143/// Collect cast instructions that can be ignored in the vectorizer's cost
144/// model, given a reduction exit value and the minimal type in which the
145// reduction can be represented. Also search casts to the recurrence type
146// to find the minimum width used by the recurrence.
147static void collectCastInstrs(Loop *TheLoop, Instruction *Exit,
148 Type *RecurrenceType,
150 unsigned &MinWidthCastToRecurTy) {
151
154 Worklist.push_back(Exit);
155 MinWidthCastToRecurTy = -1U;
156
157 while (!Worklist.empty()) {
158 Instruction *Val = Worklist.pop_back_val();
159 Visited.insert(Val);
160 if (auto *Cast = dyn_cast<CastInst>(Val)) {
161 if (Cast->getSrcTy() == RecurrenceType) {
162 // If the source type of a cast instruction is equal to the recurrence
163 // type, it will be eliminated, and should be ignored in the vectorizer
164 // cost model.
165 Casts.insert(Cast);
166 continue;
167 }
168 if (Cast->getDestTy() == RecurrenceType) {
169 // The minimum width used by the recurrence is found by checking for
170 // casts on its operands. The minimum width is used by the vectorizer
171 // when finding the widest type for in-loop reductions without any
172 // loads/stores.
173 MinWidthCastToRecurTy = std::min<unsigned>(
174 MinWidthCastToRecurTy, Cast->getSrcTy()->getScalarSizeInBits());
175 continue;
176 }
177 }
178 // Add all operands to the work list if they are loop-varying values that
179 // we haven't yet visited.
180 for (Value *O : cast<User>(Val)->operands())
181 if (auto *I = dyn_cast<Instruction>(O))
182 if (TheLoop->contains(I) && !Visited.count(I))
183 Worklist.push_back(I);
184 }
185}
186
187// Check if a given Phi node can be recognized as an ordered reduction for
188// vectorizing floating point operations without unsafe math.
189static bool checkOrderedReduction(RecurKind Kind, Instruction *ExactFPMathInst,
190 Instruction *Exit, PHINode *Phi) {
191 // Currently only FAdd and FMulAdd are supported.
192 if (Kind != RecurKind::FAdd && Kind != RecurKind::FMulAdd)
193 return false;
194
195 if (Kind == RecurKind::FAdd && Exit->getOpcode() != Instruction::FAdd)
196 return false;
197
198 if (Kind == RecurKind::FMulAdd &&
200 return false;
201
202 // Ensure the exit instruction has only one user other than the reduction PHI
203 if (Exit != ExactFPMathInst || Exit->hasNUsesOrMore(3))
204 return false;
205
206 // The only pattern accepted is the one in which the reduction PHI
207 // is used as one of the operands of the exit instruction
208 auto *Op0 = Exit->getOperand(0);
209 auto *Op1 = Exit->getOperand(1);
210 if (Kind == RecurKind::FAdd && Op0 != Phi && Op1 != Phi)
211 return false;
212 if (Kind == RecurKind::FMulAdd && Exit->getOperand(2) != Phi)
213 return false;
214
215 LLVM_DEBUG(dbgs() << "LV: Found an ordered reduction: Phi: " << *Phi
216 << ", ExitInst: " << *Exit << "\n");
217
218 return true;
219}
220
221// Collect FMF from a value and its associated fcmp in select patterns
223 FastMathFlags FMF = cast<FPMathOperator>(V)->getFastMathFlags();
224 if (auto *Sel = dyn_cast<SelectInst>(V)) {
225 // Accept FMF from either fcmp or select in a min/max idiom.
226 // TODO: Remove this when FMF propagation is fixed or we standardize on
227 // intrinsics.
228 if (auto *FCmp = dyn_cast<FCmpInst>(Sel->getCondition()))
229 FMF |= FCmp->getFastMathFlags();
230 }
231 return FMF;
232}
233
234static std::optional<FastMathFlags>
236 bool HasRequiredFMF = FPOp && FPOp->hasNoNaNs() && FPOp->hasNoSignedZeros();
237 if (HasRequiredFMF)
238 return collectMinMaxFMF(FPOp);
239
240 switch (RK) {
245 break;
246
247 case RecurKind::FMax:
249 return std::nullopt;
251 break;
252 case RecurKind::FMin:
254 return std::nullopt;
256 break;
257 default:
258 return std::nullopt;
259 }
260 return collectMinMaxFMF(FPOp);
261}
262
264 ScalarEvolution *SE) {
265 Type *Ty = Phi->getType()->getScalarType();
266 BasicBlock *Latch = TheLoop->getLoopLatch();
267 if (Phi->getNumIncomingValues() != 2 ||
268 Phi->getParent() != TheLoop->getHeader() ||
269 (!Ty->isIntegerTy() && !Ty->isFloatingPointTy()) || !Latch)
270 return {};
271
272 auto GetMinMaxRK = [](Value *V, Value *&A, Value *&B) -> RecurKind {
273 if (match(V, m_UMin(m_Value(A), m_Value(B))))
274 return RecurKind::UMin;
275 if (match(V, m_UMax(m_Value(A), m_Value(B))))
276 return RecurKind::UMax;
277 if (match(V, m_SMax(m_Value(A), m_Value(B))))
278 return RecurKind::SMax;
279 if (match(V, m_SMin(m_Value(A), m_Value(B))))
280 return RecurKind::SMin;
281 if (match(V, m_OrdOrUnordFMin(m_Value(A), m_Value(B))) ||
283 return RecurKind::FMin;
284 if (match(V, m_OrdOrUnordFMax(m_Value(A), m_Value(B))) ||
286 return RecurKind::FMax;
287 if (match(V, m_FMinimum(m_Value(A), m_Value(B))))
288 return RecurKind::FMinimum;
289 if (match(V, m_FMaximum(m_Value(A), m_Value(B))))
290 return RecurKind::FMaximum;
295 return RecurKind::None;
296 };
297
299 Value *BackedgeValue = Phi->getIncomingValueForBlock(Latch);
301 // Walk def-use chains upwards from BackedgeValue to identify min/max
302 // recurrences.
303 SmallVector<Value *> WorkList({BackedgeValue});
304 SmallPtrSet<Value *, 8> Chain({Phi});
305 while (!WorkList.empty()) {
306 Value *Cur = WorkList.pop_back_val();
307 if (!Chain.insert(Cur).second)
308 continue;
309 auto *I = dyn_cast<Instruction>(Cur);
310 if (!I || !TheLoop->contains(I))
311 return {};
312 if (auto *PN = dyn_cast<PHINode>(I)) {
313 append_range(WorkList, PN->operands());
314 continue;
315 }
316 Value *A, *B;
317 RecurKind CurRK = GetMinMaxRK(Cur, A, B);
318 if (CurRK == RecurKind::None || (RK != RecurKind::None && CurRK != RK))
319 return {};
320
321 RK = CurRK;
322 // Check required fast-math flags for FP recurrences.
324 auto CurFMF = hasRequiredFastMathFlags(cast<FPMathOperator>(Cur), RK);
325 if (!CurFMF)
326 return {};
327 FMF &= *CurFMF;
328 }
329
330 if (auto *SI = dyn_cast<SelectInst>(I))
331 Chain.insert(SI->getCondition());
332
333 if (A == Phi || B == Phi)
334 continue;
335
336 // Add operand to worklist if it matches the pattern (exactly one must
337 // match)
338 Value *X, *Y;
339 auto *IA = dyn_cast<Instruction>(A);
340 auto *IB = dyn_cast<Instruction>(B);
341 bool AMatches = IA && TheLoop->contains(IA) && GetMinMaxRK(A, X, Y) == RK;
342 bool BMatches = IB && TheLoop->contains(IB) && GetMinMaxRK(B, X, Y) == RK;
343 if (AMatches == BMatches) // Both or neither match
344 return {};
345 WorkList.push_back(AMatches ? A : B);
346 }
347
348 // Handle argmin/argmax pattern: PHI has uses outside the reduction chain
349 // that are not intermediate min/max operations (which are handled below).
350 // Requires integer min/max, and the PHI must be the only in-loop user of
351 // BackedgeValue (so vectorizer can handle both PHIs together).
352 bool PhiHasInvalidUses = any_of(Phi->users(), [&](Instruction *U) {
353 Value *A, *B;
354 return !Chain.contains(U) && TheLoop->contains(U) &&
355 GetMinMaxRK(U, A, B) == RecurKind::None;
356 });
357 if (PhiHasInvalidUses) {
359 any_of(BackedgeValue->users(), [&](User *U) {
360 auto *UI = cast<Instruction>(U);
361 return UI != Phi && TheLoop->contains(UI);
362 }))
363 return {};
365 Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()),
366 /*Exit=*/nullptr, /*Store=*/nullptr, RK, FastMathFlags(),
367 /*ExactFP=*/nullptr, Phi->getType(), /*IsMultiUse=*/true);
368 }
369
370 // Validate chain entries and collect stores from chain entries and
371 // intermediate ops.
373 for (Value *V : Chain) {
374 for (User *U : V->users()) {
375 if (Chain.contains(U))
376 continue;
377 auto *I = dyn_cast<Instruction>(U);
378 if (!I || (!TheLoop->contains(I) && V != BackedgeValue))
379 return {};
380 if (!TheLoop->contains(I))
381 continue;
382 if (auto *SI = dyn_cast<StoreInst>(I)) {
383 Stores.push_back(SI);
384 continue;
385 }
386 // Must be intermediate min/max of the same kind.
387 Value *A, *B;
388 if (GetMinMaxRK(I, A, B) != RK)
389 return {};
390 for (User *IU : I->users()) {
391 if (auto *SI = dyn_cast<StoreInst>(IU))
392 Stores.push_back(SI);
393 else if (!Chain.contains(IU))
394 return {};
395 }
396 }
397 }
398
399 // Validate all stores go to same invariant address and are in the same block.
400 StoreInst *IntermediateStore = nullptr;
401 const SCEV *StorePtrSCEV = nullptr;
402 for (StoreInst *SI : Stores) {
403 if (!SE)
404 return {};
405 const SCEV *Ptr = SE->getSCEV(SI->getPointerOperand());
406 if (!SE->isLoopInvariant(Ptr, TheLoop) ||
407 (StorePtrSCEV && StorePtrSCEV != Ptr))
408 return {};
409 StorePtrSCEV = Ptr;
410 if (!IntermediateStore)
411 IntermediateStore = SI;
412 else if (IntermediateStore->getParent() != SI->getParent())
413 return {};
414 else if (IntermediateStore->comesBefore(SI))
415 IntermediateStore = SI;
416 }
417
419 Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()),
420 cast<Instruction>(BackedgeValue), IntermediateStore, RK, FMF, nullptr,
421 Phi->getType());
422}
423
424// This matches a phi that selects between the original value (HeaderPhi) and an
425// arbitrary non-reduction value.
426static bool isFindLastLikePhi(PHINode *Phi, PHINode *HeaderPhi,
427 SmallPtrSetImpl<Instruction *> &ReductionInstrs) {
428 unsigned NumNonReduxInputs = 0;
429 for (const Value *Op : Phi->operands()) {
430 if (!ReductionInstrs.contains(dyn_cast<Instruction>(Op))) {
431 if (++NumNonReduxInputs > 1)
432 return false;
433 } else if (Op != HeaderPhi) {
434 // TODO: Remove this restriction once chained phis are supported.
435 return false;
436 }
437 }
438 return NumNonReduxInputs == 1;
439}
440
442 PHINode *Phi, RecurKind Kind, Loop *TheLoop, RecurrenceDescriptor &RedDes,
444 ScalarEvolution *SE) {
445 if (Phi->getNumIncomingValues() != 2)
446 return false;
447
448 // Reduction variables are only found in the loop header block.
449 if (Phi->getParent() != TheLoop->getHeader())
450 return false;
451
452 // Obtain the reduction start value from the value that comes from the loop
453 // preheader.
454 if (!TheLoop->getLoopPreheader())
455 return false;
456
457 Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
458 // ExitInstruction is the single value which is used outside the loop.
459 // We only allow for a single reduction value to be used outside the loop.
460 // This includes users of the reduction, variables (which form a cycle
461 // which ends in the phi node).
462 Instruction *ExitInstruction = nullptr;
463
464 // Variable to keep last visited store instruction. By the end of the
465 // algorithm this variable will be either empty or having intermediate
466 // reduction value stored in invariant address.
467 StoreInst *IntermediateStore = nullptr;
468
469 // Indicates that we found a reduction operation in our scan.
470 bool FoundReduxOp = false;
471
472 // We start with the PHI node and scan for all of the users of this
473 // instruction. All users must be instructions that can be used as reduction
474 // variables (such as ADD). We must have a single out-of-block user. The cycle
475 // must include the original PHI.
476 bool FoundStartPHI = false;
477
478 // To recognize AnyOf patterns formed by a icmp select sequence, we store
479 // the number of instruction we saw to make sure we only see one.
480 unsigned NumCmpSelectPatternInst = 0;
481 InstDesc ReduxDesc(false, nullptr);
482
483 // To recognize find-lasts of conditional operations (such as loads or
484 // divides), that need masking, we track non-phi users and if we've found a
485 // "find-last-like" phi (see isFindLastLikePhi). We currently only support
486 // find-last reduction chains with a single "find-last-like" phi and do not
487 // allow any other operations.
488 [[maybe_unused]] unsigned NumNonPHIUsers = 0;
489 bool FoundFindLastLikePhi = false;
490
491 // Data used for determining if the recurrence has been type-promoted.
492 Type *RecurrenceType = Phi->getType();
494 unsigned MinWidthCastToRecurrenceType;
495 Instruction *Start = Phi;
496 bool IsSigned = false;
497
500
501 // Return early if the recurrence kind does not match the type of Phi. If the
502 // recurrence kind is arithmetic, we attempt to look through AND operations
503 // resulting from the type promotion performed by InstCombine. Vector
504 // operations are not limited to the legal integer widths, so we may be able
505 // to evaluate the reduction in the narrower width.
506 // Check the scalar type to handle both scalar and vector types.
507 Type *ScalarTy = RecurrenceType->getScalarType();
508 if (Kind == RecurKind::FindLast) {
509 // FindLast supports all primitive scalar types.
510 if (!ScalarTy->isFloatingPointTy() && !ScalarTy->isIntegerTy() &&
511 !ScalarTy->isPointerTy())
512 return false;
513 } else if (ScalarTy->isFloatingPointTy()) {
515 return false;
516 } else if (ScalarTy->isIntegerTy()) {
517 if (!isIntegerRecurrenceKind(Kind))
518 return false;
519 Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts);
520 } else {
521 // Pointer min/max may exist, but it is not supported as a reduction op.
522 return false;
523 }
524
525 Worklist.push_back(Start);
526 VisitedInsts.insert(Start);
527
528 // Start with all flags set because we will intersect this with the reduction
529 // flags from all the reduction operations.
531
532 // The first instruction in the use-def chain of the Phi node that requires
533 // exact floating point operations.
534 Instruction *ExactFPMathInst = nullptr;
535
536 // A value in the reduction can be used:
537 // - By the reduction:
538 // - Reduction operation:
539 // - One use of reduction value (safe).
540 // - Multiple use of reduction value (not safe).
541 // - PHI:
542 // - All uses of the PHI must be the reduction (safe).
543 // - Otherwise, not safe.
544 // - By instructions outside of the loop (safe).
545 // * One value may have several outside users, but all outside
546 // uses must be of the same value.
547 // - By store instructions with a loop invariant address (safe with
548 // the following restrictions):
549 // * If there are several stores, all must have the same address.
550 // * Final value should be stored in that loop invariant address.
551 // - By an instruction that is not part of the reduction (not safe).
552 // This is either:
553 // * An instruction type other than PHI or the reduction operation.
554 // * A PHI in the header other than the initial PHI.
555 while (!Worklist.empty()) {
556 Instruction *Cur = Worklist.pop_back_val();
557
558 // Store instructions are allowed iff it is the store of the reduction
559 // value to the same loop invariant memory location.
560 if (auto *SI = dyn_cast<StoreInst>(Cur)) {
561 if (!SE) {
562 LLVM_DEBUG(dbgs() << "Store instructions are not processed without "
563 << "Scalar Evolution Analysis\n");
564 return false;
565 }
566
567 const SCEV *PtrScev = SE->getSCEV(SI->getPointerOperand());
568 // Check it is the same address as previous stores
569 if (IntermediateStore) {
570 const SCEV *OtherScev =
571 SE->getSCEV(IntermediateStore->getPointerOperand());
572
573 if (OtherScev != PtrScev) {
574 LLVM_DEBUG(dbgs() << "Storing reduction value to different addresses "
575 << "inside the loop: " << *SI->getPointerOperand()
576 << " and "
577 << *IntermediateStore->getPointerOperand() << '\n');
578 return false;
579 }
580 }
581
582 // Check the pointer is loop invariant
583 if (!SE->isLoopInvariant(PtrScev, TheLoop)) {
584 LLVM_DEBUG(dbgs() << "Storing reduction value to non-uniform address "
585 << "inside the loop: " << *SI->getPointerOperand()
586 << '\n');
587 return false;
588 }
589
590 // IntermediateStore is always the last store in the loop.
592 continue;
593 }
594
595 // No Users.
596 // If the instruction has no users then this is a broken chain and can't be
597 // a reduction variable.
598 if (Cur->use_empty())
599 return false;
600
601 bool IsAPhi = isa<PHINode>(Cur);
602 if (!IsAPhi)
603 ++NumNonPHIUsers;
604
605 // A header PHI use other than the original PHI.
606 if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
607 return false;
608
609 // Reductions of instructions such as Div, and Sub is only possible if the
610 // LHS is the reduction variable.
611 if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
612 !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
613 !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
614 return false;
615
616 // Any reduction instruction must be of one of the allowed kinds. We ignore
617 // the starting value (the Phi or an AND instruction if the Phi has been
618 // type-promoted).
619 if (Cur != Start) {
620 ReduxDesc = isRecurrenceInstr(TheLoop, Phi, Cur, Kind, ReduxDesc, SE);
621 ExactFPMathInst = ExactFPMathInst == nullptr
622 ? ReduxDesc.getExactFPMathInst()
623 : ExactFPMathInst;
624 if (!ReduxDesc.isRecurrence())
625 return false;
626 // FIXME: FMF is allowed on phi, but propagation is not handled correctly.
627 if (isa<FPMathOperator>(ReduxDesc.getPatternInst()) && !IsAPhi)
628 FMF &= collectMinMaxFMF(ReduxDesc.getPatternInst());
629 // Update this reduction kind if we matched a new instruction.
630 // TODO: Can we eliminate the need for a 2nd InstDesc by keeping 'Kind'
631 // state accurate while processing the worklist?
632 if (ReduxDesc.getRecKind() != RecurKind::None)
633 Kind = ReduxDesc.getRecKind();
634 }
635
636 bool IsASelect = isa<SelectInst>(Cur);
637
638 // A conditional reduction operation must only have 2 or less uses in
639 // VisitedInsts.
640 if (IsASelect && (Kind == RecurKind::FAdd || Kind == RecurKind::FMul) &&
641 hasMultipleUsesOf(Cur, VisitedInsts, 2))
642 return false;
643
644 // A reduction operation must only have one use of the reduction value.
645 if (!IsAPhi && !IsASelect && !isAnyOfRecurrenceKind(Kind) &&
646 hasMultipleUsesOf(Cur, VisitedInsts, 1))
647 return false;
648
649 // All inputs to a PHI node must be a reduction value, unless the phi is a
650 // "FindLast-like" phi (described below).
651 if (IsAPhi && Cur != Phi) {
652 if (!areAllUsesIn(Cur, VisitedInsts)) {
653 // A "FindLast-like" phi acts like a conditional select between the
654 // previous reduction value, and an arbitrary value. Note: Multiple
655 // "FindLast-like" phis are not supported see:
656 // IVDescriptorsTest.UnsupportedFindLastPhi.
657 FoundFindLastLikePhi =
658 Kind == RecurKind::FindLast && !FoundFindLastLikePhi &&
659 isFindLastLikePhi(cast<PHINode>(Cur), Phi, VisitedInsts);
660 if (!FoundFindLastLikePhi)
661 return false;
662 }
663 }
664
665 if (isAnyOfRecurrenceKind(Kind) && IsASelect)
666 ++NumCmpSelectPatternInst;
667
668 // Check whether we found a reduction operator.
669 FoundReduxOp |= (!IsAPhi || FoundFindLastLikePhi) && Cur != Start;
670
671 // Process users of current instruction. Push non-PHI nodes after PHI nodes
672 // onto the stack. This way we are going to have seen all inputs to PHI
673 // nodes once we get to them.
676 for (User *U : Cur->users()) {
678
679 // If the user is a call to llvm.fmuladd then the instruction can only be
680 // the final operand.
681 if (isFMulAddIntrinsic(UI))
682 if (Cur == UI->getOperand(0) || Cur == UI->getOperand(1))
683 return false;
684
685 // Check if we found the exit user.
686 BasicBlock *Parent = UI->getParent();
687 if (!TheLoop->contains(Parent)) {
688 // If we already know this instruction is used externally, move on to
689 // the next user.
690 if (ExitInstruction == Cur)
691 continue;
692
693 // Exit if you find multiple values used outside or if the header phi
694 // node is being used. In this case the user uses the value of the
695 // previous iteration, in which case we would loose "VF-1" iterations of
696 // the reduction operation if we vectorize.
697 if (ExitInstruction != nullptr || Cur == Phi)
698 return false;
699
700 // The instruction used by an outside user must be the last instruction
701 // before we feed back to the reduction phi. Otherwise, we loose VF-1
702 // operations on the value.
703 if (!is_contained(Phi->operands(), Cur))
704 return false;
705
706 ExitInstruction = Cur;
707 continue;
708 }
709
710 // Process instructions only once (termination). Each reduction cycle
711 // value must only be used once, except by phi nodes and conditional
712 // reductions which are represented as a cmp followed by a select.
713 InstDesc IgnoredVal(false, nullptr);
714 if (VisitedInsts.insert(UI).second) {
715 if (isa<PHINode>(UI)) {
716 PHIs.push_back(UI);
717 } else {
719 if (SI && SI->getPointerOperand() == Cur) {
720 // Reduction variable chain can only be stored somewhere but it
721 // can't be used as an address.
722 return false;
723 }
724 NonPHIs.push_back(UI);
725 }
726 } else if (!isa<PHINode>(UI) &&
727 ((!isConditionalRdxPattern(UI).isRecurrence() &&
728 !isAnyOfPattern(TheLoop, Phi, UI, IgnoredVal)
729 .isRecurrence())))
730 return false;
731
732 // Remember that we completed the cycle.
733 if (UI == Phi)
734 FoundStartPHI = true;
735 }
736 Worklist.append(PHIs.begin(), PHIs.end());
737 Worklist.append(NonPHIs.begin(), NonPHIs.end());
738 }
739
740 // We only expect to match a single "find-last-like" phi per find-last
741 // reduction, with no non-phi operations in the reduction use chain.
742 assert((!FoundFindLastLikePhi ||
743 (Kind == RecurKind::FindLast && NumNonPHIUsers == 0)) &&
744 "Unexpectedly matched a 'find-last-like' phi");
745
746 if (isAnyOfRecurrenceKind(Kind) && NumCmpSelectPatternInst != 1)
747 return false;
748
749 if (IntermediateStore) {
750 // Check that stored value goes to the phi node again. This way we make sure
751 // that the value stored in IntermediateStore is indeed the final reduction
752 // value.
753 if (!is_contained(Phi->operands(), IntermediateStore->getValueOperand())) {
754 LLVM_DEBUG(dbgs() << "Not a final reduction value stored: "
755 << *IntermediateStore << '\n');
756 return false;
757 }
758
759 // If there is an exit instruction it's value should be stored in
760 // IntermediateStore
761 if (ExitInstruction &&
762 IntermediateStore->getValueOperand() != ExitInstruction) {
763 LLVM_DEBUG(dbgs() << "Last store Instruction of reduction value does not "
764 "store last calculated value of the reduction: "
765 << *IntermediateStore << '\n');
766 return false;
767 }
768
769 // If all uses are inside the loop (intermediate stores), then the
770 // reduction value after the loop will be the one used in the last store.
771 if (!ExitInstruction)
772 ExitInstruction = cast<Instruction>(IntermediateStore->getValueOperand());
773 }
774
775 if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
776 return false;
777
778 const bool IsOrdered =
779 checkOrderedReduction(Kind, ExactFPMathInst, ExitInstruction, Phi);
780
781 if (Start != Phi) {
782 // If the starting value is not the same as the phi node, we speculatively
783 // looked through an 'and' instruction when evaluating a potential
784 // arithmetic reduction to determine if it may have been type-promoted.
785 //
786 // We now compute the minimal bit width that is required to represent the
787 // reduction. If this is the same width that was indicated by the 'and', we
788 // can represent the reduction in the smaller type. The 'and' instruction
789 // will be eliminated since it will essentially be a cast instruction that
790 // can be ignore in the cost model. If we compute a different type than we
791 // did when evaluating the 'and', the 'and' will not be eliminated, and we
792 // will end up with different kinds of operations in the recurrence
793 // expression (e.g., IntegerAND, IntegerADD). We give up if this is
794 // the case.
795 //
796 // The vectorizer relies on InstCombine to perform the actual
797 // type-shrinking. It does this by inserting instructions to truncate the
798 // exit value of the reduction to the width indicated by RecurrenceType and
799 // then extend this value back to the original width. If IsSigned is false,
800 // a 'zext' instruction will be generated; otherwise, a 'sext' will be
801 // used.
802 //
803 // TODO: We should not rely on InstCombine to rewrite the reduction in the
804 // smaller type. We should just generate a correctly typed expression
805 // to begin with.
806 Type *ComputedType;
807 std::tie(ComputedType, IsSigned) =
808 computeRecurrenceType(ExitInstruction, DB, AC, DT);
809 if (ComputedType != RecurrenceType)
810 return false;
811 }
812
813 // Collect cast instructions and the minimum width used by the recurrence.
814 // If the starting value is not the same as the phi node and the computed
815 // recurrence type is equal to the recurrence type, the recurrence expression
816 // will be represented in a narrower or wider type. If there are any cast
817 // instructions that will be unnecessary, collect them in CastsFromRecurTy.
818 // Note that the 'and' instruction was already included in this list.
819 //
820 // TODO: A better way to represent this may be to tag in some way all the
821 // instructions that are a part of the reduction. The vectorizer cost
822 // model could then apply the recurrence type to these instructions,
823 // without needing a white list of instructions to ignore.
824 // This may also be useful for the inloop reductions, if it can be
825 // kept simple enough.
826 collectCastInstrs(TheLoop, ExitInstruction, RecurrenceType, CastInsts,
827 MinWidthCastToRecurrenceType);
828
829 // We found a reduction var if we have reached the original phi node and we
830 // only have a single instruction with out-of-loop users.
831
832 // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
833 // is saved as part of the RecurrenceDescriptor.
834
835 // Save the description of this reduction variable.
836 RedDes =
837 RecurrenceDescriptor(RdxStart, ExitInstruction, IntermediateStore, Kind,
838 FMF, ExactFPMathInst, RecurrenceType, IsSigned,
839 IsOrdered, CastInsts, MinWidthCastToRecurrenceType);
840 return true;
841}
842
843// We are looking for loops that do something like this:
844// int r = 0;
845// for (int i = 0; i < n; i++) {
846// if (src[i] > 3)
847// r = 3;
848// }
849// where the reduction value (r) only has two states, in this example 0 or 3.
850// The generated LLVM IR for this type of loop will be like this:
851// for.body:
852// %r = phi i32 [ %spec.select, %for.body ], [ 0, %entry ]
853// ...
854// %cmp = icmp sgt i32 %5, 3
855// %spec.select = select i1 %cmp, i32 3, i32 %r
856// ...
857// In general we can support vectorization of loops where 'r' flips between
858// any two non-constants, provided they are loop invariant. The only thing
859// we actually care about at the end of the loop is whether or not any lane
860// in the selected vector is different from the start value. The final
861// across-vector reduction after the loop simply involves choosing the start
862// value if nothing changed (0 in the example above) or the other selected
863// value (3 in the example above).
866 Instruction *I, InstDesc &Prev) {
867 // We must handle the select(cmp(),x,y) as a single instruction. Advance to
868 // the select.
869 if (match(I, m_OneUse(m_Cmp()))) {
870 if (auto *Select = dyn_cast<SelectInst>(*I->user_begin()))
871 return InstDesc(Select, Prev.getRecKind());
872 }
873
874 if (!match(I, m_Select(m_Cmp(), m_Value(), m_Value())))
875 return InstDesc(false, I);
876
878 Value *NonPhi = nullptr;
879
880 if (OrigPhi == dyn_cast<PHINode>(SI->getTrueValue()))
881 NonPhi = SI->getFalseValue();
882 else if (OrigPhi == dyn_cast<PHINode>(SI->getFalseValue()))
883 NonPhi = SI->getTrueValue();
884 else
885 return InstDesc(false, I);
886
887 // We are looking for selects of the form:
888 // select(cmp(), phi, loop_invariant) or
889 // select(cmp(), loop_invariant, phi)
890 if (!Loop->isLoopInvariant(NonPhi))
891 return InstDesc(false, I);
892
893 return InstDesc(I, RecurKind::AnyOf);
894}
895
896// We are looking for loops that do something like this:
897// int r = 0;
898// for (int i = 0; i < n; i++) {
899// if (src[i] > 3)
900// r = i;
901// }
902// or like this:
903// int r = 0;
904// for (int i = 0; i < n; i++) {
905// if (src[i] > 3)
906// r = <loop-varying value>;
907// }
908// The reduction value (r) is derived from either the values of an induction
909// variable (i) sequence, an arbitrary loop-varying value, or from the start
910// value (0). The LLVM IR generated for such loops would be as follows:
911// for.body:
912// %r = phi i32 [ %spec.select, %for.body ], [ 0, %entry ]
913// %i = phi i32 [ %inc, %for.body ], [ 0, %entry ]
914// ...
915// %cmp = icmp sgt i32 %5, 3
916// %spec.select = select i1 %cmp, i32 %i, i32 %r
917// %inc = add nsw i32 %i, 1
918// ...
919//
920// When searching for an arbitrary loop-varying value, the reduction value will
921// either be the initial value (0) if the condition was never met, or the value
922// of the loop-varying value in the most recent loop iteration where the
923// condition was met.
927 // TODO: Support the vectorization of FindLastIV when the reduction phi is
928 // used by more than one select instruction. This vectorization is only
929 // performed when the SCEV of each increasing induction variable used by the
930 // select instructions is identical.
931 if (!OrigPhi->hasOneUse())
932 return InstDesc(false, I);
933
934 // We are looking for selects of the form:
935 // select(cmp(), phi, value) or
936 // select(cmp(), value, phi)
937 if (!match(I, m_CombineOr(m_Select(m_Cmp(), m_Value(), m_Specific(OrigPhi)),
938 m_Select(m_Cmp(), m_Specific(OrigPhi), m_Value()))))
939 return InstDesc(false, I);
940
942}
943
944/// Returns true if the select instruction has users in the compare-and-add
945/// reduction pattern below. The select instruction argument is the last one
946/// in the sequence.
947///
948/// %sum.1 = phi ...
949/// ...
950/// %cmp = fcmp pred %0, %CFP
951/// %add = fadd %0, %sum.1
952/// %sum.2 = select %cmp, %add, %sum.1
955 Value *TrueVal, *FalseVal;
956 // Only handle single use cases for now.
957 if (!match(I,
958 m_Select(m_OneUse(m_Cmp()), m_Value(TrueVal), m_Value(FalseVal))))
959 return InstDesc(false, I);
960
961 // Handle only when either of operands of select instruction is a PHI
962 // node for now.
963 if ((isa<PHINode>(TrueVal) && isa<PHINode>(FalseVal)) ||
964 (!isa<PHINode>(TrueVal) && !isa<PHINode>(FalseVal)))
965 return InstDesc(false, I);
966
967 Instruction *I1 = isa<PHINode>(TrueVal) ? dyn_cast<Instruction>(FalseVal)
968 : dyn_cast<Instruction>(TrueVal);
969 if (!I1 || !I1->isBinaryOp())
970 return InstDesc(false, I);
971
972 Value *Op1, *Op2;
973 if (!(((m_FAdd(m_Value(Op1), m_Value(Op2)).match(I1) ||
974 m_FSub(m_Value(Op1), m_Value(Op2)).match(I1)) &&
975 I1->isFast()) ||
976 (m_FMul(m_Value(Op1), m_Value(Op2)).match(I1) && (I1->isFast())) ||
977 ((m_Add(m_Value(Op1), m_Value(Op2)).match(I1) ||
978 m_Sub(m_Value(Op1), m_Value(Op2)).match(I1))) ||
979 (m_Mul(m_Value(Op1), m_Value(Op2)).match(I1))))
980 return InstDesc(false, I);
981
984 if (!IPhi || IPhi != FalseVal)
985 return InstDesc(false, I);
986
987 return InstDesc(true, I);
988}
989
992 Instruction *I, RecurKind Kind,
993 InstDesc &Prev, ScalarEvolution *SE) {
994 assert(Prev.getRecKind() == RecurKind::None || Prev.getRecKind() == Kind);
995 switch (I->getOpcode()) {
996 default:
997 return InstDesc(false, I);
998 case Instruction::PHI:
999 return InstDesc(I, Prev.getRecKind(), Prev.getExactFPMathInst());
1000 case Instruction::Sub:
1001 return InstDesc(
1002 Kind == RecurKind::Sub || Kind == RecurKind::AddChainWithSubs, I);
1003 case Instruction::Add:
1004 return InstDesc(
1005 Kind == RecurKind::Add || Kind == RecurKind::AddChainWithSubs, I);
1006 case Instruction::Mul:
1007 return InstDesc(Kind == RecurKind::Mul, I);
1008 case Instruction::And:
1009 return InstDesc(Kind == RecurKind::And, I);
1010 case Instruction::Or:
1011 return InstDesc(Kind == RecurKind::Or, I);
1012 case Instruction::Xor:
1013 return InstDesc(Kind == RecurKind::Xor, I);
1014 case Instruction::FDiv:
1015 case Instruction::FMul:
1016 return InstDesc(Kind == RecurKind::FMul, I,
1017 I->hasAllowReassoc() ? nullptr : I);
1018 case Instruction::FSub:
1019 return InstDesc(Kind == RecurKind::FSub ||
1021 I, I->hasAllowReassoc() ? nullptr : I);
1022 case Instruction::FAdd:
1023 return InstDesc(Kind == RecurKind::FAdd ||
1025 I, I->hasAllowReassoc() ? nullptr : I);
1026 case Instruction::Select:
1027 if (isSubRecurrenceKind(Kind) || Kind == RecurKind::FAdd ||
1028 Kind == RecurKind::FMul || Kind == RecurKind::Add ||
1029 Kind == RecurKind::Mul || Kind == RecurKind::AddChainWithSubs ||
1031 return isConditionalRdxPattern(I);
1032 if (isFindRecurrenceKind(Kind) && SE)
1033 return isFindPattern(L, OrigPhi, I, *SE);
1034 [[fallthrough]];
1035 case Instruction::FCmp:
1036 case Instruction::ICmp:
1037 case Instruction::Call:
1038 if (isAnyOfRecurrenceKind(Kind))
1039 return isAnyOfPattern(L, OrigPhi, I, Prev);
1040 if (isFMulAddIntrinsic(I))
1041 return InstDesc(Kind == RecurKind::FMulAdd, I,
1042 I->hasAllowReassoc() ? nullptr : I);
1043 return InstDesc(false, I);
1044 }
1045}
1046
1049 unsigned MaxNumUses) {
1050 unsigned NumUses = 0;
1051 for (const Use &U : I->operands()) {
1052 if (Insts.count(dyn_cast<Instruction>(U)))
1053 ++NumUses;
1054 if (NumUses > MaxNumUses)
1055 return true;
1056 }
1057
1058 return false;
1059}
1060
1062 RecurrenceDescriptor &RedDes,
1064 DominatorTree *DT,
1065 ScalarEvolution *SE) {
1066 if (AddReductionVar(Phi, RecurKind::Add, TheLoop, RedDes, DB, AC, DT, SE)) {
1067 LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
1068 return true;
1069 }
1070 if (AddReductionVar(Phi, RecurKind::Sub, TheLoop, RedDes, DB, AC, DT, SE)) {
1071 LLVM_DEBUG(dbgs() << "Found a SUB reduction PHI." << *Phi << "\n");
1072 return true;
1073 }
1074 if (AddReductionVar(Phi, RecurKind::AddChainWithSubs, TheLoop, RedDes, DB, AC,
1075 DT, SE)) {
1076 LLVM_DEBUG(dbgs() << "Found a chained ADD-SUB reduction PHI." << *Phi
1077 << "\n");
1078 return true;
1079 }
1080 if (AddReductionVar(Phi, RecurKind::Mul, TheLoop, RedDes, DB, AC, DT, SE)) {
1081 LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
1082 return true;
1083 }
1084 if (AddReductionVar(Phi, RecurKind::Or, TheLoop, RedDes, DB, AC, DT, SE)) {
1085 LLVM_DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
1086 return true;
1087 }
1088 if (AddReductionVar(Phi, RecurKind::And, TheLoop, RedDes, DB, AC, DT, SE)) {
1089 LLVM_DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
1090 return true;
1091 }
1092 if (AddReductionVar(Phi, RecurKind::Xor, TheLoop, RedDes, DB, AC, DT, SE)) {
1093 LLVM_DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
1094 return true;
1095 }
1096 auto RD = getMinMaxRecurrence(Phi, TheLoop, SE);
1097 if (RD.getRecurrenceKind() != RecurKind::None) {
1098 assert(
1099 RecurrenceDescriptor::isMinMaxRecurrenceKind(RD.getRecurrenceKind()) &&
1100 "Expected a min/max recurrence kind");
1101 LLVM_DEBUG(dbgs() << "Found a min/max reduction PHI." << *Phi << "\n");
1102 RedDes = std::move(RD);
1103 return true;
1104 }
1105 if (AddReductionVar(Phi, RecurKind::AnyOf, TheLoop, RedDes, DB, AC, DT, SE)) {
1106 LLVM_DEBUG(dbgs() << "Found a conditional select reduction PHI." << *Phi
1107 << "\n");
1108 return true;
1109 }
1110 if (AddReductionVar(Phi, RecurKind::FindLast, TheLoop, RedDes, DB, AC, DT,
1111 SE)) {
1112 LLVM_DEBUG(dbgs() << "Found a Find reduction PHI." << *Phi << "\n");
1113 return true;
1114 }
1115 if (AddReductionVar(Phi, RecurKind::FMul, TheLoop, RedDes, DB, AC, DT, SE)) {
1116 LLVM_DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
1117 return true;
1118 }
1119 if (AddReductionVar(Phi, RecurKind::FSub, TheLoop, RedDes, DB, AC, DT, SE)) {
1120 LLVM_DEBUG(dbgs() << "Found an FSub reduction PHI." << *Phi << "\n");
1121 return true;
1122 }
1123 if (AddReductionVar(Phi, RecurKind::FAdd, TheLoop, RedDes, DB, AC, DT, SE)) {
1124 LLVM_DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
1125 return true;
1126 }
1127 if (AddReductionVar(Phi, RecurKind::FAddChainWithSubs, TheLoop, RedDes, DB,
1128 AC, DT, SE)) {
1129 LLVM_DEBUG(dbgs() << "Found a chained FADD-FSUB chained reduction PHI."
1130 << *Phi << "\n");
1131 return true;
1132 }
1133 if (AddReductionVar(Phi, RecurKind::FMulAdd, TheLoop, RedDes, DB, AC, DT,
1134 SE)) {
1135 LLVM_DEBUG(dbgs() << "Found an FMulAdd reduction PHI." << *Phi << "\n");
1136 return true;
1137 }
1138
1139 // Not a reduction of known type.
1140 return false;
1141}
1142
1144 DominatorTree *DT) {
1145
1146 // Ensure the phi node is in the loop header and has two incoming values.
1147 if (Phi->getParent() != TheLoop->getHeader() ||
1148 Phi->getNumIncomingValues() != 2)
1149 return false;
1150
1151 // Ensure the loop has a preheader and a single latch block. The loop
1152 // vectorizer will need the latch to set up the next iteration of the loop.
1153 auto *Preheader = TheLoop->getLoopPreheader();
1154 auto *Latch = TheLoop->getLoopLatch();
1155 if (!Preheader || !Latch)
1156 return false;
1157
1158 // Ensure the phi node's incoming blocks are the loop preheader and latch.
1159 if (Phi->getBasicBlockIndex(Preheader) < 0 ||
1160 Phi->getBasicBlockIndex(Latch) < 0)
1161 return false;
1162
1163 // Get the previous value. The previous value comes from the latch edge while
1164 // the initial value comes from the preheader edge.
1165 auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch));
1166
1167 // If Previous is a phi in the header, go through incoming values from the
1168 // latch until we find a non-phi value. Use this as the new Previous, all uses
1169 // in the header will be dominated by the original phi, but need to be moved
1170 // after the non-phi previous value.
1172 while (auto *PrevPhi = dyn_cast_or_null<PHINode>(Previous)) {
1173 if (PrevPhi->getParent() != Phi->getParent())
1174 return false;
1175 if (!SeenPhis.insert(PrevPhi).second)
1176 return false;
1177 Previous = dyn_cast<Instruction>(PrevPhi->getIncomingValueForBlock(Latch));
1178 }
1179
1180 if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous))
1181 return false;
1182
1183 // Ensure every user of the phi node (recursively) is dominated by the
1184 // previous value. The dominance requirement ensures the loop vectorizer will
1185 // not need to vectorize the initial value prior to the first iteration of the
1186 // loop.
1187 // TODO: Consider extending this sinking to handle memory instructions.
1188
1190 BasicBlock *PhiBB = Phi->getParent();
1192 auto TryToPushSinkCandidate = [&](Instruction *SinkCandidate) {
1193 // Cyclic dependence.
1194 if (Previous == SinkCandidate)
1195 return false;
1196
1197 if (!Seen.insert(SinkCandidate).second)
1198 return true;
1199 if (DT->dominates(Previous,
1200 SinkCandidate)) // We already are good w/o sinking.
1201 return true;
1202
1203 if (SinkCandidate->getParent() != PhiBB ||
1204 SinkCandidate->mayHaveSideEffects() ||
1205 SinkCandidate->mayReadFromMemory() || SinkCandidate->isTerminator())
1206 return false;
1207
1208 // If we reach a PHI node that is not dominated by Previous, we reached a
1209 // header PHI. No need for sinking.
1210 if (isa<PHINode>(SinkCandidate))
1211 return true;
1212
1213 // Sink User tentatively and check its users
1214 WorkList.push_back(SinkCandidate);
1215 return true;
1216 };
1217
1218 WorkList.push_back(Phi);
1219 // Try to recursively sink instructions and their users after Previous.
1220 while (!WorkList.empty()) {
1221 Instruction *Current = WorkList.pop_back_val();
1222 for (User *User : Current->users()) {
1223 if (!TryToPushSinkCandidate(cast<Instruction>(User)))
1224 return false;
1225 }
1226 }
1227
1228 return true;
1229}
1230
1232 switch (Kind) {
1233 case RecurKind::Sub:
1234 return Instruction::Sub;
1236 case RecurKind::Add:
1237 return Instruction::Add;
1238 case RecurKind::Mul:
1239 return Instruction::Mul;
1240 case RecurKind::Or:
1241 return Instruction::Or;
1242 case RecurKind::And:
1243 return Instruction::And;
1244 case RecurKind::Xor:
1245 return Instruction::Xor;
1246 case RecurKind::FMul:
1247 return Instruction::FMul;
1248 case RecurKind::FMulAdd:
1250 case RecurKind::FAdd:
1251 return Instruction::FAdd;
1252 case RecurKind::FSub:
1253 return Instruction::FSub;
1254 case RecurKind::SMax:
1255 case RecurKind::SMin:
1256 case RecurKind::UMax:
1257 case RecurKind::UMin:
1258 return Instruction::ICmp;
1259 case RecurKind::FMax:
1260 case RecurKind::FMin:
1265 return Instruction::FCmp;
1267 case RecurKind::AnyOf:
1268 case RecurKind::FindIV:
1269 // TODO: Set AnyOf and FindIV to Instruction::Select once in-loop reductions
1270 // are supported.
1271 default:
1272 llvm_unreachable("Unknown recurrence operation");
1273 }
1274}
1275
1278 SmallVector<Instruction *, 4> ReductionOperations;
1279 const bool IsMinMax = isMinMaxRecurrenceKind(Kind);
1280
1281 // Search down from the Phi to the LoopExitInstr, looking for instructions
1282 // with a single user of the correct type for the reduction.
1283
1284 // Note that we check that the type of the operand is correct for each item in
1285 // the chain, including the last (the loop exit value). This can come up from
1286 // sub, which would otherwise be treated as an add reduction. MinMax also need
1287 // to check for a pair of icmp/select, for which we use getNextInstruction and
1288 // isCorrectOpcode functions to step the right number of instruction, and
1289 // check the icmp/select pair.
1290 // FIXME: We also do not attempt to look through Select's yet, which might
1291 // be part of the reduction chain, or attempt to looks through And's to find a
1292 // smaller bitwidth. Subs are also currently not allowed (which are usually
1293 // treated as part of a add reduction) as they are expected to generally be
1294 // more expensive than out-of-loop reductions, and need to be costed more
1295 // carefully.
1296 unsigned ExpectedUses = 1;
1297 if (IsMinMax)
1298 ExpectedUses = 2;
1299
1300 auto getNextInstruction = [&](Instruction *Cur) -> Instruction * {
1301 for (auto *User : Cur->users()) {
1303 if (isa<PHINode>(UI))
1304 continue;
1305 if (IsMinMax) {
1306 // We are expecting a icmp/select pair, which we go to the next select
1307 // instruction if we can. We already know that Cur has 2 uses.
1308 if (isa<SelectInst>(UI))
1309 return UI;
1310 continue;
1311 }
1312 return UI;
1313 }
1314 return nullptr;
1315 };
1316 auto isCorrectOpcode = [&](Instruction *Cur) {
1317 if (IsMinMax) {
1318 Value *LHS, *RHS;
1320 matchSelectPattern(Cur, LHS, RHS).Flavor);
1321 }
1322 // Recognize a call to the llvm.fmuladd intrinsic.
1323 if (isFMulAddIntrinsic(Cur))
1324 return true;
1325
1326 if (Cur->getOpcode() == Instruction::Sub &&
1328 return true;
1329
1330 if (Cur->getOpcode() == Instruction::FSub &&
1332 return true;
1333
1334 return Cur->getOpcode() == getOpcode();
1335 };
1336
1337 // Attempt to look through Phis which are part of the reduction chain
1338 unsigned ExtraPhiUses = 0;
1339 Instruction *RdxInstr = LoopExitInstr;
1340 if (auto ExitPhi = dyn_cast<PHINode>(LoopExitInstr)) {
1341 if (ExitPhi->getNumIncomingValues() != 2)
1342 return {};
1343
1344 Instruction *Inc0 = dyn_cast<Instruction>(ExitPhi->getIncomingValue(0));
1345 Instruction *Inc1 = dyn_cast<Instruction>(ExitPhi->getIncomingValue(1));
1346
1347 Instruction *Chain = nullptr;
1348 if (Inc0 == Phi)
1349 Chain = Inc1;
1350 else if (Inc1 == Phi)
1351 Chain = Inc0;
1352 else
1353 return {};
1354
1355 RdxInstr = Chain;
1356 ExtraPhiUses = 1;
1357 }
1358
1359 // The loop exit instruction we check first (as a quick test) but add last. We
1360 // check the opcode is correct (and dont allow them to be Subs) and that they
1361 // have expected to have the expected number of uses. They will have one use
1362 // from the phi and one from a LCSSA value, no matter the type.
1363 if (!isCorrectOpcode(RdxInstr) || !LoopExitInstr->hasNUses(2))
1364 return {};
1365
1366 // Check that the Phi has one (or two for min/max) uses, plus an extra use
1367 // for conditional reductions.
1368 if (!Phi->hasNUses(ExpectedUses + ExtraPhiUses))
1369 return {};
1370
1371 Instruction *Cur = getNextInstruction(Phi);
1372
1373 // Each other instruction in the chain should have the expected number of uses
1374 // and be the correct opcode.
1375 while (Cur != RdxInstr) {
1376 if (!Cur || !isCorrectOpcode(Cur) || !Cur->hasNUses(ExpectedUses))
1377 return {};
1378
1379 ReductionOperations.push_back(Cur);
1380 Cur = getNextInstruction(Cur);
1381 }
1382
1383 ReductionOperations.push_back(Cur);
1384 return ReductionOperations;
1385}
1386
1388 Value *Start, InductionKind K, const SCEV *Step, BinaryOperator *BOp,
1391 : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
1392 assert(IK != IK_NoInduction && "Not an induction");
1393
1394 // Start value type should match the induction kind and the value
1395 // itself should not be null.
1396 assert(StartValue && "StartValue is null");
1397 assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) &&
1398 "StartValue is not a pointer for pointer induction");
1399 assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) &&
1400 "StartValue is not an integer for integer induction");
1401
1402 // Check the Step Value. It should be non-zero integer value.
1403 assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
1404 "Step value is zero");
1405
1406 assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) &&
1407 "StepValue is not an integer");
1408
1409 assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) &&
1410 "StepValue is not FP for FpInduction");
1411 assert((IK != IK_FpInduction ||
1412 (InductionBinOp &&
1413 (InductionBinOp->getOpcode() == Instruction::FAdd ||
1414 InductionBinOp->getOpcode() == Instruction::FSub))) &&
1415 "Binary opcode should be specified for FP induction");
1416
1417 if (Casts)
1418 llvm::append_range(RedundantCasts, *Casts);
1419 llvm::append_range(NoWrapPredicates, NoWrapPreds);
1420}
1421
1427
1429 if (auto *ConstStep = dyn_cast<SCEVConstant>(Step))
1430 return ConstStep->getValue();
1431 return nullptr;
1432}
1433
1435 ScalarEvolution *SE,
1437
1438 // Here we only handle FP induction variables.
1439 assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type");
1440
1441 if (TheLoop->getHeader() != Phi->getParent())
1442 return false;
1443
1444 // The loop may have multiple entrances or multiple exits; we can analyze
1445 // this phi if it has a unique entry value and a unique backedge value.
1446 if (Phi->getNumIncomingValues() != 2)
1447 return false;
1448 Value *BEValue = nullptr, *StartValue = nullptr;
1449 if (TheLoop->contains(Phi->getIncomingBlock(0))) {
1450 BEValue = Phi->getIncomingValue(0);
1451 StartValue = Phi->getIncomingValue(1);
1452 } else {
1453 assert(TheLoop->contains(Phi->getIncomingBlock(1)) &&
1454 "Unexpected Phi node in the loop");
1455 BEValue = Phi->getIncomingValue(1);
1456 StartValue = Phi->getIncomingValue(0);
1457 }
1458
1460 if (!BOp)
1461 return false;
1462
1463 Value *Addend = nullptr;
1464 if (BOp->getOpcode() == Instruction::FAdd) {
1465 if (BOp->getOperand(0) == Phi)
1466 Addend = BOp->getOperand(1);
1467 else if (BOp->getOperand(1) == Phi)
1468 Addend = BOp->getOperand(0);
1469 } else if (BOp->getOpcode() == Instruction::FSub)
1470 if (BOp->getOperand(0) == Phi)
1471 Addend = BOp->getOperand(1);
1472
1473 if (!Addend)
1474 return false;
1475
1476 // The addend should be loop invariant
1477 if (auto *I = dyn_cast<Instruction>(Addend))
1478 if (TheLoop->contains(I))
1479 return false;
1480
1481 // FP Step has unknown SCEV
1482 const SCEV *Step = SE->getUnknown(Addend);
1483 D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp);
1484 return true;
1485}
1486
1487/// This function is called when we suspect that the update-chain of a phi node
1488/// (whose symbolic SCEV expression sin \p PhiScev) contains redundant casts,
1489/// that can be ignored. (This can happen when the PSCEV rewriter adds a runtime
1490/// predicate P under which the SCEV expression for the phi can be the
1491/// AddRecurrence \p AR; See createAddRecFromPHIWithCast). We want to find the
1492/// cast instructions that are involved in the update-chain of this induction.
1493/// A caller that adds the required runtime predicate can be free to drop these
1494/// cast instructions, and compute the phi using \p AR (instead of some scev
1495/// expression with casts).
1496///
1497/// For example, without a predicate the scev expression can take the following
1498/// form:
1499/// (Ext ix (Trunc iy ( Start + i*Step ) to ix) to iy)
1500///
1501/// It corresponds to the following IR sequence:
1502/// %for.body:
1503/// %x = phi i64 [ 0, %ph ], [ %add, %for.body ]
1504/// %casted_phi = "ExtTrunc i64 %x"
1505/// %add = add i64 %casted_phi, %step
1506///
1507/// where %x is given in \p PN,
1508/// PSE.getSCEV(%x) is equal to PSE.getSCEV(%casted_phi) under a predicate,
1509/// and the IR sequence that "ExtTrunc i64 %x" represents can take one of
1510/// several forms, for example, such as:
1511/// ExtTrunc1: %casted_phi = and %x, 2^n-1
1512/// or:
1513/// ExtTrunc2: %t = shl %x, m
1514/// %casted_phi = ashr %t, m
1515///
1516/// If we are able to find such sequence, we return the instructions
1517/// we found, namely %casted_phi and the instructions on its use-def chain up
1518/// to the phi (not including the phi).
1519static bool
1521 const SCEVUnknown *PhiScev, const SCEVAddRecExpr *AR,
1523 ArrayRef<const SCEVPredicate *> NoWrapPreds) {
1524
1525 assert(CastInsts.empty() && "CastInsts is expected to be empty.");
1526 auto *PN = cast<PHINode>(PhiScev->getValue());
1527
1528 // Build a predicate to rewrite SCEVs of values in the cast chain using the
1529 // predicates needed for this induction.
1530 ScalarEvolution &SE = *PSE.getSE();
1531 SCEVUnionPredicate NoWrapUnionPred(NoWrapPreds, SE);
1532 const Loop *L = AR->getLoop();
1533 assert(SE.rewriteUsingPredicate(SE.getSCEV(PN), L, NoWrapUnionPred) == AR &&
1534 "Unexpected phi node SCEV expression");
1535
1536 // Find any cast instructions that participate in the def-use chain of
1537 // PhiScev in the loop.
1538 // FORNOW/TODO: We currently expect the def-use chain to include only
1539 // two-operand instructions, where one of the operands is an invariant.
1540 // createAddRecFromPHIWithCasts() currently does not support anything more
1541 // involved than that, so we keep the search simple. This can be
1542 // extended/generalized as needed.
1543
1544 auto getDef = [&](const Value *Val) -> Value * {
1545 const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val);
1546 if (!BinOp)
1547 return nullptr;
1548 Value *Op0 = BinOp->getOperand(0);
1549 Value *Op1 = BinOp->getOperand(1);
1550 Value *Def = nullptr;
1551 if (L->isLoopInvariant(Op0))
1552 Def = Op1;
1553 else if (L->isLoopInvariant(Op1))
1554 Def = Op0;
1555 return Def;
1556 };
1557
1558 // Look for the instruction that defines the induction via the
1559 // loop backedge.
1560 BasicBlock *Latch = L->getLoopLatch();
1561 if (!Latch)
1562 return false;
1563 Value *Val = PN->getIncomingValueForBlock(Latch);
1564 if (!Val)
1565 return false;
1566
1567 // Follow the def-use chain until the induction phi is reached.
1568 // If on the way we encounter a Value that has the same SCEV Expr as the
1569 // phi node, we can consider the instructions we visit from that point
1570 // as part of the cast-sequence that can be ignored.
1571 bool InCastSequence = false;
1572 auto *Inst = dyn_cast<Instruction>(Val);
1573 while (Val != PN) {
1574 // If we encountered a phi node other than PN, or if we left the loop,
1575 // we bail out.
1576 if (!Inst || !L->contains(Inst)) {
1577 return false;
1578 }
1579 // Create AddRec with NoWrapPredicates applied.
1580 auto *AddRec = dyn_cast<SCEVAddRecExpr>(
1581 SE.rewriteUsingPredicate(SE.getSCEV(Val), L, NoWrapUnionPred));
1582 if (AddRec && PSE.areAddRecsEqualWithPreds(AddRec, AR, NoWrapPreds))
1583 InCastSequence = true;
1584 if (InCastSequence) {
1585 // Only the last instruction in the cast sequence is expected to have
1586 // uses outside the induction def-use chain.
1587 if (!CastInsts.empty())
1588 if (!Inst->hasOneUse())
1589 return false;
1590 CastInsts.push_back(Inst);
1591 }
1592 Val = getDef(Val);
1593 if (!Val)
1594 return false;
1595 Inst = dyn_cast<Instruction>(Val);
1596 }
1597
1598 return InCastSequence;
1599}
1600
1603 InductionDescriptor &D, bool Assume) {
1604 Type *PhiTy = Phi->getType();
1605
1606 // Handle integer and pointer inductions variables.
1607 // Now we handle also FP induction but not trying to make a
1608 // recurrent expression from the PHI node in-place.
1609
1610 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() && !PhiTy->isFloatTy() &&
1611 !PhiTy->isDoubleTy() && !PhiTy->isHalfTy())
1612 return false;
1613
1614 if (PhiTy->isFloatingPointTy())
1615 return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D);
1616
1617 const SCEV *PhiScev = PSE.getSCEV(Phi);
1618 const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
1619
1620 // Collect predicates needed to force the SCEV into an AddRecExpr.
1622
1623 // We need this expression to be an AddRecExpr.
1624 if (Assume && !AR)
1625 AR = PSE.getAsAddRec(Phi, &Preds);
1626
1627 if (!AR) {
1628 LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1629 return false;
1630 }
1631
1632 // Record any Cast instructions that participate in the induction update
1633 const auto *SymbolicPhi = dyn_cast<SCEVUnknown>(PhiScev);
1634 // If we started from an UnknownSCEV, and managed to build an addRecurrence
1635 // only after enabling Assume with PSCEV, this means we may have encountered
1636 // cast instructions that required adding a runtime check in order to
1637 // guarantee the correctness of the AddRecurrence respresentation of the
1638 // induction.
1639 if (PhiScev != AR && SymbolicPhi) {
1641 if (getCastsForInductionPHI(PSE, SymbolicPhi, AR, Casts, Preds))
1642 return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, Preds, AR, &Casts);
1643 }
1644
1645 return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, Preds, AR);
1646}
1647
1649 PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE,
1651 const SCEV *Expr, SmallVectorImpl<Instruction *> *CastsToIgnore) {
1652 Type *PhiTy = Phi->getType();
1653 // isSCEVable returns true for integer and pointer types.
1654 if (!SE->isSCEVable(PhiTy))
1655 return false;
1656
1657 // Check that the PHI is consecutive.
1658 const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
1659 const SCEV *Step;
1660
1661 // FIXME: We are currently matching the specific loop TheLoop; if it doesn't
1662 // match, we should treat it as a uniform. Unfortunately, we don't currently
1663 // know how to handled uniform PHIs.
1664 if (!match(PhiScev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step),
1665 m_SpecificLoop(TheLoop)))) {
1666 LLVM_DEBUG(
1667 dbgs() << "LV: PHI is not a poly recurrence for requested loop.\n");
1668 return false;
1669 }
1670
1671 // This function assumes that InductionPhi is called only on Phi nodes
1672 // present inside loop headers. Check for the same, and throw an assert if
1673 // the current Phi is not present inside the loop header.
1674 assert(Phi->getParent() == TheLoop->getHeader() &&
1675 "Invalid Phi node, not present in loop header");
1676
1677 if (!TheLoop->getLoopPreheader())
1678 return false;
1679
1680 Value *StartValue =
1681 Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
1682
1683 BasicBlock *Latch = TheLoop->getLoopLatch();
1684 if (!Latch)
1685 return false;
1686
1687 if (PhiTy->isIntegerTy()) {
1688 BinaryOperator *BOp =
1689 dyn_cast<BinaryOperator>(Phi->getIncomingValueForBlock(Latch));
1690 D = InductionDescriptor(StartValue, IK_IntInduction, Step, BOp,
1691 CastsToIgnore, Preds);
1692 return true;
1693 }
1694
1695 assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
1696
1697 // This allows induction variables w/non-constant steps.
1698 D = InductionDescriptor(StartValue, IK_PtrInduction, Step,
1699 /*InductionBinOp=*/nullptr, /*Casts=*/nullptr, Preds);
1700 return true;
1701}
1702
1703// Recognize a conditional induction PHI by matching the following pattern:
1704// loop_header:
1705// %conditional_iv = phi [ %start, %preheader ], [ %latch_phi, %latch ]
1706// br i1 %do_step, label %step_bb, label %latch
1707//
1708// step_bb:
1709// %step = add/gep %conditional_iv, %step_val
1710// br label %latch
1711//
1712// latch:
1713// %latch_phi = phi [ %conditional_iv, %loop_header ], [ %step, %step_bb ]
1714// br label %loop_header
1717 ScalarEvolution &SE) {
1718 BasicBlock *Preheader = L->getLoopPreheader();
1719 if (!Preheader)
1720 return false;
1721
1722 BasicBlock *Latch = L->getLoopLatch();
1723 if (!Latch || !PN->getType()->isIntOrPtrTy() ||
1724 PN->getParent() != L->getHeader())
1725 return false;
1726
1727 auto *BackedgePHI = dyn_cast<PHINode>(PN->getIncomingValueForBlock(Latch));
1728 if (!BackedgePHI)
1729 return false;
1730
1731 // Ensure the only users of the backedge PHI are outside the loop or the
1732 // header PHI (PN).
1733 for (User *U : BackedgePHI->users()) {
1734 auto *UI = cast<Instruction>(U);
1735 if (UI != PN && L->contains(UI))
1736 return false;
1737 }
1738
1739 // Find the step operation used to increment the conditional induction PHI.
1740 // TODO: Support chains of PHIs.
1741 Value *StepOp =
1742 find_singleton<Value>(BackedgePHI->incoming_values(),
1743 [&](Use &Incoming, bool /*AllowRepeats*/) {
1744 return Incoming != PN ? Incoming.get() : nullptr;
1745 });
1746 if (!StepOp || !StepOp->hasOneUse())
1747 return false;
1748
1749 auto *StepInst = dyn_cast<Instruction>(StepOp);
1750 if (!StepInst)
1751 return false;
1752
1753 Value *Step = nullptr;
1754 bool StepMatch =
1755 PN->getType()->isPointerTy()
1756 ? match(StepInst, m_PtrAdd(m_Specific(PN), m_Value(Step)))
1757 : match(StepInst, m_c_Add(m_Specific(PN), m_Value(Step)));
1758 if (!StepMatch || !L->isLoopInvariant(Step))
1759 return false;
1760
1761 // Ensure GEP offsets are extended to the size of the PHI.
1762 const SCEV *StepSCEV = SE.getTruncateOrSignExtend(
1763 SE.getSCEV(Step), SE.getEffectiveSCEVType(PN->getType()));
1764
1765 if (StepSCEV->isZero())
1766 return false;
1767
1768 Value *Start = PN->getIncomingValueForBlock(Preheader);
1769 const SCEV *StartSCEV = SE.getSCEV(Start);
1770
1771 SCEVFlags NoWrapFlags = SCEV::FlagNone;
1772 if (auto *GEP = dyn_cast<GEPOperator>(StepInst)) {
1773 // With NUSW, we can add NUW if the step is non-negative. We can't add NSW
1774 // as the base address is unsigned.
1775 if (GEP->hasNoUnsignedWrap() ||
1776 (GEP->hasNoUnsignedSignedWrap() && SE.isKnownNonNegative(StepSCEV)))
1777 NoWrapFlags = ScalarEvolution::setFlags(NoWrapFlags, SCEV::FlagNUW);
1778 } else if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(StepInst)) {
1779 if (OBO->hasNoUnsignedWrap())
1780 NoWrapFlags = ScalarEvolution::setFlags(NoWrapFlags, SCEV::FlagNUW);
1781 if (OBO->hasNoSignedWrap())
1782 NoWrapFlags = ScalarEvolution::setFlags(NoWrapFlags, SCEV::FlagNSW);
1783 }
1784
1785 LLVM_DEBUG(dbgs() << "LV: Found a conditional induction phi: HeaderPHI: "
1786 << *PN << ", StepInst: " << *StepInst << "\n");
1787
1788 Desc = ConditionalInductionDescriptor(PN, BackedgePHI, StepInst, StartSCEV,
1789 StepSCEV, NoWrapFlags);
1790 return true;
1791}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Hexagon Common GEP
static bool getCastsForInductionPHI(PredicatedScalarEvolution &PSE, const SCEVUnknown *PhiScev, const SCEVAddRecExpr *AR, SmallVectorImpl< Instruction * > &CastInsts, ArrayRef< const SCEVPredicate * > NoWrapPreds)
This function is called when we suspect that the update-chain of a phi node (whose symbolic SCEV expr...
static std::optional< FastMathFlags > hasRequiredFastMathFlags(FPMathOperator *FPOp, RecurKind &RK)
static void collectCastInstrs(Loop *TheLoop, Instruction *Exit, Type *RecurrenceType, SmallPtrSetImpl< Instruction * > &Casts, unsigned &MinWidthCastToRecurTy)
Collect cast instructions that can be ignored in the vectorizer's cost model, given a reduction exit ...
static bool checkOrderedReduction(RecurKind Kind, Instruction *ExactFPMathInst, Instruction *Exit, PHINode *Phi)
static bool isFindLastLikePhi(PHINode *Phi, PHINode *HeaderPhi, SmallPtrSetImpl< Instruction * > &ReductionInstrs)
static Instruction * lookThroughAnd(PHINode *Phi, Type *&RT, SmallPtrSetImpl< Instruction * > &Visited, SmallPtrSetImpl< Instruction * > &CI)
Determines if Phi may have been type-promoted.
static FastMathFlags collectMinMaxFMF(Value *V)
static RecurrenceDescriptor getMinMaxRecurrence(PHINode *Phi, Loop *TheLoop, ScalarEvolution *SE)
static std::pair< Type *, bool > computeRecurrenceType(Instruction *Exit, DemandedBits *DB, AssumptionCache *AC, DominatorTree *DT)
Compute the minimal bit width needed to represent a reduction whose exit instruction is given by Exit...
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:540
#define I(x, y, z)
Definition MD5.cpp:57
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
BinaryOps getOpcode() const
Definition InstrTypes.h:409
static LLVM_ABI bool isConditionalInductionPHI(PHINode *PN, const Loop *L, ConditionalInductionDescriptor &Desc, ScalarEvolution &SE)
Returns true if PN is a conditional induction variable in the loop L.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:202
bool hasNoNaNs() const
Test if this operation's arguments and results are assumed not-NaN.
Definition Operator.h:270
bool hasNoSignedZeros() const
Test if this operation can ignore the sign of zero.
Definition Operator.h:276
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
static FastMathFlags getFast()
Definition FMF.h:50
A struct for saving information about induction variables.
static LLVM_ABI InductionDescriptor getCanonicalIntInduction(Type *Ty, ScalarEvolution &SE)
Returns the canonical integer induction for type Ty with start = 0 and step = 1.
static LLVM_ABI bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, ArrayRef< const SCEVPredicate * > NoWrapPreds={}, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction * > *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
@ IK_FpInduction
Floating point induction variable.
@ IK_PtrInduction
Pointer induction var. Step = C.
@ IK_IntInduction
Integer induction variable. Step = C.
static LLVM_ABI bool isFPInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D)
Returns true if Phi is a floating point induction in the loop L.
InductionDescriptor()=default
Default constructor - creates an invalid induction.
LLVM_ABI ConstantInt * getConstIntStepValue() const
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
iterator_range< user_iterator > users()
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:338
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
BlockT * getHeader() const
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition LoopInfo.cpp:67
Value * getIncomingValueForBlock(const BasicBlock *BB) const
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2, ArrayRef< const SCEVPredicate * > ExtraPreds={}) const
Check if AR1 and AR2 are equal, while taking into account Equal predicates in Preds and ExtraPreds.
LLVM_ABI const SCEVAddRecExpr * getAsAddRec(Value *V, SmallVectorImpl< const SCEVPredicate * > *WrapPredsAdded=nullptr)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
This POD struct holds information about a potential recurrence operation.
Instruction * getExactFPMathInst() const
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
static bool isFPMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating-point min/max kind.
static bool isFMulAddIntrinsic(Instruction *I)
Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
static LLVM_ABI bool isFixedOrderRecurrence(PHINode *Phi, Loop *TheLoop, DominatorTree *DT)
Returns true if Phi is a fixed-order recurrence.
static LLVM_ABI InstDesc isConditionalRdxPattern(Instruction *I)
Returns a struct describing if the instruction is a Select(FCmp(X, Y), (Z = X op PHINode),...
static LLVM_ABI bool hasMultipleUsesOf(Instruction *I, SmallPtrSetImpl< Instruction * > &Insts, unsigned MaxNumUses)
Returns true if instruction I has multiple uses in Insts.
static LLVM_ABI bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes, DemandedBits *DB=nullptr, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr, ScalarEvolution *SE=nullptr)
Returns true if Phi is a reduction in TheLoop.
static LLVM_ABI bool areAllUsesIn(Instruction *I, SmallPtrSetImpl< Instruction * > &Set)
Returns true if all uses of the instruction I is within the Set.
LLVM_ABI SmallVector< Instruction *, 4 > getReductionOpChain(PHINode *Phi, Loop *L) const
Attempts to find a chain of operations from Phi to LoopExitInst that can be treated as a set of reduc...
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static LLVM_ABI InstDesc isAnyOfPattern(Loop *Loop, PHINode *OrigPhi, Instruction *I, InstDesc &Prev)
Returns a struct describing whether the instruction is either a Select(ICmp(A, B),...
static LLVM_ABI bool isSubRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is for a sub operation.
StoreInst * IntermediateStore
Reductions may store temporary or final result to an invariant address.
static bool isFindRecurrenceKind(RecurKind Kind)
static LLVM_ABI bool isFloatingPointRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating point kind.
static LLVM_ABI InstDesc isRecurrenceInstr(Loop *L, PHINode *Phi, Instruction *I, RecurKind Kind, InstDesc &Prev, ScalarEvolution *SE)
Returns a struct describing if the instruction 'I' can be a recurrence variable of type 'Kind' for a ...
static LLVM_ABI bool AddReductionVar(PHINode *Phi, RecurKind Kind, Loop *TheLoop, RecurrenceDescriptor &RedDes, DemandedBits *DB=nullptr, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr, ScalarEvolution *SE=nullptr)
Returns true if Phi is a reduction of type Kind and adds it to the RecurrenceDescriptor.
static LLVM_ABI InstDesc isFindPattern(Loop *TheLoop, PHINode *OrigPhi, Instruction *I, ScalarEvolution &SE)
Returns a struct describing whether the instruction is either a Select(ICmp(A, B),...
static LLVM_ABI bool isIntegerRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is an integer kind.
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
This node represents a polynomial recurrence on the trip count of the specified loop.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
static constexpr auto FlagNUW
static constexpr auto FlagNSW
Type * getType() const
Return the LLVM type of this SCEV expression.
static constexpr auto FlagNone
The main scalar evolution driver.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
static SCEVFlags setFlags(SCEVFlags Flags, SCEVFlags OnFlags)
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI const SCEV * getUnknown(Value *V)
LLVM_ABI const SCEV * rewriteUsingPredicate(const SCEV *S, const Loop *L, const SCEVPredicate &A)
Re-writes the SCEV according to the Predicates in A.
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
This class represents the LLVM 'select' instruction.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:265
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:303
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
iterator_range< user_iterator > users()
Definition Value.h:428
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition Value.cpp:147
bool use_empty() const
Definition Value.h:348
const ParentTy * getParent() const
Definition ilist_node.h:34
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
auto m_Cmp()
Matches any compare instruction and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1)
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
auto m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
match_combine_or< FMaxMin_match< LHS, RHS, ofmin_pred_ty >, FMaxMin_match< LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< FMaxMin_match< LHS, RHS, ofmax_pred_ty >, FMaxMin_match< LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
specificloop_ty m_SpecificLoop(const Loop *L)
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MachineInstr * getDef(const MachineOperand &MO, const MachineRegisterInfo *MRI)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition bit.h:362
Op::Description Desc
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
T * find_singleton(R &&Range, Predicate P, bool AllowRepeats=false)
Return the single value in Range that satisfies P(<member of Range> *, AllowRepeats)->T * returning n...
Definition STLExtras.h:1853
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
RecurKind
These are the kinds of recurrences that we support.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FMinimumNum
FP min with llvm.minimumnum semantics.
@ FindIV
FindIV reduction with select(icmp(),x,y) where one of (x,y) is a loop induction variable (increasing ...
@ Or
Bitwise or logical OR of integers.
@ FMinimum
FP min with llvm.minimum semantics.
@ FMaxNum
FP max with llvm.maxnum semantics including NaNs.
@ Mul
Product of integers.
@ FSub
Subtraction of floats.
@ FAddChainWithSubs
A chain of fadds and fsubs.
@ None
Not a recurrence.
@ AnyOf
AnyOf reduction with select(cmp(),x,y) where one of (x,y) is loop invariant, and both x and y are int...
@ Xor
Bitwise or logical XOR of integers.
@ FindLast
FindLast reduction with select(cmp(),x,y) where x and y.
@ FMax
FP max implemented in terms of select(cmp()).
@ FMaximum
FP max with llvm.maximum semantics.
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ FMul
Product of floats.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ FMin
FP min implemented in terms of select(cmp()).
@ FMinNum
FP min with llvm.minnum semantics including NaNs.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ FAdd
Sum of floats.
@ FMaximumNum
FP max with llvm.maximumnum semantics.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
DWARFExpression::Operation Op
SCEVFlags
SCEVFlags are bitfield indices into SCEV's SubclassData.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?