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