LLVM 22.0.0git
VectorUtils.cpp
Go to the documentation of this file.
1//===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
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 defines vectorizer utilities.
10//
11//===----------------------------------------------------------------------===//
12
23#include "llvm/IR/Constants.h"
25#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Value.h"
30
31#define DEBUG_TYPE "vectorutils"
32
33using namespace llvm;
34using namespace llvm::PatternMatch;
35
36/// Maximum factor for an interleaved memory access.
38 "max-interleave-group-factor", cl::Hidden,
39 cl::desc("Maximum factor for an interleaved access group (default = 8)"),
40 cl::init(8));
41
42/// Return true if all of the intrinsic's arguments and return type are scalars
43/// for the scalar form of the intrinsic, and vectors for the vector form of the
44/// intrinsic (except operands that are marked as always being scalar by
45/// isVectorIntrinsicWithScalarOpAtArg).
47 switch (ID) {
48 case Intrinsic::abs: // Begin integer bit-manipulation.
49 case Intrinsic::bswap:
50 case Intrinsic::bitreverse:
51 case Intrinsic::ctpop:
52 case Intrinsic::ctlz:
53 case Intrinsic::cttz:
54 case Intrinsic::fshl:
55 case Intrinsic::fshr:
56 case Intrinsic::smax:
57 case Intrinsic::smin:
58 case Intrinsic::umax:
59 case Intrinsic::umin:
60 case Intrinsic::sadd_sat:
61 case Intrinsic::ssub_sat:
62 case Intrinsic::uadd_sat:
63 case Intrinsic::usub_sat:
64 case Intrinsic::smul_fix:
65 case Intrinsic::smul_fix_sat:
66 case Intrinsic::umul_fix:
67 case Intrinsic::umul_fix_sat:
68 case Intrinsic::sqrt: // Begin floating-point.
69 case Intrinsic::asin:
70 case Intrinsic::acos:
71 case Intrinsic::atan:
72 case Intrinsic::atan2:
73 case Intrinsic::sin:
74 case Intrinsic::cos:
75 case Intrinsic::sincos:
76 case Intrinsic::sincospi:
77 case Intrinsic::tan:
78 case Intrinsic::sinh:
79 case Intrinsic::cosh:
80 case Intrinsic::tanh:
81 case Intrinsic::exp:
82 case Intrinsic::exp10:
83 case Intrinsic::exp2:
84 case Intrinsic::ldexp:
85 case Intrinsic::log:
86 case Intrinsic::log10:
87 case Intrinsic::log2:
88 case Intrinsic::fabs:
89 case Intrinsic::minnum:
90 case Intrinsic::maxnum:
91 case Intrinsic::minimum:
92 case Intrinsic::maximum:
93 case Intrinsic::minimumnum:
94 case Intrinsic::maximumnum:
95 case Intrinsic::modf:
96 case Intrinsic::copysign:
97 case Intrinsic::floor:
98 case Intrinsic::ceil:
99 case Intrinsic::trunc:
100 case Intrinsic::rint:
101 case Intrinsic::nearbyint:
102 case Intrinsic::round:
103 case Intrinsic::roundeven:
104 case Intrinsic::pow:
105 case Intrinsic::fma:
106 case Intrinsic::fmuladd:
107 case Intrinsic::is_fpclass:
108 case Intrinsic::powi:
109 case Intrinsic::canonicalize:
110 case Intrinsic::fptosi_sat:
111 case Intrinsic::fptoui_sat:
112 case Intrinsic::lround:
113 case Intrinsic::llround:
114 case Intrinsic::lrint:
115 case Intrinsic::llrint:
116 case Intrinsic::ucmp:
117 case Intrinsic::scmp:
118 return true;
119 default:
120 return false;
121 }
122}
123
125 const TargetTransformInfo *TTI) {
127 return true;
128
131
132 // TODO: Move frexp to isTriviallyVectorizable.
133 // https://github.com/llvm/llvm-project/issues/112408
134 switch (ID) {
135 case Intrinsic::frexp:
136 case Intrinsic::uadd_with_overflow:
137 case Intrinsic::sadd_with_overflow:
138 case Intrinsic::ssub_with_overflow:
139 case Intrinsic::usub_with_overflow:
140 case Intrinsic::umul_with_overflow:
141 case Intrinsic::smul_with_overflow:
142 return true;
143 }
144 return false;
145}
146
147/// Identifies if the vector form of the intrinsic has a scalar operand.
149 unsigned ScalarOpdIdx,
150 const TargetTransformInfo *TTI) {
151
153 return TTI->isTargetIntrinsicWithScalarOpAtArg(ID, ScalarOpdIdx);
154
155 // Vector predication intrinsics have the EVL as the last operand.
156 if (VPIntrinsic::getVectorLengthParamPos(ID) == ScalarOpdIdx)
157 return true;
158
159 switch (ID) {
160 case Intrinsic::abs:
161 case Intrinsic::vp_abs:
162 case Intrinsic::ctlz:
163 case Intrinsic::vp_ctlz:
164 case Intrinsic::cttz:
165 case Intrinsic::vp_cttz:
166 case Intrinsic::is_fpclass:
167 case Intrinsic::vp_is_fpclass:
168 case Intrinsic::powi:
169 return (ScalarOpdIdx == 1);
170 case Intrinsic::smul_fix:
171 case Intrinsic::smul_fix_sat:
172 case Intrinsic::umul_fix:
173 case Intrinsic::umul_fix_sat:
174 return (ScalarOpdIdx == 2);
175 case Intrinsic::experimental_vp_splice:
176 return ScalarOpdIdx == 2 || ScalarOpdIdx == 4;
177 default:
178 return false;
179 }
180}
181
183 Intrinsic::ID ID, int OpdIdx, const TargetTransformInfo *TTI) {
184 assert(ID != Intrinsic::not_intrinsic && "Not an intrinsic!");
185
188
190 return OpdIdx == -1 || OpdIdx == 0;
191
192 switch (ID) {
193 case Intrinsic::fptosi_sat:
194 case Intrinsic::fptoui_sat:
195 case Intrinsic::lround:
196 case Intrinsic::llround:
197 case Intrinsic::lrint:
198 case Intrinsic::llrint:
199 case Intrinsic::vp_lrint:
200 case Intrinsic::vp_llrint:
201 case Intrinsic::ucmp:
202 case Intrinsic::scmp:
203 return OpdIdx == -1 || OpdIdx == 0;
204 case Intrinsic::modf:
205 case Intrinsic::sincos:
206 case Intrinsic::sincospi:
207 case Intrinsic::is_fpclass:
208 case Intrinsic::vp_is_fpclass:
209 return OpdIdx == 0;
210 case Intrinsic::powi:
211 case Intrinsic::ldexp:
212 return OpdIdx == -1 || OpdIdx == 1;
213 default:
214 return OpdIdx == -1;
215 }
216}
217
219 Intrinsic::ID ID, int RetIdx, const TargetTransformInfo *TTI) {
220
223
224 switch (ID) {
225 case Intrinsic::frexp:
226 return RetIdx == 0 || RetIdx == 1;
227 default:
228 return RetIdx == 0;
229 }
230}
231
232/// Returns intrinsic ID for call.
233/// For the input call instruction it finds mapping intrinsic and returns
234/// its ID, in case it does not found it return not_intrinsic.
236 const TargetLibraryInfo *TLI) {
240
241 if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
242 ID == Intrinsic::lifetime_end || ID == Intrinsic::assume ||
243 ID == Intrinsic::experimental_noalias_scope_decl ||
244 ID == Intrinsic::sideeffect || ID == Intrinsic::pseudoprobe)
245 return ID;
247}
248
250 switch (ID) {
251 case Intrinsic::vector_interleave2:
252 return 2;
253 case Intrinsic::vector_interleave3:
254 return 3;
255 case Intrinsic::vector_interleave4:
256 return 4;
257 case Intrinsic::vector_interleave5:
258 return 5;
259 case Intrinsic::vector_interleave6:
260 return 6;
261 case Intrinsic::vector_interleave7:
262 return 7;
263 case Intrinsic::vector_interleave8:
264 return 8;
265 default:
266 return 0;
267 }
268}
269
271 switch (ID) {
272 case Intrinsic::vector_deinterleave2:
273 return 2;
274 case Intrinsic::vector_deinterleave3:
275 return 3;
276 case Intrinsic::vector_deinterleave4:
277 return 4;
278 case Intrinsic::vector_deinterleave5:
279 return 5;
280 case Intrinsic::vector_deinterleave6:
281 return 6;
282 case Intrinsic::vector_deinterleave7:
283 return 7;
284 case Intrinsic::vector_deinterleave8:
285 return 8;
286 default:
287 return 0;
288 }
289}
290
292 [[maybe_unused]] unsigned Factor =
294 ArrayRef<Type *> DISubtypes = DI->getType()->subtypes();
295 assert(Factor && Factor == DISubtypes.size() &&
296 "unexpected deinterleave factor or result type");
297 return cast<VectorType>(DISubtypes[0]);
298}
299
300/// Given a vector and an element number, see if the scalar value is
301/// already around as a register, for example if it were inserted then extracted
302/// from the vector.
303Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
304 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
305 VectorType *VTy = cast<VectorType>(V->getType());
306 // For fixed-length vector, return poison for out of range access.
307 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
308 unsigned Width = FVTy->getNumElements();
309 if (EltNo >= Width)
310 return PoisonValue::get(FVTy->getElementType());
311 }
312
313 if (Constant *C = dyn_cast<Constant>(V))
314 return C->getAggregateElement(EltNo);
315
316 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
317 // If this is an insert to a variable element, we don't know what it is.
318 if (!isa<ConstantInt>(III->getOperand(2)))
319 return nullptr;
320 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
321
322 // If this is an insert to the element we are looking for, return the
323 // inserted value.
324 if (EltNo == IIElt)
325 return III->getOperand(1);
326
327 // Guard against infinite loop on malformed, unreachable IR.
328 if (III == III->getOperand(0))
329 return nullptr;
330
331 // Otherwise, the insertelement doesn't modify the value, recurse on its
332 // vector input.
333 return findScalarElement(III->getOperand(0), EltNo);
334 }
335
336 ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V);
337 // Restrict the following transformation to fixed-length vector.
338 if (SVI && isa<FixedVectorType>(SVI->getType())) {
339 unsigned LHSWidth =
340 cast<FixedVectorType>(SVI->getOperand(0)->getType())->getNumElements();
341 int InEl = SVI->getMaskValue(EltNo);
342 if (InEl < 0)
343 return PoisonValue::get(VTy->getElementType());
344 if (InEl < (int)LHSWidth)
345 return findScalarElement(SVI->getOperand(0), InEl);
346 return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
347 }
348
349 // Extract a value from a vector add operation with a constant zero.
350 // TODO: Use getBinOpIdentity() to generalize this.
351 Value *Val; Constant *C;
352 if (match(V, m_Add(m_Value(Val), m_Constant(C))))
353 if (Constant *Elt = C->getAggregateElement(EltNo))
354 if (Elt->isNullValue())
355 return findScalarElement(Val, EltNo);
356
357 // If the vector is a splat then we can trivially find the scalar element.
358 if (isa<ScalableVectorType>(VTy))
359 if (Value *Splat = getSplatValue(V))
360 if (EltNo < VTy->getElementCount().getKnownMinValue())
361 return Splat;
362
363 // Otherwise, we don't know.
364 return nullptr;
365}
366
368 int SplatIndex = -1;
369 for (int M : Mask) {
370 // Ignore invalid (undefined) mask elements.
371 if (M < 0)
372 continue;
373
374 // There can be only 1 non-negative mask element value if this is a splat.
375 if (SplatIndex != -1 && SplatIndex != M)
376 return -1;
377
378 // Initialize the splat index to the 1st non-negative mask element.
379 SplatIndex = M;
380 }
381 assert((SplatIndex == -1 || SplatIndex >= 0) && "Negative index?");
382 return SplatIndex;
383}
384
385/// Get splat value if the input is a splat vector or return nullptr.
386/// This function is not fully general. It checks only 2 cases:
387/// the input value is (1) a splat constant vector or (2) a sequence
388/// of instructions that broadcasts a scalar at element 0.
390 if (isa<VectorType>(V->getType()))
391 if (auto *C = dyn_cast<Constant>(V))
392 return C->getSplatValue();
393
394 // shuf (inselt ?, Splat, 0), ?, <0, undef, 0, ...>
395 Value *Splat;
396 if (match(V,
398 m_Value(), m_ZeroMask())))
399 return Splat;
400
401 return nullptr;
402}
403
404bool llvm::isSplatValue(const Value *V, int Index, unsigned Depth) {
405 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
406
407 if (isa<VectorType>(V->getType())) {
408 if (isa<UndefValue>(V))
409 return true;
410 // FIXME: We can allow undefs, but if Index was specified, we may want to
411 // check that the constant is defined at that index.
412 if (auto *C = dyn_cast<Constant>(V))
413 return C->getSplatValue() != nullptr;
414 }
415
416 if (auto *Shuf = dyn_cast<ShuffleVectorInst>(V)) {
417 // FIXME: We can safely allow undefs here. If Index was specified, we will
418 // check that the mask elt is defined at the required index.
419 if (!all_equal(Shuf->getShuffleMask()))
420 return false;
421
422 // Match any index.
423 if (Index == -1)
424 return true;
425
426 // Match a specific element. The mask should be defined at and match the
427 // specified index.
428 return Shuf->getMaskValue(Index) == Index;
429 }
430
431 // The remaining tests are all recursive, so bail out if we hit the limit.
433 return false;
434
435 // If both operands of a binop are splats, the result is a splat.
436 Value *X, *Y, *Z;
437 if (match(V, m_BinOp(m_Value(X), m_Value(Y))))
438 return isSplatValue(X, Index, Depth) && isSplatValue(Y, Index, Depth);
439
440 // If all operands of a select are splats, the result is a splat.
441 if (match(V, m_Select(m_Value(X), m_Value(Y), m_Value(Z))))
442 return isSplatValue(X, Index, Depth) && isSplatValue(Y, Index, Depth) &&
443 isSplatValue(Z, Index, Depth);
444
445 // TODO: Add support for unary ops (fneg), casts, intrinsics (overflow ops).
446
447 return false;
448}
449
451 const APInt &DemandedElts, APInt &DemandedLHS,
452 APInt &DemandedRHS, bool AllowUndefElts) {
453 DemandedLHS = DemandedRHS = APInt::getZero(SrcWidth);
454
455 // Early out if we don't demand any elements.
456 if (DemandedElts.isZero())
457 return true;
458
459 // Simple case of a shuffle with zeroinitializer.
460 if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
461 DemandedLHS.setBit(0);
462 return true;
463 }
464
465 for (unsigned I = 0, E = Mask.size(); I != E; ++I) {
466 int M = Mask[I];
467 assert((-1 <= M) && (M < (SrcWidth * 2)) &&
468 "Invalid shuffle mask constant");
469
470 if (!DemandedElts[I] || (AllowUndefElts && (M < 0)))
471 continue;
472
473 // For undef elements, we don't know anything about the common state of
474 // the shuffle result.
475 if (M < 0)
476 return false;
477
478 if (M < SrcWidth)
479 DemandedLHS.setBit(M);
480 else
481 DemandedRHS.setBit(M - SrcWidth);
482 }
483
484 return true;
485}
486
488 std::array<std::pair<int, int>, 2> &SrcInfo) {
489 const int SignalValue = NumElts * 2;
490 SrcInfo[0] = {-1, SignalValue};
491 SrcInfo[1] = {-1, SignalValue};
492 for (auto [i, M] : enumerate(Mask)) {
493 if (M < 0)
494 continue;
495 int Src = M >= (int)NumElts;
496 int Diff = (int)i - (M % NumElts);
497 bool Match = false;
498 for (int j = 0; j < 2; j++) {
499 auto &[SrcE, DiffE] = SrcInfo[j];
500 if (SrcE == -1) {
501 assert(DiffE == SignalValue);
502 SrcE = Src;
503 DiffE = Diff;
504 }
505 if (SrcE == Src && DiffE == Diff) {
506 Match = true;
507 break;
508 }
509 }
510 if (!Match)
511 return false;
512 }
513 // Avoid all undef masks
514 return SrcInfo[0].first != -1;
515}
516
518 SmallVectorImpl<int> &ScaledMask) {
519 assert(Scale > 0 && "Unexpected scaling factor");
520
521 // Fast-path: if no scaling, then it is just a copy.
522 if (Scale == 1) {
523 ScaledMask.assign(Mask.begin(), Mask.end());
524 return;
525 }
526
527 ScaledMask.clear();
528 for (int MaskElt : Mask) {
529 if (MaskElt >= 0) {
530 assert(((uint64_t)Scale * MaskElt + (Scale - 1)) <= INT32_MAX &&
531 "Overflowed 32-bits");
532 }
533 for (int SliceElt = 0; SliceElt != Scale; ++SliceElt)
534 ScaledMask.push_back(MaskElt < 0 ? MaskElt : Scale * MaskElt + SliceElt);
535 }
536}
537
539 SmallVectorImpl<int> &ScaledMask) {
540 assert(Scale > 0 && "Unexpected scaling factor");
541
542 // Fast-path: if no scaling, then it is just a copy.
543 if (Scale == 1) {
544 ScaledMask.assign(Mask.begin(), Mask.end());
545 return true;
546 }
547
548 // We must map the original elements down evenly to a type with less elements.
549 int NumElts = Mask.size();
550 if (NumElts % Scale != 0)
551 return false;
552
553 ScaledMask.clear();
554 ScaledMask.reserve(NumElts / Scale);
555
556 // Step through the input mask by splitting into Scale-sized slices.
557 do {
558 ArrayRef<int> MaskSlice = Mask.take_front(Scale);
559 assert((int)MaskSlice.size() == Scale && "Expected Scale-sized slice.");
560
561 // The first element of the slice determines how we evaluate this slice.
562 int SliceFront = MaskSlice.front();
563 if (SliceFront < 0) {
564 // Negative values (undef or other "sentinel" values) must be equal across
565 // the entire slice.
566 if (!all_equal(MaskSlice))
567 return false;
568 ScaledMask.push_back(SliceFront);
569 } else {
570 // A positive mask element must be cleanly divisible.
571 if (SliceFront % Scale != 0)
572 return false;
573 // Elements of the slice must be consecutive.
574 for (int i = 1; i < Scale; ++i)
575 if (MaskSlice[i] != SliceFront + i)
576 return false;
577 ScaledMask.push_back(SliceFront / Scale);
578 }
579 Mask = Mask.drop_front(Scale);
580 } while (!Mask.empty());
581
582 assert((int)ScaledMask.size() * Scale == NumElts && "Unexpected scaled mask");
583
584 // All elements of the original mask can be scaled down to map to the elements
585 // of a mask with wider elements.
586 return true;
587}
588
590 SmallVectorImpl<int> &NewMask) {
591 unsigned NumElts = M.size();
592 if (NumElts % 2 != 0)
593 return false;
594
595 NewMask.clear();
596 for (unsigned i = 0; i < NumElts; i += 2) {
597 int M0 = M[i];
598 int M1 = M[i + 1];
599
600 // If both elements are undef, new mask is undef too.
601 if (M0 == -1 && M1 == -1) {
602 NewMask.push_back(-1);
603 continue;
604 }
605
606 if (M0 == -1 && M1 != -1 && (M1 % 2) == 1) {
607 NewMask.push_back(M1 / 2);
608 continue;
609 }
610
611 if (M0 != -1 && (M0 % 2) == 0 && ((M0 + 1) == M1 || M1 == -1)) {
612 NewMask.push_back(M0 / 2);
613 continue;
614 }
615
616 NewMask.clear();
617 return false;
618 }
620 assert(NewMask.size() == NumElts / 2 && "Incorrect size for mask!");
621 return true;
622}
623
624bool llvm::scaleShuffleMaskElts(unsigned NumDstElts, ArrayRef<int> Mask,
625 SmallVectorImpl<int> &ScaledMask) {
626 unsigned NumSrcElts = Mask.size();
627 assert(NumSrcElts > 0 && NumDstElts > 0 && "Unexpected scaling factor");
628
629 // Fast-path: if no scaling, then it is just a copy.
630 if (NumSrcElts == NumDstElts) {
631 ScaledMask.assign(Mask.begin(), Mask.end());
632 return true;
633 }
634
635 // Ensure we can find a whole scale factor.
636 assert(((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) &&
637 "Unexpected scaling factor");
638
639 if (NumSrcElts > NumDstElts) {
640 int Scale = NumSrcElts / NumDstElts;
641 return widenShuffleMaskElts(Scale, Mask, ScaledMask);
642 }
643
644 int Scale = NumDstElts / NumSrcElts;
645 narrowShuffleMaskElts(Scale, Mask, ScaledMask);
646 return true;
647}
648
650 SmallVectorImpl<int> &ScaledMask) {
651 std::array<SmallVector<int, 16>, 2> TmpMasks;
652 SmallVectorImpl<int> *Output = &TmpMasks[0], *Tmp = &TmpMasks[1];
653 ArrayRef<int> InputMask = Mask;
654 for (unsigned Scale = 2; Scale <= InputMask.size(); ++Scale) {
655 while (widenShuffleMaskElts(Scale, InputMask, *Output)) {
656 InputMask = *Output;
657 std::swap(Output, Tmp);
658 }
659 }
660 ScaledMask.assign(InputMask.begin(), InputMask.end());
661}
662
664 ArrayRef<int> Mask, unsigned NumOfSrcRegs, unsigned NumOfDestRegs,
665 unsigned NumOfUsedRegs, function_ref<void()> NoInputAction,
666 function_ref<void(ArrayRef<int>, unsigned, unsigned)> SingleInputAction,
667 function_ref<void(ArrayRef<int>, unsigned, unsigned, bool)>
668 ManyInputsAction) {
669 SmallVector<SmallVector<SmallVector<int>>> Res(NumOfDestRegs);
670 // Try to perform better estimation of the permutation.
671 // 1. Split the source/destination vectors into real registers.
672 // 2. Do the mask analysis to identify which real registers are
673 // permuted.
674 int Sz = Mask.size();
675 unsigned SzDest = Sz / NumOfDestRegs;
676 unsigned SzSrc = Sz / NumOfSrcRegs;
677 for (unsigned I = 0; I < NumOfDestRegs; ++I) {
678 auto &RegMasks = Res[I];
679 RegMasks.assign(2 * NumOfSrcRegs, {});
680 // Check that the values in dest registers are in the one src
681 // register.
682 for (unsigned K = 0; K < SzDest; ++K) {
683 int Idx = I * SzDest + K;
684 if (Idx == Sz)
685 break;
686 if (Mask[Idx] >= 2 * Sz || Mask[Idx] == PoisonMaskElem)
687 continue;
688 int MaskIdx = Mask[Idx] % Sz;
689 int SrcRegIdx = MaskIdx / SzSrc + (Mask[Idx] >= Sz ? NumOfSrcRegs : 0);
690 // Add a cost of PermuteTwoSrc for each new source register permute,
691 // if we have more than one source registers.
692 if (RegMasks[SrcRegIdx].empty())
693 RegMasks[SrcRegIdx].assign(SzDest, PoisonMaskElem);
694 RegMasks[SrcRegIdx][K] = MaskIdx % SzSrc;
695 }
696 }
697 // Process split mask.
698 for (unsigned I : seq<unsigned>(NumOfUsedRegs)) {
699 auto &Dest = Res[I];
700 int NumSrcRegs =
701 count_if(Dest, [](ArrayRef<int> Mask) { return !Mask.empty(); });
702 switch (NumSrcRegs) {
703 case 0:
704 // No input vectors were used!
705 NoInputAction();
706 break;
707 case 1: {
708 // Find the only mask with at least single undef mask elem.
709 auto *It =
710 find_if(Dest, [](ArrayRef<int> Mask) { return !Mask.empty(); });
711 unsigned SrcReg = std::distance(Dest.begin(), It);
712 SingleInputAction(*It, SrcReg, I);
713 break;
714 }
715 default: {
716 // The first mask is a permutation of a single register. Since we have >2
717 // input registers to shuffle, we merge the masks for 2 first registers
718 // and generate a shuffle of 2 registers rather than the reordering of the
719 // first register and then shuffle with the second register. Next,
720 // generate the shuffles of the resulting register + the remaining
721 // registers from the list.
722 auto &&CombineMasks = [](MutableArrayRef<int> FirstMask,
723 ArrayRef<int> SecondMask) {
724 for (int Idx = 0, VF = FirstMask.size(); Idx < VF; ++Idx) {
725 if (SecondMask[Idx] != PoisonMaskElem) {
726 assert(FirstMask[Idx] == PoisonMaskElem &&
727 "Expected undefined mask element.");
728 FirstMask[Idx] = SecondMask[Idx] + VF;
729 }
730 }
731 };
732 auto &&NormalizeMask = [](MutableArrayRef<int> Mask) {
733 for (int Idx = 0, VF = Mask.size(); Idx < VF; ++Idx) {
734 if (Mask[Idx] != PoisonMaskElem)
735 Mask[Idx] = Idx;
736 }
737 };
738 int SecondIdx;
739 bool NewReg = true;
740 do {
741 int FirstIdx = -1;
742 SecondIdx = -1;
743 MutableArrayRef<int> FirstMask, SecondMask;
744 for (unsigned I : seq<unsigned>(2 * NumOfSrcRegs)) {
745 SmallVectorImpl<int> &RegMask = Dest[I];
746 if (RegMask.empty())
747 continue;
748
749 if (FirstIdx == SecondIdx) {
750 FirstIdx = I;
751 FirstMask = RegMask;
752 continue;
753 }
754 SecondIdx = I;
755 SecondMask = RegMask;
756 CombineMasks(FirstMask, SecondMask);
757 ManyInputsAction(FirstMask, FirstIdx, SecondIdx, NewReg);
758 NewReg = false;
759 NormalizeMask(FirstMask);
760 RegMask.clear();
761 SecondMask = FirstMask;
762 SecondIdx = FirstIdx;
763 }
764 if (FirstIdx != SecondIdx && SecondIdx >= 0) {
765 CombineMasks(SecondMask, FirstMask);
766 ManyInputsAction(SecondMask, SecondIdx, FirstIdx, NewReg);
767 NewReg = false;
768 Dest[FirstIdx].clear();
769 NormalizeMask(SecondMask);
770 }
771 } while (SecondIdx >= 0);
772 break;
773 }
774 }
775 }
776}
777
778void llvm::getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth,
779 const APInt &DemandedElts,
780 APInt &DemandedLHS,
781 APInt &DemandedRHS) {
782 assert(VectorBitWidth >= 128 && "Vectors smaller than 128 bit not supported");
783 int NumLanes = VectorBitWidth / 128;
784 int NumElts = DemandedElts.getBitWidth();
785 int NumEltsPerLane = NumElts / NumLanes;
786 int HalfEltsPerLane = NumEltsPerLane / 2;
787
788 DemandedLHS = APInt::getZero(NumElts);
789 DemandedRHS = APInt::getZero(NumElts);
790
791 // Map DemandedElts to the horizontal operands.
792 for (int Idx = 0; Idx != NumElts; ++Idx) {
793 if (!DemandedElts[Idx])
794 continue;
795 int LaneIdx = (Idx / NumEltsPerLane) * NumEltsPerLane;
796 int LocalIdx = Idx % NumEltsPerLane;
797 if (LocalIdx < HalfEltsPerLane) {
798 DemandedLHS.setBit(LaneIdx + 2 * LocalIdx);
799 } else {
800 LocalIdx -= HalfEltsPerLane;
801 DemandedRHS.setBit(LaneIdx + 2 * LocalIdx);
802 }
803 }
804}
805
808 const TargetTransformInfo *TTI) {
809
810 // DemandedBits will give us every value's live-out bits. But we want
811 // to ensure no extra casts would need to be inserted, so every DAG
812 // of connected values must have the same minimum bitwidth.
818 SmallPtrSet<Instruction *, 4> InstructionSet;
820
821 // Determine the roots. We work bottom-up, from truncs or icmps.
822 bool SeenExtFromIllegalType = false;
823 for (auto *BB : Blocks)
824 for (auto &I : *BB) {
825 InstructionSet.insert(&I);
826
827 if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) &&
828 !TTI->isTypeLegal(I.getOperand(0)->getType()))
829 SeenExtFromIllegalType = true;
830
831 // Only deal with non-vector integers up to 64-bits wide.
832 if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) &&
833 !I.getType()->isVectorTy() &&
834 I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) {
835 // Don't make work for ourselves. If we know the loaded type is legal,
836 // don't add it to the worklist.
837 if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType()))
838 continue;
839
840 Worklist.push_back(&I);
841 Roots.insert(&I);
842 }
843 }
844 // Early exit.
845 if (Worklist.empty() || (TTI && !SeenExtFromIllegalType))
846 return MinBWs;
847
848 // Now proceed breadth-first, unioning values together.
849 while (!Worklist.empty()) {
850 Instruction *I = Worklist.pop_back_val();
851 Value *Leader = ECs.getOrInsertLeaderValue(I);
852
853 if (!Visited.insert(I).second)
854 continue;
855
856 // If we encounter a type that is larger than 64 bits, we can't represent
857 // it so bail out.
858 if (DB.getDemandedBits(I).getBitWidth() > 64)
860
861 uint64_t V = DB.getDemandedBits(I).getZExtValue();
862 DBits[Leader] |= V;
863 DBits[I] = V;
864
865 // Casts, loads and instructions outside of our range terminate a chain
866 // successfully.
867 if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) ||
868 !InstructionSet.count(I))
869 continue;
870
871 // Unsafe casts terminate a chain unsuccessfully. We can't do anything
872 // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
873 // transform anything that relies on them.
874 if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
875 !I->getType()->isIntegerTy()) {
876 DBits[Leader] |= ~0ULL;
877 continue;
878 }
879
880 // We don't modify the types of PHIs. Reductions will already have been
881 // truncated if possible, and inductions' sizes will have been chosen by
882 // indvars.
883 if (isa<PHINode>(I))
884 continue;
885
886 // Don't modify the types of operands of a call, as doing that would cause a
887 // signature mismatch.
888 if (isa<CallBase>(I))
889 continue;
890
891 if (DBits[Leader] == ~0ULL)
892 // All bits demanded, no point continuing.
893 continue;
894
895 for (Value *O : I->operands()) {
896 ECs.unionSets(Leader, O);
897 if (auto *OI = dyn_cast<Instruction>(O))
898 Worklist.push_back(OI);
899 }
900 }
901
902 // Now we've discovered all values, walk them to see if there are
903 // any users we didn't see. If there are, we can't optimize that
904 // chain.
905 for (auto &I : DBits)
906 for (auto *U : I.first->users())
907 if (U->getType()->isIntegerTy() && DBits.count(U) == 0)
908 DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
909
910 for (const auto &E : ECs) {
911 if (!E->isLeader())
912 continue;
913 uint64_t LeaderDemandedBits = 0;
914 for (Value *M : ECs.members(*E))
915 LeaderDemandedBits |= DBits[M];
916
917 uint64_t MinBW = llvm::bit_width(LeaderDemandedBits);
918 // Round up to a power of 2
919 MinBW = llvm::bit_ceil(MinBW);
920
921 // We don't modify the types of PHIs. Reductions will already have been
922 // truncated if possible, and inductions' sizes will have been chosen by
923 // indvars.
924 // If we are required to shrink a PHI, abandon this entire equivalence class.
925 bool Abort = false;
926 for (Value *M : ECs.members(*E))
927 if (isa<PHINode>(M) && MinBW < M->getType()->getScalarSizeInBits()) {
928 Abort = true;
929 break;
930 }
931 if (Abort)
932 continue;
933
934 for (Value *M : ECs.members(*E)) {
935 auto *MI = dyn_cast<Instruction>(M);
936 if (!MI)
937 continue;
938 Type *Ty = M->getType();
939 if (Roots.count(MI))
940 Ty = MI->getOperand(0)->getType();
941
942 if (MinBW >= Ty->getScalarSizeInBits())
943 continue;
944
945 // If any of M's operands demand more bits than MinBW then M cannot be
946 // performed safely in MinBW.
947 auto *Call = dyn_cast<CallBase>(MI);
948 auto Ops = Call ? Call->args() : MI->operands();
949 if (any_of(Ops, [&DB, MinBW](Use &U) {
950 auto *CI = dyn_cast<ConstantInt>(U);
951 // For constants shift amounts, check if the shift would result in
952 // poison.
953 if (CI &&
954 isa<ShlOperator, LShrOperator, AShrOperator>(U.getUser()) &&
955 U.getOperandNo() == 1)
956 return CI->uge(MinBW);
957 uint64_t BW = bit_width(DB.getDemandedBits(&U).getZExtValue());
958 return bit_ceil(BW) > MinBW;
959 }))
960 continue;
961
962 MinBWs[MI] = MinBW;
963 }
964 }
965
966 return MinBWs;
967}
968
969/// Add all access groups in @p AccGroups to @p List.
970template <typename ListT>
971static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
972 // Interpret an access group as a list containing itself.
973 if (AccGroups->getNumOperands() == 0) {
974 assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
975 List.insert(AccGroups);
976 return;
977 }
978
979 for (const auto &AccGroupListOp : AccGroups->operands()) {
980 auto *Item = cast<MDNode>(AccGroupListOp.get());
981 assert(isValidAsAccessGroup(Item) && "List item must be an access group");
982 List.insert(Item);
983 }
984}
985
986MDNode *llvm::uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2) {
987 if (!AccGroups1)
988 return AccGroups2;
989 if (!AccGroups2)
990 return AccGroups1;
991 if (AccGroups1 == AccGroups2)
992 return AccGroups1;
993
995 addToAccessGroupList(Union, AccGroups1);
996 addToAccessGroupList(Union, AccGroups2);
997
998 if (Union.size() == 0)
999 return nullptr;
1000 if (Union.size() == 1)
1001 return cast<MDNode>(Union.front());
1002
1003 LLVMContext &Ctx = AccGroups1->getContext();
1004 return MDNode::get(Ctx, Union.getArrayRef());
1005}
1006
1008 const Instruction *Inst2) {
1009 bool MayAccessMem1 = Inst1->mayReadOrWriteMemory();
1010 bool MayAccessMem2 = Inst2->mayReadOrWriteMemory();
1011
1012 if (!MayAccessMem1 && !MayAccessMem2)
1013 return nullptr;
1014 if (!MayAccessMem1)
1015 return Inst2->getMetadata(LLVMContext::MD_access_group);
1016 if (!MayAccessMem2)
1017 return Inst1->getMetadata(LLVMContext::MD_access_group);
1018
1019 MDNode *MD1 = Inst1->getMetadata(LLVMContext::MD_access_group);
1020 MDNode *MD2 = Inst2->getMetadata(LLVMContext::MD_access_group);
1021 if (!MD1 || !MD2)
1022 return nullptr;
1023 if (MD1 == MD2)
1024 return MD1;
1025
1026 // Use set for scalable 'contains' check.
1027 SmallPtrSet<Metadata *, 4> AccGroupSet2;
1028 addToAccessGroupList(AccGroupSet2, MD2);
1029
1030 SmallVector<Metadata *, 4> Intersection;
1031 if (MD1->getNumOperands() == 0) {
1032 assert(isValidAsAccessGroup(MD1) && "Node must be an access group");
1033 if (AccGroupSet2.count(MD1))
1034 Intersection.push_back(MD1);
1035 } else {
1036 for (const MDOperand &Node : MD1->operands()) {
1037 auto *Item = cast<MDNode>(Node.get());
1038 assert(isValidAsAccessGroup(Item) && "List item must be an access group");
1039 if (AccGroupSet2.count(Item))
1040 Intersection.push_back(Item);
1041 }
1042 }
1043
1044 if (Intersection.size() == 0)
1045 return nullptr;
1046 if (Intersection.size() == 1)
1047 return cast<MDNode>(Intersection.front());
1048
1049 LLVMContext &Ctx = Inst1->getContext();
1050 return MDNode::get(Ctx, Intersection);
1051}
1052
1053/// Add metadata from \p Inst to \p Metadata, if it can be preserved after
1054/// vectorization.
1056 Instruction *Inst,
1057 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Metadata) {
1059 static const unsigned SupportedIDs[] = {
1060 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1061 LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
1062 LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
1063 LLVMContext::MD_access_group, LLVMContext::MD_mmra};
1064
1065 // Remove any unsupported metadata kinds from Metadata.
1066 for (unsigned Idx = 0; Idx != Metadata.size();) {
1067 if (is_contained(SupportedIDs, Metadata[Idx].first)) {
1068 ++Idx;
1069 } else {
1070 // Swap element to end and remove it.
1071 std::swap(Metadata[Idx], Metadata.back());
1072 Metadata.pop_back();
1073 }
1074 }
1075}
1076
1077/// \returns \p I after propagating metadata from \p VL.
1079 if (VL.empty())
1080 return Inst;
1082 getMetadataToPropagate(cast<Instruction>(VL[0]), Metadata);
1083
1084 for (auto &[Kind, MD] : Metadata) {
1085 for (int J = 1, E = VL.size(); MD && J != E; ++J) {
1086 const Instruction *IJ = cast<Instruction>(VL[J]);
1087 MDNode *IMD = IJ->getMetadata(Kind);
1088
1089 switch (Kind) {
1090 case LLVMContext::MD_mmra: {
1091 MD = MMRAMetadata::combine(Inst->getContext(), MD, IMD);
1092 break;
1093 }
1094 case LLVMContext::MD_tbaa:
1095 MD = MDNode::getMostGenericTBAA(MD, IMD);
1096 break;
1097 case LLVMContext::MD_alias_scope:
1099 break;
1100 case LLVMContext::MD_fpmath:
1101 MD = MDNode::getMostGenericFPMath(MD, IMD);
1102 break;
1103 case LLVMContext::MD_noalias:
1104 case LLVMContext::MD_nontemporal:
1105 case LLVMContext::MD_invariant_load:
1106 MD = MDNode::intersect(MD, IMD);
1107 break;
1108 case LLVMContext::MD_access_group:
1109 MD = intersectAccessGroups(Inst, IJ);
1110 break;
1111 default:
1112 llvm_unreachable("unhandled metadata");
1113 }
1114 }
1115
1116 Inst->setMetadata(Kind, MD);
1117 }
1118
1119 return Inst;
1120}
1121
1122Constant *
1124 const InterleaveGroup<Instruction> &Group) {
1125 // All 1's means mask is not needed.
1126 if (Group.isFull())
1127 return nullptr;
1128
1129 // TODO: support reversed access.
1130 assert(!Group.isReverse() && "Reversed group not supported.");
1131
1133 for (unsigned i = 0; i < VF; i++)
1134 for (unsigned j = 0; j < Group.getFactor(); ++j) {
1135 unsigned HasMember = Group.getMember(j) ? 1 : 0;
1136 Mask.push_back(Builder.getInt1(HasMember));
1137 }
1138
1139 return ConstantVector::get(Mask);
1140}
1141
1143llvm::createReplicatedMask(unsigned ReplicationFactor, unsigned VF) {
1144 SmallVector<int, 16> MaskVec;
1145 for (unsigned i = 0; i < VF; i++)
1146 for (unsigned j = 0; j < ReplicationFactor; j++)
1147 MaskVec.push_back(i);
1148
1149 return MaskVec;
1150}
1151
1153 unsigned NumVecs) {
1155 for (unsigned i = 0; i < VF; i++)
1156 for (unsigned j = 0; j < NumVecs; j++)
1157 Mask.push_back(j * VF + i);
1158
1159 return Mask;
1160}
1161
1163llvm::createStrideMask(unsigned Start, unsigned Stride, unsigned VF) {
1165 for (unsigned i = 0; i < VF; i++)
1166 Mask.push_back(Start + i * Stride);
1167
1168 return Mask;
1169}
1170
1172 unsigned NumInts,
1173 unsigned NumUndefs) {
1175 for (unsigned i = 0; i < NumInts; i++)
1176 Mask.push_back(Start + i);
1177
1178 for (unsigned i = 0; i < NumUndefs; i++)
1179 Mask.push_back(-1);
1180
1181 return Mask;
1182}
1183
1185 unsigned NumElts) {
1186 // Avoid casts in the loop and make sure we have a reasonable number.
1187 int NumEltsSigned = NumElts;
1188 assert(NumEltsSigned > 0 && "Expected smaller or non-zero element count");
1189
1190 // If the mask chooses an element from operand 1, reduce it to choose from the
1191 // corresponding element of operand 0. Undef mask elements are unchanged.
1192 SmallVector<int, 16> UnaryMask;
1193 for (int MaskElt : Mask) {
1194 assert((MaskElt < NumEltsSigned * 2) && "Expected valid shuffle mask");
1195 int UnaryElt = MaskElt >= NumEltsSigned ? MaskElt - NumEltsSigned : MaskElt;
1196 UnaryMask.push_back(UnaryElt);
1197 }
1198 return UnaryMask;
1199}
1200
1201/// A helper function for concatenating vectors. This function concatenates two
1202/// vectors having the same element type. If the second vector has fewer
1203/// elements than the first, it is padded with undefs.
1205 Value *V2) {
1206 VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType());
1207 VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType());
1208 assert(VecTy1 && VecTy2 &&
1209 VecTy1->getScalarType() == VecTy2->getScalarType() &&
1210 "Expect two vectors with the same element type");
1211
1212 unsigned NumElts1 = cast<FixedVectorType>(VecTy1)->getNumElements();
1213 unsigned NumElts2 = cast<FixedVectorType>(VecTy2)->getNumElements();
1214 assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements");
1215
1216 if (NumElts1 > NumElts2) {
1217 // Extend with UNDEFs.
1218 V2 = Builder.CreateShuffleVector(
1219 V2, createSequentialMask(0, NumElts2, NumElts1 - NumElts2));
1220 }
1221
1222 return Builder.CreateShuffleVector(
1223 V1, V2, createSequentialMask(0, NumElts1 + NumElts2, 0));
1224}
1225
1227 ArrayRef<Value *> Vecs) {
1228 unsigned NumVecs = Vecs.size();
1229 assert(NumVecs > 1 && "Should be at least two vectors");
1230
1232 ResList.append(Vecs.begin(), Vecs.end());
1233 do {
1235 for (unsigned i = 0; i < NumVecs - 1; i += 2) {
1236 Value *V0 = ResList[i], *V1 = ResList[i + 1];
1237 assert((V0->getType() == V1->getType() || i == NumVecs - 2) &&
1238 "Only the last vector may have a different type");
1239
1240 TmpList.push_back(concatenateTwoVectors(Builder, V0, V1));
1241 }
1242
1243 // Push the last vector if the total number of vectors is odd.
1244 if (NumVecs % 2 != 0)
1245 TmpList.push_back(ResList[NumVecs - 1]);
1246
1247 ResList = TmpList;
1248 NumVecs = ResList.size();
1249 } while (NumVecs > 1);
1250
1251 return ResList[0];
1252}
1253
1255 assert(isa<VectorType>(Mask->getType()) &&
1256 isa<IntegerType>(Mask->getType()->getScalarType()) &&
1257 cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
1258 1 &&
1259 "Mask must be a vector of i1");
1260
1261 auto *ConstMask = dyn_cast<Constant>(Mask);
1262 if (!ConstMask)
1263 return false;
1264 if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
1265 return true;
1266 if (isa<ScalableVectorType>(ConstMask->getType()))
1267 return false;
1268 for (unsigned
1269 I = 0,
1270 E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
1271 I != E; ++I) {
1272 if (auto *MaskElt = ConstMask->getAggregateElement(I))
1273 if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
1274 continue;
1275 return false;
1276 }
1277 return true;
1278}
1279
1281 assert(isa<VectorType>(Mask->getType()) &&
1282 isa<IntegerType>(Mask->getType()->getScalarType()) &&
1283 cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
1284 1 &&
1285 "Mask must be a vector of i1");
1286
1287 auto *ConstMask = dyn_cast<Constant>(Mask);
1288 if (!ConstMask)
1289 return false;
1290 if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
1291 return true;
1292 if (isa<ScalableVectorType>(ConstMask->getType()))
1293 return false;
1294 for (unsigned
1295 I = 0,
1296 E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
1297 I != E; ++I) {
1298 if (auto *MaskElt = ConstMask->getAggregateElement(I))
1299 if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
1300 continue;
1301 return false;
1302 }
1303 return true;
1304}
1305
1307 assert(isa<VectorType>(Mask->getType()) &&
1308 isa<IntegerType>(Mask->getType()->getScalarType()) &&
1309 cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
1310 1 &&
1311 "Mask must be a vector of i1");
1312
1313 auto *ConstMask = dyn_cast<Constant>(Mask);
1314 if (!ConstMask)
1315 return false;
1316 if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
1317 return true;
1318 if (isa<ScalableVectorType>(ConstMask->getType()))
1319 return false;
1320 for (unsigned
1321 I = 0,
1322 E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
1323 I != E; ++I) {
1324 if (auto *MaskElt = ConstMask->getAggregateElement(I))
1325 if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
1326 return true;
1327 }
1328 return false;
1329}
1330
1331/// TODO: This is a lot like known bits, but for
1332/// vectors. Is there something we can common this with?
1334 assert(isa<FixedVectorType>(Mask->getType()) &&
1335 isa<IntegerType>(Mask->getType()->getScalarType()) &&
1336 cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
1337 1 &&
1338 "Mask must be a fixed width vector of i1");
1339
1340 const unsigned VWidth =
1341 cast<FixedVectorType>(Mask->getType())->getNumElements();
1342 APInt DemandedElts = APInt::getAllOnes(VWidth);
1343 if (auto *CV = dyn_cast<ConstantVector>(Mask))
1344 for (unsigned i = 0; i < VWidth; i++)
1345 if (CV->getAggregateElement(i)->isNullValue())
1346 DemandedElts.clearBit(i);
1347 return DemandedElts;
1348}
1349
1350bool InterleavedAccessInfo::isStrided(int Stride) {
1351 unsigned Factor = std::abs(Stride);
1352 return Factor >= 2 && Factor <= MaxInterleaveGroupFactor;
1353}
1354
1355void InterleavedAccessInfo::collectConstStrideAccesses(
1357 const DenseMap<Value*, const SCEV*> &Strides) {
1358 auto &DL = TheLoop->getHeader()->getDataLayout();
1359
1360 // Since it's desired that the load/store instructions be maintained in
1361 // "program order" for the interleaved access analysis, we have to visit the
1362 // blocks in the loop in reverse postorder (i.e., in a topological order).
1363 // Such an ordering will ensure that any load/store that may be executed
1364 // before a second load/store will precede the second load/store in
1365 // AccessStrideInfo.
1366 LoopBlocksDFS DFS(TheLoop);
1367 DFS.perform(LI);
1368 for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO()))
1369 for (auto &I : *BB) {
1371 if (!Ptr)
1372 continue;
1373 Type *ElementTy = getLoadStoreType(&I);
1374
1375 // Currently, codegen doesn't support cases where the type size doesn't
1376 // match the alloc size. Skip them for now.
1377 uint64_t Size = DL.getTypeAllocSize(ElementTy);
1378 if (Size * 8 != DL.getTypeSizeInBits(ElementTy))
1379 continue;
1380
1381 // We don't check wrapping here because we don't know yet if Ptr will be
1382 // part of a full group or a group with gaps. Checking wrapping for all
1383 // pointers (even those that end up in groups with no gaps) will be overly
1384 // conservative. For full groups, wrapping should be ok since if we would
1385 // wrap around the address space we would do a memory access at nullptr
1386 // even without the transformation. The wrapping checks are therefore
1387 // deferred until after we've formed the interleaved groups.
1388 int64_t Stride =
1389 getPtrStride(PSE, ElementTy, Ptr, TheLoop, Strides,
1390 /*Assume=*/true, /*ShouldCheckWrap=*/false).value_or(0);
1391
1392 const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
1393 AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size,
1395 }
1396}
1397
1398// Analyze interleaved accesses and collect them into interleaved load and
1399// store groups.
1400//
1401// When generating code for an interleaved load group, we effectively hoist all
1402// loads in the group to the location of the first load in program order. When
1403// generating code for an interleaved store group, we sink all stores to the
1404// location of the last store. This code motion can change the order of load
1405// and store instructions and may break dependences.
1406//
1407// The code generation strategy mentioned above ensures that we won't violate
1408// any write-after-read (WAR) dependences.
1409//
1410// E.g., for the WAR dependence: a = A[i]; // (1)
1411// A[i] = b; // (2)
1412//
1413// The store group of (2) is always inserted at or below (2), and the load
1414// group of (1) is always inserted at or above (1). Thus, the instructions will
1415// never be reordered. All other dependences are checked to ensure the
1416// correctness of the instruction reordering.
1417//
1418// The algorithm visits all memory accesses in the loop in bottom-up program
1419// order. Program order is established by traversing the blocks in the loop in
1420// reverse postorder when collecting the accesses.
1421//
1422// We visit the memory accesses in bottom-up order because it can simplify the
1423// construction of store groups in the presence of write-after-write (WAW)
1424// dependences.
1425//
1426// E.g., for the WAW dependence: A[i] = a; // (1)
1427// A[i] = b; // (2)
1428// A[i + 1] = c; // (3)
1429//
1430// We will first create a store group with (3) and (2). (1) can't be added to
1431// this group because it and (2) are dependent. However, (1) can be grouped
1432// with other accesses that may precede it in program order. Note that a
1433// bottom-up order does not imply that WAW dependences should not be checked.
1435 bool EnablePredicatedInterleavedMemAccesses) {
1436 LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n");
1437 const auto &Strides = LAI->getSymbolicStrides();
1438
1439 // Holds all accesses with a constant stride.
1441 collectConstStrideAccesses(AccessStrideInfo, Strides);
1442
1443 if (AccessStrideInfo.empty())
1444 return;
1445
1446 // Collect the dependences in the loop.
1447 collectDependences();
1448
1449 // Holds all interleaved store groups temporarily.
1451 // Holds all interleaved load groups temporarily.
1453 // Groups added to this set cannot have new members added.
1454 SmallPtrSet<InterleaveGroup<Instruction> *, 4> CompletedLoadGroups;
1455
1456 // Search in bottom-up program order for pairs of accesses (A and B) that can
1457 // form interleaved load or store groups. In the algorithm below, access A
1458 // precedes access B in program order. We initialize a group for B in the
1459 // outer loop of the algorithm, and then in the inner loop, we attempt to
1460 // insert each A into B's group if:
1461 //
1462 // 1. A and B have the same stride,
1463 // 2. A and B have the same memory object size, and
1464 // 3. A belongs in B's group according to its distance from B.
1465 //
1466 // Special care is taken to ensure group formation will not break any
1467 // dependences.
1468 for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend();
1469 BI != E; ++BI) {
1470 Instruction *B = BI->first;
1471 StrideDescriptor DesB = BI->second;
1472
1473 // Initialize a group for B if it has an allowable stride. Even if we don't
1474 // create a group for B, we continue with the bottom-up algorithm to ensure
1475 // we don't break any of B's dependences.
1476 InterleaveGroup<Instruction> *GroupB = nullptr;
1477 if (isStrided(DesB.Stride) &&
1478 (!isPredicated(B->getParent()) || EnablePredicatedInterleavedMemAccesses)) {
1479 GroupB = getInterleaveGroup(B);
1480 if (!GroupB) {
1481 LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B
1482 << '\n');
1483 GroupB = createInterleaveGroup(B, DesB.Stride, DesB.Alignment);
1484 if (B->mayWriteToMemory())
1485 StoreGroups.insert(GroupB);
1486 else
1487 LoadGroups.insert(GroupB);
1488 }
1489 }
1490
1491 for (auto AI = std::next(BI); AI != E; ++AI) {
1492 Instruction *A = AI->first;
1493 StrideDescriptor DesA = AI->second;
1494
1495 // Our code motion strategy implies that we can't have dependences
1496 // between accesses in an interleaved group and other accesses located
1497 // between the first and last member of the group. Note that this also
1498 // means that a group can't have more than one member at a given offset.
1499 // The accesses in a group can have dependences with other accesses, but
1500 // we must ensure we don't extend the boundaries of the group such that
1501 // we encompass those dependent accesses.
1502 //
1503 // For example, assume we have the sequence of accesses shown below in a
1504 // stride-2 loop:
1505 //
1506 // (1, 2) is a group | A[i] = a; // (1)
1507 // | A[i-1] = b; // (2) |
1508 // A[i-3] = c; // (3)
1509 // A[i] = d; // (4) | (2, 4) is not a group
1510 //
1511 // Because accesses (2) and (3) are dependent, we can group (2) with (1)
1512 // but not with (4). If we did, the dependent access (3) would be within
1513 // the boundaries of the (2, 4) group.
1514 auto DependentMember = [&](InterleaveGroup<Instruction> *Group,
1515 StrideEntry *A) -> Instruction * {
1516 for (uint32_t Index = 0; Index < Group->getFactor(); ++Index) {
1517 Instruction *MemberOfGroupB = Group->getMember(Index);
1518 if (MemberOfGroupB && !canReorderMemAccessesForInterleavedGroups(
1519 A, &*AccessStrideInfo.find(MemberOfGroupB)))
1520 return MemberOfGroupB;
1521 }
1522 return nullptr;
1523 };
1524
1525 auto GroupA = getInterleaveGroup(A);
1526 // If A is a load, dependencies are tolerable, there's nothing to do here.
1527 // If both A and B belong to the same (store) group, they are independent,
1528 // even if dependencies have not been recorded.
1529 // If both GroupA and GroupB are null, there's nothing to do here.
1530 if (A->mayWriteToMemory() && GroupA != GroupB) {
1531 Instruction *DependentInst = nullptr;
1532 // If GroupB is a load group, we have to compare AI against all
1533 // members of GroupB because if any load within GroupB has a dependency
1534 // on AI, we need to mark GroupB as complete and also release the
1535 // store GroupA (if A belongs to one). The former prevents incorrect
1536 // hoisting of load B above store A while the latter prevents incorrect
1537 // sinking of store A below load B.
1538 if (GroupB && LoadGroups.contains(GroupB))
1539 DependentInst = DependentMember(GroupB, &*AI);
1540 else if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI))
1541 DependentInst = B;
1542
1543 if (DependentInst) {
1544 // A has a store dependence on B (or on some load within GroupB) and
1545 // is part of a store group. Release A's group to prevent illegal
1546 // sinking of A below B. A will then be free to form another group
1547 // with instructions that precede it.
1548 if (GroupA && StoreGroups.contains(GroupA)) {
1549 LLVM_DEBUG(dbgs() << "LV: Invalidated store group due to "
1550 "dependence between "
1551 << *A << " and " << *DependentInst << '\n');
1552 StoreGroups.remove(GroupA);
1553 releaseGroup(GroupA);
1554 }
1555 // If B is a load and part of an interleave group, no earlier loads
1556 // can be added to B's interleave group, because this would mean the
1557 // DependentInst would move across store A. Mark the interleave group
1558 // as complete.
1559 if (GroupB && LoadGroups.contains(GroupB)) {
1560 LLVM_DEBUG(dbgs() << "LV: Marking interleave group for " << *B
1561 << " as complete.\n");
1562 CompletedLoadGroups.insert(GroupB);
1563 }
1564 }
1565 }
1566 if (CompletedLoadGroups.contains(GroupB)) {
1567 // Skip trying to add A to B, continue to look for other conflicting A's
1568 // in groups to be released.
1569 continue;
1570 }
1571
1572 // At this point, we've checked for illegal code motion. If either A or B
1573 // isn't strided, there's nothing left to do.
1574 if (!isStrided(DesA.Stride) || !isStrided(DesB.Stride))
1575 continue;
1576
1577 // Ignore A if it's already in a group or isn't the same kind of memory
1578 // operation as B.
1579 // Note that mayReadFromMemory() isn't mutually exclusive to
1580 // mayWriteToMemory in the case of atomic loads. We shouldn't see those
1581 // here, canVectorizeMemory() should have returned false - except for the
1582 // case we asked for optimization remarks.
1583 if (isInterleaved(A) ||
1584 (A->mayReadFromMemory() != B->mayReadFromMemory()) ||
1585 (A->mayWriteToMemory() != B->mayWriteToMemory()))
1586 continue;
1587
1588 // Check rules 1 and 2. Ignore A if its stride or size is different from
1589 // that of B.
1590 if (DesA.Stride != DesB.Stride || DesA.Size != DesB.Size)
1591 continue;
1592
1593 // Ignore A if the memory object of A and B don't belong to the same
1594 // address space
1596 continue;
1597
1598 // Calculate the distance from A to B.
1599 const SCEVConstant *DistToB = dyn_cast<SCEVConstant>(
1600 PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev));
1601 if (!DistToB)
1602 continue;
1603 int64_t DistanceToB = DistToB->getAPInt().getSExtValue();
1604
1605 // Check rule 3. Ignore A if its distance to B is not a multiple of the
1606 // size.
1607 if (DistanceToB % static_cast<int64_t>(DesB.Size))
1608 continue;
1609
1610 // All members of a predicated interleave-group must have the same predicate,
1611 // and currently must reside in the same BB.
1612 BasicBlock *BlockA = A->getParent();
1613 BasicBlock *BlockB = B->getParent();
1614 if ((isPredicated(BlockA) || isPredicated(BlockB)) &&
1615 (!EnablePredicatedInterleavedMemAccesses || BlockA != BlockB))
1616 continue;
1617
1618 // The index of A is the index of B plus A's distance to B in multiples
1619 // of the size.
1620 int IndexA =
1621 GroupB->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size);
1622
1623 // Try to insert A into B's group.
1624 if (GroupB->insertMember(A, IndexA, DesA.Alignment)) {
1625 LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n'
1626 << " into the interleave group with" << *B
1627 << '\n');
1628 InterleaveGroupMap[A] = GroupB;
1629
1630 // Set the first load in program order as the insert position.
1631 if (A->mayReadFromMemory())
1632 GroupB->setInsertPos(A);
1633 }
1634 } // Iteration over A accesses.
1635 } // Iteration over B accesses.
1636
1637 auto InvalidateGroupIfMemberMayWrap = [&](InterleaveGroup<Instruction> *Group,
1638 int Index,
1639 const char *FirstOrLast) -> bool {
1640 Instruction *Member = Group->getMember(Index);
1641 assert(Member && "Group member does not exist");
1642 Value *MemberPtr = getLoadStorePointerOperand(Member);
1643 Type *AccessTy = getLoadStoreType(Member);
1644 if (getPtrStride(PSE, AccessTy, MemberPtr, TheLoop, Strides,
1645 /*Assume=*/false, /*ShouldCheckWrap=*/true).value_or(0))
1646 return false;
1647 LLVM_DEBUG(dbgs() << "LV: Invalidate candidate interleaved group due to "
1648 << FirstOrLast
1649 << " group member potentially pointer-wrapping.\n");
1650 releaseGroup(Group);
1651 return true;
1652 };
1653
1654 // Remove interleaved groups with gaps whose memory
1655 // accesses may wrap around. We have to revisit the getPtrStride analysis,
1656 // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does
1657 // not check wrapping (see documentation there).
1658 // FORNOW we use Assume=false;
1659 // TODO: Change to Assume=true but making sure we don't exceed the threshold
1660 // of runtime SCEV assumptions checks (thereby potentially failing to
1661 // vectorize altogether).
1662 // Additional optional optimizations:
1663 // TODO: If we are peeling the loop and we know that the first pointer doesn't
1664 // wrap then we can deduce that all pointers in the group don't wrap.
1665 // This means that we can forcefully peel the loop in order to only have to
1666 // check the first pointer for no-wrap. When we'll change to use Assume=true
1667 // we'll only need at most one runtime check per interleaved group.
1668 for (auto *Group : LoadGroups) {
1669 // Case 1: A full group. Can Skip the checks; For full groups, if the wide
1670 // load would wrap around the address space we would do a memory access at
1671 // nullptr even without the transformation.
1672 if (Group->isFull())
1673 continue;
1674
1675 // Case 2: If first and last members of the group don't wrap this implies
1676 // that all the pointers in the group don't wrap.
1677 // So we check only group member 0 (which is always guaranteed to exist),
1678 // and group member Factor - 1; If the latter doesn't exist we rely on
1679 // peeling (if it is a non-reversed access -- see Case 3).
1680 if (InvalidateGroupIfMemberMayWrap(Group, 0, "first"))
1681 continue;
1682 if (Group->getMember(Group->getFactor() - 1))
1683 InvalidateGroupIfMemberMayWrap(Group, Group->getFactor() - 1, "last");
1684 else {
1685 // Case 3: A non-reversed interleaved load group with gaps: We need
1686 // to execute at least one scalar epilogue iteration. This will ensure
1687 // we don't speculatively access memory out-of-bounds. We only need
1688 // to look for a member at index factor - 1, since every group must have
1689 // a member at index zero.
1690 if (Group->isReverse()) {
1691 LLVM_DEBUG(
1692 dbgs() << "LV: Invalidate candidate interleaved group due to "
1693 "a reverse access with gaps.\n");
1694 releaseGroup(Group);
1695 continue;
1696 }
1697 LLVM_DEBUG(
1698 dbgs() << "LV: Interleaved group requires epilogue iteration.\n");
1699 RequiresScalarEpilogue = true;
1700 }
1701 }
1702
1703 for (auto *Group : StoreGroups) {
1704 // Case 1: A full group. Can Skip the checks; For full groups, if the wide
1705 // store would wrap around the address space we would do a memory access at
1706 // nullptr even without the transformation.
1707 if (Group->isFull())
1708 continue;
1709
1710 // Interleave-store-group with gaps is implemented using masked wide store.
1711 // Remove interleaved store groups with gaps if
1712 // masked-interleaved-accesses are not enabled by the target.
1713 if (!EnablePredicatedInterleavedMemAccesses) {
1714 LLVM_DEBUG(
1715 dbgs() << "LV: Invalidate candidate interleaved store group due "
1716 "to gaps.\n");
1717 releaseGroup(Group);
1718 continue;
1719 }
1720
1721 // Case 2: If first and last members of the group don't wrap this implies
1722 // that all the pointers in the group don't wrap.
1723 // So we check only group member 0 (which is always guaranteed to exist),
1724 // and the last group member. Case 3 (scalar epilog) is not relevant for
1725 // stores with gaps, which are implemented with masked-store (rather than
1726 // speculative access, as in loads).
1727 if (InvalidateGroupIfMemberMayWrap(Group, 0, "first"))
1728 continue;
1729 for (int Index = Group->getFactor() - 1; Index > 0; Index--)
1730 if (Group->getMember(Index)) {
1731 InvalidateGroupIfMemberMayWrap(Group, Index, "last");
1732 break;
1733 }
1734 }
1735}
1736
1738 // If no group had triggered the requirement to create an epilogue loop,
1739 // there is nothing to do.
1741 return;
1742
1743 // Release groups requiring scalar epilogues. Note that this also removes them
1744 // from InterleaveGroups.
1745 bool ReleasedGroup = InterleaveGroups.remove_if([&](auto *Group) {
1746 if (!Group->requiresScalarEpilogue())
1747 return false;
1748 LLVM_DEBUG(
1749 dbgs()
1750 << "LV: Invalidate candidate interleaved group due to gaps that "
1751 "require a scalar epilogue (not allowed under optsize) and cannot "
1752 "be masked (not enabled). \n");
1753 releaseGroupWithoutRemovingFromSet(Group);
1754 return true;
1755 });
1756 assert(ReleasedGroup && "At least one group must be invalidated, as a "
1757 "scalar epilogue was required");
1758 (void)ReleasedGroup;
1759 RequiresScalarEpilogue = false;
1760}
1761
1762template <typename InstT>
1763void InterleaveGroup<InstT>::addMetadata(InstT *NewInst) const {
1764 llvm_unreachable("addMetadata can only be used for Instruction");
1765}
1766
1767namespace llvm {
1768template <>
1771 propagateMetadata(NewInst, VL);
1772}
1773} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Size
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const NodeList & List
Definition: RDFGraph.cpp:200
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition: Debug.h:119
static unsigned getScalarSizeInBits(Type *Ty)
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
static Value * concatenateTwoVectors(IRBuilderBase &Builder, Value *V1, Value *V2)
A helper function for concatenating vectors.
static cl::opt< unsigned > MaxInterleaveGroupFactor("max-interleave-group-factor", cl::Hidden, cl::desc("Maximum factor for an interleaved access group (default = 8)"), cl::init(8))
Maximum factor for an interleaved memory access.
static void addToAccessGroupList(ListT &List, MDNode *AccGroups)
Add all access groups in AccGroups to List.
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1406
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1562
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:150
iterator end() const
Definition: ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
iterator begin() const
Definition: ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
Definition: BasicBlock.cpp:252
This class represents a function call, abstracting a target machine's calling convention.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1423
This is an important base class in LLVM.
Definition: Constant.h:43
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:173
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
iterator_range< member_iterator > members(const ECValue &ECV) const
const ElemTy & getOrInsertLeaderValue(const ElemTy &V)
getOrInsertLeaderValue - Return the leader for the specified value that is in the set.
member_iterator unionSets(const ElemTy &V1, const ElemTy &V2)
union - Merge the two equivalence sets for the specified values, inserting them if they do not alread...
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:497
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2593
This instruction inserts a single (scalar) element into a VectorType value.
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
Definition: Instruction.h:808
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:428
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1718
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
This does the same thing as getAllMetadata, except that it filters out the debug location.
Definition: Instruction.h:453
The group of interleaved loads/stores sharing the same stride and close to each other.
Definition: VectorUtils.h:524
uint32_t getFactor() const
Definition: VectorUtils.h:540
InstTy * getMember(uint32_t Index) const
Get the member with the given index Index.
Definition: VectorUtils.h:594
bool isFull() const
Return true if this group is full, i.e. it has no gaps.
Definition: VectorUtils.h:637
uint32_t getIndex(const InstTy *Instr) const
Get the index for the given member.
Definition: VectorUtils.h:601
void setInsertPos(InstTy *Inst)
Definition: VectorUtils.h:611
bool isReverse() const
Definition: VectorUtils.h:539
void addMetadata(InstTy *NewInst) const
Add metadata (e.g.
bool insertMember(InstTy *Instr, int32_t Index, Align NewAlign)
Try to insert a new member Instr with index Index and alignment NewAlign.
Definition: VectorUtils.h:549
InterleaveGroup< Instruction > * getInterleaveGroup(const Instruction *Instr) const
Get the interleave group that Instr belongs to.
Definition: VectorUtils.h:714
bool requiresScalarEpilogue() const
Returns true if an interleaved group that may access memory out-of-bounds requires a scalar epilogue ...
Definition: VectorUtils.h:725
bool isInterleaved(Instruction *Instr) const
Check if Instr belongs to any interleave group.
Definition: VectorUtils.h:706
LLVM_ABI void analyzeInterleaving(bool EnableMaskedInterleavedGroup)
Analyze the interleaved accesses and collect them in interleave groups.
LLVM_ABI void invalidateGroupsRequiringScalarEpilogue()
Invalidate groups that require a scalar epilogue (due to gaps).
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
const DenseMap< Value *, const SCEV * > & getSymbolicStrides() const
If an access has a symbolic strides, this maps the pointer value to the stride symbol.
BlockT * getHeader() const
Store the result of a depth first search within basic blocks contained by a single loop.
Definition: LoopIterator.h:97
Metadata node.
Definition: Metadata.h:1077
static LLVM_ABI MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1142
static LLVM_ABI MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1443
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1565
static LLVM_ABI MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1174
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1451
static LLVM_ABI MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1129
LLVMContext & getContext() const
Definition: Metadata.h:1241
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:899
static LLVM_ABI MDNode * combine(LLVMContext &Ctx, const MMRAMetadata &A, const MMRAMetadata &B)
Combines A and B according to MMRA semantics.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
reverse_iterator rend()
Definition: MapVector.h:72
iterator find(const KeyT &Key)
Definition: MapVector.h:141
bool empty() const
Definition: MapVector.h:75
reverse_iterator rbegin()
Definition: MapVector.h:70
Root of the metadata hierarchy.
Definition: Metadata.h:63
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
This class represents a constant integer value.
const APInt & getAPInt() const
This class represents an analyzed expression in the program.
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:198
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:168
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:269
This instruction constructs a fixed permutation of two input vectors.
int getMaskValue(unsigned Elt) const
Return the shuffle mask value of this instruction for the given element index.
VectorType * getType() const
Overload to return most specific vector type.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:476
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:356
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:705
void reserve(size_type N)
Definition: SmallVector.h:664
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
LLVM_ABI bool isTargetIntrinsicWithStructReturnOverloadAtField(Intrinsic::ID ID, int RetIdx) const
Identifies if the vector form of the intrinsic that returns a struct is overloaded at the struct elem...
LLVM_ABI bool isTargetIntrinsicTriviallyScalarizable(Intrinsic::ID ID) const
LLVM_ABI bool isTargetIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx) const
Identifies if the vector form of the intrinsic has a scalar operand.
LLVM_ABI bool isTargetIntrinsicWithOverloadTypeAtArg(Intrinsic::ID ID, int OpdIdx) const
Identifies if the vector form of the intrinsic is overloaded on the type of the operand at index OpdI...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
ArrayRef< Type * > subtypes() const
Definition: Type.h:365
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
Value * getOperand(unsigned i) const
Definition: User.h:232
static LLVM_ABI bool isVPCast(Intrinsic::ID ID)
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
Type * getElementType() const
Definition: DerivedTypes.h:463
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LLVM_ABI bool isTargetIntrinsic(ID IID)
isTargetIntrinsic - Returns true if IID is an intrinsic specific to a certain target.
Definition: Intrinsics.cpp:629
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool isTriviallyScalarizable(Intrinsic::ID ID, const TargetTransformInfo *TTI)
Identify if the intrinsic is trivially scalarizable.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
unsigned getLoadStoreAddressSpace(const Value *I)
A helper function that returns the address space of the pointer operand of load or store instruction.
LLVM_ABI Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
LLVM_ABI APInt possiblyDemandedEltsInMask(Value *Mask)
Given a mask vector of the form <Y x i1>, return an APInt (of bitwidth Y) for each lane which may be ...
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI llvm::SmallVector< int, 16 > createUnaryMask(ArrayRef< int > Mask, unsigned NumElts)
Given a shuffle mask for a binary shuffle, create the equivalent shuffle mask assuming both operands ...
LLVM_ABI void getMetadataToPropagate(Instruction *Inst, SmallVectorImpl< std::pair< unsigned, MDNode * > > &Metadata)
Add metadata from Inst to Metadata, if it can be preserved after vectorization.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:270
LLVM_ABI Value * concatenateVectors(IRBuilderBase &Builder, ArrayRef< Value * > Vecs)
Concatenate a list of vectors.
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
LLVM_ABI bool widenShuffleMaskElts(int Scale, ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Try to transform a shuffle mask by replacing elements with the scaled index for an equivalent mask of...
LLVM_ABI Instruction * propagateMetadata(Instruction *I, ArrayRef< Value * > VL)
Specifically, let Kinds = [MD_tbaa, MD_alias_scope, MD_noalias, MD_fpmath, MD_nontemporal,...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition: bit.h:295
LLVM_ABI MDNode * intersectAccessGroups(const Instruction *Inst1, const Instruction *Inst2)
Compute the access-group list of access groups that Inst1 and Inst2 are both in.
unsigned M1(unsigned Val)
Definition: VE.h:377
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:1751
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
LLVM_ABI Constant * createBitMaskForGaps(IRBuilderBase &Builder, unsigned VF, const InterleaveGroup< Instruction > &Group)
Create a mask that filters the members of an interleave group where there are gaps.
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:47
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI llvm::SmallVector< int, 16 > createReplicatedMask(unsigned ReplicationFactor, unsigned VF)
Create a mask with replicated elements.
LLVM_ABI unsigned getDeinterleaveIntrinsicFactor(Intrinsic::ID ID)
Returns the corresponding factor of llvm.vector.deinterleaveN intrinsics.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI std::optional< int64_t > getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp, const DenseMap< Value *, const SCEV * > &StridesMap=DenseMap< Value *, const SCEV * >(), bool Assume=false, bool ShouldCheckWrap=true)
If the pointer has a constant stride return it in units of the access type size.
LLVM_ABI unsigned getInterleaveIntrinsicFactor(Intrinsic::ID ID)
Returns the corresponding factor of llvm.vector.interleaveN intrinsics.
LLVM_ABI bool maskIsAllOneOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
constexpr int PoisonMaskElem
LLVM_ABI bool isValidAsAccessGroup(MDNode *AccGroup)
Return whether an MDNode might represent an access group.
Definition: LoopInfo.cpp:1178
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
LLVM_ABI bool isVectorIntrinsicWithStructReturnOverloadAtField(Intrinsic::ID ID, int RetIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic that returns a struct is overloaded at the struct elem...
LLVM_ABI void narrowShuffleMaskElts(int Scale, ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Replace each shuffle mask index with the scaled sequential indices for an equivalent mask of narrowed...
LLVM_ABI bool isMaskedSlidePair(ArrayRef< int > Mask, int NumElts, std::array< std::pair< int, int >, 2 > &SrcInfo)
Does this shuffle mask represent either one slide shuffle or a pair of two slide shuffles,...
LLVM_ABI VectorType * getDeinterleavedVectorType(IntrinsicInst *DI)
Given a deinterleaveN intrinsic, return the (narrow) vector type of each factor.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
LLVM_ABI bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic has a scalar operand.
LLVM_ABI const SCEV * replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &PtrToStride, Value *Ptr)
Return the SCEV corresponding to a pointer with the symbolic stride replaced with constant one,...
LLVM_ABI Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register,...
LLVM_ABI MDNode * uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2)
Compute the union of two access-group lists.
unsigned M0(unsigned Val)
Definition: VE.h:376
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
Definition: STLExtras.h:1454
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1980
LLVM_ABI bool maskIsAllZeroOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1777
LLVM_ABI void getShuffleMaskWithWidestElts(ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Repetitively apply widenShuffleMaskElts() for as long as it succeeds, to get the shuffle mask with wi...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
LLVM_ABI void processShuffleMasks(ArrayRef< int > Mask, unsigned NumOfSrcRegs, unsigned NumOfDestRegs, unsigned NumOfUsedRegs, function_ref< void()> NoInputAction, function_ref< void(ArrayRef< int >, unsigned, unsigned)> SingleInputAction, function_ref< void(ArrayRef< int >, unsigned, unsigned, bool)> ManyInputsAction)
Splits and processes shuffle mask depending on the number of input and output registers.
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition: STLExtras.h:2127
LLVM_ABI bool maskContainsAllOneOrUndef(Value *Mask)
Given a mask vector of i1, Return true if any of the elements of this predicate mask are known to be ...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
LLVM_ABI llvm::SmallVector< int, 16 > createSequentialMask(unsigned Start, unsigned NumInts, unsigned NumUndefs)
Create a sequential shuffle mask.
LLVM_ABI bool isVectorIntrinsicWithOverloadTypeAtArg(Intrinsic::ID ID, int OpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic is overloaded on the type of the operand at index OpdI...
LLVM_ABI MapVector< Instruction *, uint64_t > computeMinimumValueSizes(ArrayRef< BasicBlock * > Blocks, DemandedBits &DB, const TargetTransformInfo *TTI=nullptr)
Compute a map of integer instructions to their minimum legal type size.
LLVM_ABI bool scaleShuffleMaskElts(unsigned NumDstElts, ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Attempt to narrow/widen the Mask shuffle mask to the NumDstElts target width.
LLVM_ABI int getSplatIndex(ArrayRef< int > Mask)
If all non-negative Mask elements are the same value, return that value.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858