LLVM  10.0.0
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 
16 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/IR/Value.h"
28 
29 #define DEBUG_TYPE "vectorutils"
30 
31 using namespace llvm;
32 using namespace llvm::PatternMatch;
33 
34 /// Maximum factor for an interleaved memory access.
36  "max-interleave-group-factor", cl::Hidden,
37  cl::desc("Maximum factor for an interleaved access group (default = 8)"),
38  cl::init(8));
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 /// hasVectorInstrinsicScalarOpd).
45  switch (ID) {
46  case Intrinsic::bswap: // Begin integer bit-manipulation.
47  case Intrinsic::bitreverse:
48  case Intrinsic::ctpop:
49  case Intrinsic::ctlz:
50  case Intrinsic::cttz:
51  case Intrinsic::fshl:
52  case Intrinsic::fshr:
53  case Intrinsic::sadd_sat:
54  case Intrinsic::ssub_sat:
55  case Intrinsic::uadd_sat:
56  case Intrinsic::usub_sat:
57  case Intrinsic::smul_fix:
58  case Intrinsic::smul_fix_sat:
59  case Intrinsic::umul_fix:
60  case Intrinsic::umul_fix_sat:
61  case Intrinsic::sqrt: // Begin floating-point.
62  case Intrinsic::sin:
63  case Intrinsic::cos:
64  case Intrinsic::exp:
65  case Intrinsic::exp2:
66  case Intrinsic::log:
67  case Intrinsic::log10:
68  case Intrinsic::log2:
69  case Intrinsic::fabs:
70  case Intrinsic::minnum:
71  case Intrinsic::maxnum:
72  case Intrinsic::minimum:
73  case Intrinsic::maximum:
74  case Intrinsic::copysign:
75  case Intrinsic::floor:
76  case Intrinsic::ceil:
77  case Intrinsic::trunc:
78  case Intrinsic::rint:
79  case Intrinsic::nearbyint:
80  case Intrinsic::round:
81  case Intrinsic::pow:
82  case Intrinsic::fma:
83  case Intrinsic::fmuladd:
84  case Intrinsic::powi:
85  case Intrinsic::canonicalize:
86  return true;
87  default:
88  return false;
89  }
90 }
91 
92 /// Identifies if the vector form of the intrinsic has a scalar operand.
94  unsigned ScalarOpdIdx) {
95  switch (ID) {
96  case Intrinsic::ctlz:
97  case Intrinsic::cttz:
98  case Intrinsic::powi:
99  return (ScalarOpdIdx == 1);
100  case Intrinsic::smul_fix:
101  case Intrinsic::smul_fix_sat:
102  case Intrinsic::umul_fix:
103  case Intrinsic::umul_fix_sat:
104  return (ScalarOpdIdx == 2);
105  default:
106  return false;
107  }
108 }
109 
110 /// Returns intrinsic ID for call.
111 /// For the input call instruction it finds mapping intrinsic and returns
112 /// its ID, in case it does not found it return not_intrinsic.
114  const TargetLibraryInfo *TLI) {
116  if (ID == Intrinsic::not_intrinsic)
118 
119  if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
120  ID == Intrinsic::lifetime_end || ID == Intrinsic::assume ||
121  ID == Intrinsic::sideeffect)
122  return ID;
124 }
125 
126 /// Find the operand of the GEP that should be checked for consecutive
127 /// stores. This ignores trailing indices that have no effect on the final
128 /// pointer.
130  const DataLayout &DL = Gep->getModule()->getDataLayout();
131  unsigned LastOperand = Gep->getNumOperands() - 1;
132  unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());
133 
134  // Walk backwards and try to peel off zeros.
135  while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) {
136  // Find the type we're currently indexing into.
137  gep_type_iterator GEPTI = gep_type_begin(Gep);
138  std::advance(GEPTI, LastOperand - 2);
139 
140  // If it's a type with the same allocation size as the result of the GEP we
141  // can peel off the zero index.
142  if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize)
143  break;
144  --LastOperand;
145  }
146 
147  return LastOperand;
148 }
149 
150 /// If the argument is a GEP, then returns the operand identified by
151 /// getGEPInductionOperand. However, if there is some other non-loop-invariant
152 /// operand, it returns that instead.
155  if (!GEP)
156  return Ptr;
157 
158  unsigned InductionOperand = getGEPInductionOperand(GEP);
159 
160  // Check that all of the gep indices are uniform except for our induction
161  // operand.
162  for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
163  if (i != InductionOperand &&
164  !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
165  return Ptr;
166  return GEP->getOperand(InductionOperand);
167 }
168 
169 /// If a value has only one user that is a CastInst, return it.
171  Value *UniqueCast = nullptr;
172  for (User *U : Ptr->users()) {
173  CastInst *CI = dyn_cast<CastInst>(U);
174  if (CI && CI->getType() == Ty) {
175  if (!UniqueCast)
176  UniqueCast = CI;
177  else
178  return nullptr;
179  }
180  }
181  return UniqueCast;
182 }
183 
184 /// Get the stride of a pointer access in a loop. Looks for symbolic
185 /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
187  auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
188  if (!PtrTy || PtrTy->isAggregateType())
189  return nullptr;
190 
191  // Try to remove a gep instruction to make the pointer (actually index at this
192  // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the
193  // pointer, otherwise, we are analyzing the index.
194  Value *OrigPtr = Ptr;
195 
196  // The size of the pointer access.
197  int64_t PtrAccessSize = 1;
198 
199  Ptr = stripGetElementPtr(Ptr, SE, Lp);
200  const SCEV *V = SE->getSCEV(Ptr);
201 
202  if (Ptr != OrigPtr)
203  // Strip off casts.
204  while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V))
205  V = C->getOperand();
206 
207  const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
208  if (!S)
209  return nullptr;
210 
211  V = S->getStepRecurrence(*SE);
212  if (!V)
213  return nullptr;
214 
215  // Strip off the size of access multiplication if we are still analyzing the
216  // pointer.
217  if (OrigPtr == Ptr) {
218  if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
219  if (M->getOperand(0)->getSCEVType() != scConstant)
220  return nullptr;
221 
222  const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
223 
224  // Huge step value - give up.
225  if (APStepVal.getBitWidth() > 64)
226  return nullptr;
227 
228  int64_t StepVal = APStepVal.getSExtValue();
229  if (PtrAccessSize != StepVal)
230  return nullptr;
231  V = M->getOperand(1);
232  }
233  }
234 
235  // Strip off casts.
236  Type *StripedOffRecurrenceCast = nullptr;
237  if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
238  StripedOffRecurrenceCast = C->getType();
239  V = C->getOperand();
240  }
241 
242  // Look for the loop invariant symbolic value.
243  const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
244  if (!U)
245  return nullptr;
246 
247  Value *Stride = U->getValue();
248  if (!Lp->isLoopInvariant(Stride))
249  return nullptr;
250 
251  // If we have stripped off the recurrence cast we have to make sure that we
252  // return the value that is used in this loop so that we can replace it later.
253  if (StripedOffRecurrenceCast)
254  Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
255 
256  return Stride;
257 }
258 
259 /// Given a vector and an element number, see if the scalar value is
260 /// already around as a register, for example if it were inserted then extracted
261 /// from the vector.
262 Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
263  assert(V->getType()->isVectorTy() && "Not looking at a vector?");
264  VectorType *VTy = cast<VectorType>(V->getType());
265  unsigned Width = VTy->getNumElements();
266  if (EltNo >= Width) // Out of range access.
267  return UndefValue::get(VTy->getElementType());
268 
269  if (Constant *C = dyn_cast<Constant>(V))
270  return C->getAggregateElement(EltNo);
271 
272  if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
273  // If this is an insert to a variable element, we don't know what it is.
274  if (!isa<ConstantInt>(III->getOperand(2)))
275  return nullptr;
276  unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
277 
278  // If this is an insert to the element we are looking for, return the
279  // inserted value.
280  if (EltNo == IIElt)
281  return III->getOperand(1);
282 
283  // Otherwise, the insertelement doesn't modify the value, recurse on its
284  // vector input.
285  return findScalarElement(III->getOperand(0), EltNo);
286  }
287 
288  if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
289  unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
290  int InEl = SVI->getMaskValue(EltNo);
291  if (InEl < 0)
292  return UndefValue::get(VTy->getElementType());
293  if (InEl < (int)LHSWidth)
294  return findScalarElement(SVI->getOperand(0), InEl);
295  return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
296  }
297 
298  // Extract a value from a vector add operation with a constant zero.
299  // TODO: Use getBinOpIdentity() to generalize this.
300  Value *Val; Constant *C;
301  if (match(V, m_Add(m_Value(Val), m_Constant(C))))
302  if (Constant *Elt = C->getAggregateElement(EltNo))
303  if (Elt->isNullValue())
304  return findScalarElement(Val, EltNo);
305 
306  // Otherwise, we don't know.
307  return nullptr;
308 }
309 
310 /// Get splat value if the input is a splat vector or return nullptr.
311 /// This function is not fully general. It checks only 2 cases:
312 /// the input value is (1) a splat constant vector or (2) a sequence
313 /// of instructions that broadcasts a scalar at element 0.
315  if (isa<VectorType>(V->getType()))
316  if (auto *C = dyn_cast<Constant>(V))
317  return C->getSplatValue();
318 
319  // shuf (inselt ?, Splat, 0), ?, <0, undef, 0, ...>
320  Value *Splat;
322  m_ZeroInt()),
323  m_Value(), m_ZeroInt())))
324  return Splat;
325 
326  return nullptr;
327 }
328 
329 // This setting is based on its counterpart in value tracking, but it could be
330 // adjusted if needed.
331 const unsigned MaxDepth = 6;
332 
333 bool llvm::isSplatValue(const Value *V, unsigned Depth) {
334  assert(Depth <= MaxDepth && "Limit Search Depth");
335 
336  if (isa<VectorType>(V->getType())) {
337  if (isa<UndefValue>(V))
338  return true;
339  // FIXME: Constant splat analysis does not allow undef elements.
340  if (auto *C = dyn_cast<Constant>(V))
341  return C->getSplatValue() != nullptr;
342  }
343 
344  // FIXME: Constant splat analysis does not allow undef elements.
345  Constant *Mask;
346  if (match(V, m_ShuffleVector(m_Value(), m_Value(), m_Constant(Mask))))
347  return Mask->getSplatValue() != nullptr;
348 
349  // The remaining tests are all recursive, so bail out if we hit the limit.
350  if (Depth++ == MaxDepth)
351  return false;
352 
353  // If both operands of a binop are splats, the result is a splat.
354  Value *X, *Y, *Z;
355  if (match(V, m_BinOp(m_Value(X), m_Value(Y))))
356  return isSplatValue(X, Depth) && isSplatValue(Y, Depth);
357 
358  // If all operands of a select are splats, the result is a splat.
359  if (match(V, m_Select(m_Value(X), m_Value(Y), m_Value(Z))))
360  return isSplatValue(X, Depth) && isSplatValue(Y, Depth) &&
361  isSplatValue(Z, Depth);
362 
363  // TODO: Add support for unary ops (fneg), casts, intrinsics (overflow ops).
364 
365  return false;
366 }
367 
370  const TargetTransformInfo *TTI) {
371 
372  // DemandedBits will give us every value's live-out bits. But we want
373  // to ensure no extra casts would need to be inserted, so every DAG
374  // of connected values must have the same minimum bitwidth.
376  SmallVector<Value *, 16> Worklist;
378  SmallPtrSet<Value *, 16> Visited;
380  SmallPtrSet<Instruction *, 4> InstructionSet;
382 
383  // Determine the roots. We work bottom-up, from truncs or icmps.
384  bool SeenExtFromIllegalType = false;
385  for (auto *BB : Blocks)
386  for (auto &I : *BB) {
387  InstructionSet.insert(&I);
388 
389  if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) &&
390  !TTI->isTypeLegal(I.getOperand(0)->getType()))
391  SeenExtFromIllegalType = true;
392 
393  // Only deal with non-vector integers up to 64-bits wide.
394  if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) &&
395  !I.getType()->isVectorTy() &&
396  I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) {
397  // Don't make work for ourselves. If we know the loaded type is legal,
398  // don't add it to the worklist.
399  if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType()))
400  continue;
401 
402  Worklist.push_back(&I);
403  Roots.insert(&I);
404  }
405  }
406  // Early exit.
407  if (Worklist.empty() || (TTI && !SeenExtFromIllegalType))
408  return MinBWs;
409 
410  // Now proceed breadth-first, unioning values together.
411  while (!Worklist.empty()) {
412  Value *Val = Worklist.pop_back_val();
413  Value *Leader = ECs.getOrInsertLeaderValue(Val);
414 
415  if (Visited.count(Val))
416  continue;
417  Visited.insert(Val);
418 
419  // Non-instructions terminate a chain successfully.
420  if (!isa<Instruction>(Val))
421  continue;
422  Instruction *I = cast<Instruction>(Val);
423 
424  // If we encounter a type that is larger than 64 bits, we can't represent
425  // it so bail out.
426  if (DB.getDemandedBits(I).getBitWidth() > 64)
428 
429  uint64_t V = DB.getDemandedBits(I).getZExtValue();
430  DBits[Leader] |= V;
431  DBits[I] = V;
432 
433  // Casts, loads and instructions outside of our range terminate a chain
434  // successfully.
435  if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) ||
436  !InstructionSet.count(I))
437  continue;
438 
439  // Unsafe casts terminate a chain unsuccessfully. We can't do anything
440  // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
441  // transform anything that relies on them.
442  if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
443  !I->getType()->isIntegerTy()) {
444  DBits[Leader] |= ~0ULL;
445  continue;
446  }
447 
448  // We don't modify the types of PHIs. Reductions will already have been
449  // truncated if possible, and inductions' sizes will have been chosen by
450  // indvars.
451  if (isa<PHINode>(I))
452  continue;
453 
454  if (DBits[Leader] == ~0ULL)
455  // All bits demanded, no point continuing.
456  continue;
457 
458  for (Value *O : cast<User>(I)->operands()) {
459  ECs.unionSets(Leader, O);
460  Worklist.push_back(O);
461  }
462  }
463 
464  // Now we've discovered all values, walk them to see if there are
465  // any users we didn't see. If there are, we can't optimize that
466  // chain.
467  for (auto &I : DBits)
468  for (auto *U : I.first->users())
469  if (U->getType()->isIntegerTy() && DBits.count(U) == 0)
470  DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
471 
472  for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) {
473  uint64_t LeaderDemandedBits = 0;
474  for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
475  LeaderDemandedBits |= DBits[*MI];
476 
477  uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) -
478  llvm::countLeadingZeros(LeaderDemandedBits);
479  // Round up to a power of 2
480  if (!isPowerOf2_64((uint64_t)MinBW))
481  MinBW = NextPowerOf2(MinBW);
482 
483  // We don't modify the types of PHIs. Reductions will already have been
484  // truncated if possible, and inductions' sizes will have been chosen by
485  // indvars.
486  // If we are required to shrink a PHI, abandon this entire equivalence class.
487  bool Abort = false;
488  for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
489  if (isa<PHINode>(*MI) && MinBW < (*MI)->getType()->getScalarSizeInBits()) {
490  Abort = true;
491  break;
492  }
493  if (Abort)
494  continue;
495 
496  for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) {
497  if (!isa<Instruction>(*MI))
498  continue;
499  Type *Ty = (*MI)->getType();
500  if (Roots.count(*MI))
501  Ty = cast<Instruction>(*MI)->getOperand(0)->getType();
502  if (MinBW < Ty->getScalarSizeInBits())
503  MinBWs[cast<Instruction>(*MI)] = MinBW;
504  }
505  }
506 
507  return MinBWs;
508 }
509 
510 /// Add all access groups in @p AccGroups to @p List.
511 template <typename ListT>
512 static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
513  // Interpret an access group as a list containing itself.
514  if (AccGroups->getNumOperands() == 0) {
515  assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
516  List.insert(AccGroups);
517  return;
518  }
519 
520  for (auto &AccGroupListOp : AccGroups->operands()) {
521  auto *Item = cast<MDNode>(AccGroupListOp.get());
522  assert(isValidAsAccessGroup(Item) && "List item must be an access group");
523  List.insert(Item);
524  }
525 }
526 
527 MDNode *llvm::uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2) {
528  if (!AccGroups1)
529  return AccGroups2;
530  if (!AccGroups2)
531  return AccGroups1;
532  if (AccGroups1 == AccGroups2)
533  return AccGroups1;
534 
536  addToAccessGroupList(Union, AccGroups1);
537  addToAccessGroupList(Union, AccGroups2);
538 
539  if (Union.size() == 0)
540  return nullptr;
541  if (Union.size() == 1)
542  return cast<MDNode>(Union.front());
543 
544  LLVMContext &Ctx = AccGroups1->getContext();
545  return MDNode::get(Ctx, Union.getArrayRef());
546 }
547 
549  const Instruction *Inst2) {
550  bool MayAccessMem1 = Inst1->mayReadOrWriteMemory();
551  bool MayAccessMem2 = Inst2->mayReadOrWriteMemory();
552 
553  if (!MayAccessMem1 && !MayAccessMem2)
554  return nullptr;
555  if (!MayAccessMem1)
556  return Inst2->getMetadata(LLVMContext::MD_access_group);
557  if (!MayAccessMem2)
558  return Inst1->getMetadata(LLVMContext::MD_access_group);
559 
560  MDNode *MD1 = Inst1->getMetadata(LLVMContext::MD_access_group);
561  MDNode *MD2 = Inst2->getMetadata(LLVMContext::MD_access_group);
562  if (!MD1 || !MD2)
563  return nullptr;
564  if (MD1 == MD2)
565  return MD1;
566 
567  // Use set for scalable 'contains' check.
568  SmallPtrSet<Metadata *, 4> AccGroupSet2;
569  addToAccessGroupList(AccGroupSet2, MD2);
570 
571  SmallVector<Metadata *, 4> Intersection;
572  if (MD1->getNumOperands() == 0) {
573  assert(isValidAsAccessGroup(MD1) && "Node must be an access group");
574  if (AccGroupSet2.count(MD1))
575  Intersection.push_back(MD1);
576  } else {
577  for (const MDOperand &Node : MD1->operands()) {
578  auto *Item = cast<MDNode>(Node.get());
579  assert(isValidAsAccessGroup(Item) && "List item must be an access group");
580  if (AccGroupSet2.count(Item))
581  Intersection.push_back(Item);
582  }
583  }
584 
585  if (Intersection.size() == 0)
586  return nullptr;
587  if (Intersection.size() == 1)
588  return cast<MDNode>(Intersection.front());
589 
590  LLVMContext &Ctx = Inst1->getContext();
591  return MDNode::get(Ctx, Intersection);
592 }
593 
594 /// \returns \p I after propagating metadata from \p VL.
596  Instruction *I0 = cast<Instruction>(VL[0]);
598  I0->getAllMetadataOtherThanDebugLoc(Metadata);
599 
600  for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
601  LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
602  LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
603  LLVMContext::MD_access_group}) {
604  MDNode *MD = I0->getMetadata(Kind);
605 
606  for (int J = 1, E = VL.size(); MD && J != E; ++J) {
607  const Instruction *IJ = cast<Instruction>(VL[J]);
608  MDNode *IMD = IJ->getMetadata(Kind);
609  switch (Kind) {
610  case LLVMContext::MD_tbaa:
611  MD = MDNode::getMostGenericTBAA(MD, IMD);
612  break;
613  case LLVMContext::MD_alias_scope:
614  MD = MDNode::getMostGenericAliasScope(MD, IMD);
615  break;
616  case LLVMContext::MD_fpmath:
617  MD = MDNode::getMostGenericFPMath(MD, IMD);
618  break;
619  case LLVMContext::MD_noalias:
620  case LLVMContext::MD_nontemporal:
621  case LLVMContext::MD_invariant_load:
622  MD = MDNode::intersect(MD, IMD);
623  break;
624  case LLVMContext::MD_access_group:
625  MD = intersectAccessGroups(Inst, IJ);
626  break;
627  default:
628  llvm_unreachable("unhandled metadata");
629  }
630  }
631 
632  Inst->setMetadata(Kind, MD);
633  }
634 
635  return Inst;
636 }
637 
638 Constant *
640  const InterleaveGroup<Instruction> &Group) {
641  // All 1's means mask is not needed.
642  if (Group.getNumMembers() == Group.getFactor())
643  return nullptr;
644 
645  // TODO: support reversed access.
646  assert(!Group.isReverse() && "Reversed group not supported.");
647 
649  for (unsigned i = 0; i < VF; i++)
650  for (unsigned j = 0; j < Group.getFactor(); ++j) {
651  unsigned HasMember = Group.getMember(j) ? 1 : 0;
652  Mask.push_back(Builder.getInt1(HasMember));
653  }
654 
655  return ConstantVector::get(Mask);
656 }
657 
659  unsigned ReplicationFactor, unsigned VF) {
661  for (unsigned i = 0; i < VF; i++)
662  for (unsigned j = 0; j < ReplicationFactor; j++)
663  MaskVec.push_back(Builder.getInt32(i));
664 
665  return ConstantVector::get(MaskVec);
666 }
667 
669  unsigned NumVecs) {
671  for (unsigned i = 0; i < VF; i++)
672  for (unsigned j = 0; j < NumVecs; j++)
673  Mask.push_back(Builder.getInt32(j * VF + i));
674 
675  return ConstantVector::get(Mask);
676 }
677 
678 Constant *llvm::createStrideMask(IRBuilder<> &Builder, unsigned Start,
679  unsigned Stride, unsigned VF) {
681  for (unsigned i = 0; i < VF; i++)
682  Mask.push_back(Builder.getInt32(Start + i * Stride));
683 
684  return ConstantVector::get(Mask);
685 }
686 
688  unsigned NumInts, unsigned NumUndefs) {
690  for (unsigned i = 0; i < NumInts; i++)
691  Mask.push_back(Builder.getInt32(Start + i));
692 
694  for (unsigned i = 0; i < NumUndefs; i++)
695  Mask.push_back(Undef);
696 
697  return ConstantVector::get(Mask);
698 }
699 
700 /// A helper function for concatenating vectors. This function concatenates two
701 /// vectors having the same element type. If the second vector has fewer
702 /// elements than the first, it is padded with undefs.
704  Value *V2) {
705  VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType());
706  VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType());
707  assert(VecTy1 && VecTy2 &&
708  VecTy1->getScalarType() == VecTy2->getScalarType() &&
709  "Expect two vectors with the same element type");
710 
711  unsigned NumElts1 = VecTy1->getNumElements();
712  unsigned NumElts2 = VecTy2->getNumElements();
713  assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements");
714 
715  if (NumElts1 > NumElts2) {
716  // Extend with UNDEFs.
717  Constant *ExtMask =
718  createSequentialMask(Builder, 0, NumElts2, NumElts1 - NumElts2);
719  V2 = Builder.CreateShuffleVector(V2, UndefValue::get(VecTy2), ExtMask);
720  }
721 
722  Constant *Mask = createSequentialMask(Builder, 0, NumElts1 + NumElts2, 0);
723  return Builder.CreateShuffleVector(V1, V2, Mask);
724 }
725 
727  unsigned NumVecs = Vecs.size();
728  assert(NumVecs > 1 && "Should be at least two vectors");
729 
730  SmallVector<Value *, 8> ResList;
731  ResList.append(Vecs.begin(), Vecs.end());
732  do {
733  SmallVector<Value *, 8> TmpList;
734  for (unsigned i = 0; i < NumVecs - 1; i += 2) {
735  Value *V0 = ResList[i], *V1 = ResList[i + 1];
736  assert((V0->getType() == V1->getType() || i == NumVecs - 2) &&
737  "Only the last vector may have a different type");
738 
739  TmpList.push_back(concatenateTwoVectors(Builder, V0, V1));
740  }
741 
742  // Push the last vector if the total number of vectors is odd.
743  if (NumVecs % 2 != 0)
744  TmpList.push_back(ResList[NumVecs - 1]);
745 
746  ResList = TmpList;
747  NumVecs = ResList.size();
748  } while (NumVecs > 1);
749 
750  return ResList[0];
751 }
752 
754  auto *ConstMask = dyn_cast<Constant>(Mask);
755  if (!ConstMask)
756  return false;
757  if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
758  return true;
759  for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
760  ++I) {
761  if (auto *MaskElt = ConstMask->getAggregateElement(I))
762  if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
763  continue;
764  return false;
765  }
766  return true;
767 }
768 
769 
771  auto *ConstMask = dyn_cast<Constant>(Mask);
772  if (!ConstMask)
773  return false;
774  if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
775  return true;
776  for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
777  ++I) {
778  if (auto *MaskElt = ConstMask->getAggregateElement(I))
779  if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
780  continue;
781  return false;
782  }
783  return true;
784 }
785 
786 /// TODO: This is a lot like known bits, but for
787 /// vectors. Is there something we can common this with?
789 
790  const unsigned VWidth = cast<VectorType>(Mask->getType())->getNumElements();
791  APInt DemandedElts = APInt::getAllOnesValue(VWidth);
792  if (auto *CV = dyn_cast<ConstantVector>(Mask))
793  for (unsigned i = 0; i < VWidth; i++)
794  if (CV->getAggregateElement(i)->isNullValue())
795  DemandedElts.clearBit(i);
796  return DemandedElts;
797 }
798 
799 bool InterleavedAccessInfo::isStrided(int Stride) {
800  unsigned Factor = std::abs(Stride);
801  return Factor >= 2 && Factor <= MaxInterleaveGroupFactor;
802 }
803 
804 void InterleavedAccessInfo::collectConstStrideAccesses(
806  const ValueToValueMap &Strides) {
807  auto &DL = TheLoop->getHeader()->getModule()->getDataLayout();
808 
809  // Since it's desired that the load/store instructions be maintained in
810  // "program order" for the interleaved access analysis, we have to visit the
811  // blocks in the loop in reverse postorder (i.e., in a topological order).
812  // Such an ordering will ensure that any load/store that may be executed
813  // before a second load/store will precede the second load/store in
814  // AccessStrideInfo.
815  LoopBlocksDFS DFS(TheLoop);
816  DFS.perform(LI);
817  for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO()))
818  for (auto &I : *BB) {
819  auto *LI = dyn_cast<LoadInst>(&I);
820  auto *SI = dyn_cast<StoreInst>(&I);
821  if (!LI && !SI)
822  continue;
823 
825  // We don't check wrapping here because we don't know yet if Ptr will be
826  // part of a full group or a group with gaps. Checking wrapping for all
827  // pointers (even those that end up in groups with no gaps) will be overly
828  // conservative. For full groups, wrapping should be ok since if we would
829  // wrap around the address space we would do a memory access at nullptr
830  // even without the transformation. The wrapping checks are therefore
831  // deferred until after we've formed the interleaved groups.
832  int64_t Stride = getPtrStride(PSE, Ptr, TheLoop, Strides,
833  /*Assume=*/true, /*ShouldCheckWrap=*/false);
834 
835  const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
836  PointerType *PtrTy = cast<PointerType>(Ptr->getType());
837  uint64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
838 
839  // An alignment of 0 means target ABI alignment.
841  if (!Alignment)
842  Alignment = Align(DL.getABITypeAlignment(PtrTy->getElementType()));
843 
844  AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size, *Alignment);
845  }
846 }
847 
848 // Analyze interleaved accesses and collect them into interleaved load and
849 // store groups.
850 //
851 // When generating code for an interleaved load group, we effectively hoist all
852 // loads in the group to the location of the first load in program order. When
853 // generating code for an interleaved store group, we sink all stores to the
854 // location of the last store. This code motion can change the order of load
855 // and store instructions and may break dependences.
856 //
857 // The code generation strategy mentioned above ensures that we won't violate
858 // any write-after-read (WAR) dependences.
859 //
860 // E.g., for the WAR dependence: a = A[i]; // (1)
861 // A[i] = b; // (2)
862 //
863 // The store group of (2) is always inserted at or below (2), and the load
864 // group of (1) is always inserted at or above (1). Thus, the instructions will
865 // never be reordered. All other dependences are checked to ensure the
866 // correctness of the instruction reordering.
867 //
868 // The algorithm visits all memory accesses in the loop in bottom-up program
869 // order. Program order is established by traversing the blocks in the loop in
870 // reverse postorder when collecting the accesses.
871 //
872 // We visit the memory accesses in bottom-up order because it can simplify the
873 // construction of store groups in the presence of write-after-write (WAW)
874 // dependences.
875 //
876 // E.g., for the WAW dependence: A[i] = a; // (1)
877 // A[i] = b; // (2)
878 // A[i + 1] = c; // (3)
879 //
880 // We will first create a store group with (3) and (2). (1) can't be added to
881 // this group because it and (2) are dependent. However, (1) can be grouped
882 // with other accesses that may precede it in program order. Note that a
883 // bottom-up order does not imply that WAW dependences should not be checked.
885  bool EnablePredicatedInterleavedMemAccesses) {
886  LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n");
887  const ValueToValueMap &Strides = LAI->getSymbolicStrides();
888 
889  // Holds all accesses with a constant stride.
891  collectConstStrideAccesses(AccessStrideInfo, Strides);
892 
893  if (AccessStrideInfo.empty())
894  return;
895 
896  // Collect the dependences in the loop.
897  collectDependences();
898 
899  // Holds all interleaved store groups temporarily.
901  // Holds all interleaved load groups temporarily.
903 
904  // Search in bottom-up program order for pairs of accesses (A and B) that can
905  // form interleaved load or store groups. In the algorithm below, access A
906  // precedes access B in program order. We initialize a group for B in the
907  // outer loop of the algorithm, and then in the inner loop, we attempt to
908  // insert each A into B's group if:
909  //
910  // 1. A and B have the same stride,
911  // 2. A and B have the same memory object size, and
912  // 3. A belongs in B's group according to its distance from B.
913  //
914  // Special care is taken to ensure group formation will not break any
915  // dependences.
916  for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend();
917  BI != E; ++BI) {
918  Instruction *B = BI->first;
919  StrideDescriptor DesB = BI->second;
920 
921  // Initialize a group for B if it has an allowable stride. Even if we don't
922  // create a group for B, we continue with the bottom-up algorithm to ensure
923  // we don't break any of B's dependences.
924  InterleaveGroup<Instruction> *Group = nullptr;
925  if (isStrided(DesB.Stride) &&
926  (!isPredicated(B->getParent()) || EnablePredicatedInterleavedMemAccesses)) {
927  Group = getInterleaveGroup(B);
928  if (!Group) {
929  LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B
930  << '\n');
931  Group = createInterleaveGroup(B, DesB.Stride, DesB.Alignment);
932  }
933  if (B->mayWriteToMemory())
934  StoreGroups.insert(Group);
935  else
936  LoadGroups.insert(Group);
937  }
938 
939  for (auto AI = std::next(BI); AI != E; ++AI) {
940  Instruction *A = AI->first;
941  StrideDescriptor DesA = AI->second;
942 
943  // Our code motion strategy implies that we can't have dependences
944  // between accesses in an interleaved group and other accesses located
945  // between the first and last member of the group. Note that this also
946  // means that a group can't have more than one member at a given offset.
947  // The accesses in a group can have dependences with other accesses, but
948  // we must ensure we don't extend the boundaries of the group such that
949  // we encompass those dependent accesses.
950  //
951  // For example, assume we have the sequence of accesses shown below in a
952  // stride-2 loop:
953  //
954  // (1, 2) is a group | A[i] = a; // (1)
955  // | A[i-1] = b; // (2) |
956  // A[i-3] = c; // (3)
957  // A[i] = d; // (4) | (2, 4) is not a group
958  //
959  // Because accesses (2) and (3) are dependent, we can group (2) with (1)
960  // but not with (4). If we did, the dependent access (3) would be within
961  // the boundaries of the (2, 4) group.
962  if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI)) {
963  // If a dependence exists and A is already in a group, we know that A
964  // must be a store since A precedes B and WAR dependences are allowed.
965  // Thus, A would be sunk below B. We release A's group to prevent this
966  // illegal code motion. A will then be free to form another group with
967  // instructions that precede it.
968  if (isInterleaved(A)) {
969  InterleaveGroup<Instruction> *StoreGroup = getInterleaveGroup(A);
970 
971  LLVM_DEBUG(dbgs() << "LV: Invalidated store group due to "
972  "dependence between " << *A << " and "<< *B << '\n');
973 
974  StoreGroups.remove(StoreGroup);
975  releaseGroup(StoreGroup);
976  }
977 
978  // If a dependence exists and A is not already in a group (or it was
979  // and we just released it), B might be hoisted above A (if B is a
980  // load) or another store might be sunk below A (if B is a store). In
981  // either case, we can't add additional instructions to B's group. B
982  // will only form a group with instructions that it precedes.
983  break;
984  }
985 
986  // At this point, we've checked for illegal code motion. If either A or B
987  // isn't strided, there's nothing left to do.
988  if (!isStrided(DesA.Stride) || !isStrided(DesB.Stride))
989  continue;
990 
991  // Ignore A if it's already in a group or isn't the same kind of memory
992  // operation as B.
993  // Note that mayReadFromMemory() isn't mutually exclusive to
994  // mayWriteToMemory in the case of atomic loads. We shouldn't see those
995  // here, canVectorizeMemory() should have returned false - except for the
996  // case we asked for optimization remarks.
997  if (isInterleaved(A) ||
998  (A->mayReadFromMemory() != B->mayReadFromMemory()) ||
999  (A->mayWriteToMemory() != B->mayWriteToMemory()))
1000  continue;
1001 
1002  // Check rules 1 and 2. Ignore A if its stride or size is different from
1003  // that of B.
1004  if (DesA.Stride != DesB.Stride || DesA.Size != DesB.Size)
1005  continue;
1006 
1007  // Ignore A if the memory object of A and B don't belong to the same
1008  // address space
1010  continue;
1011 
1012  // Calculate the distance from A to B.
1013  const SCEVConstant *DistToB = dyn_cast<SCEVConstant>(
1014  PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev));
1015  if (!DistToB)
1016  continue;
1017  int64_t DistanceToB = DistToB->getAPInt().getSExtValue();
1018 
1019  // Check rule 3. Ignore A if its distance to B is not a multiple of the
1020  // size.
1021  if (DistanceToB % static_cast<int64_t>(DesB.Size))
1022  continue;
1023 
1024  // All members of a predicated interleave-group must have the same predicate,
1025  // and currently must reside in the same BB.
1026  BasicBlock *BlockA = A->getParent();
1027  BasicBlock *BlockB = B->getParent();
1028  if ((isPredicated(BlockA) || isPredicated(BlockB)) &&
1029  (!EnablePredicatedInterleavedMemAccesses || BlockA != BlockB))
1030  continue;
1031 
1032  // The index of A is the index of B plus A's distance to B in multiples
1033  // of the size.
1034  int IndexA =
1035  Group->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size);
1036 
1037  // Try to insert A into B's group.
1038  if (Group->insertMember(A, IndexA, DesA.Alignment)) {
1039  LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n'
1040  << " into the interleave group with" << *B
1041  << '\n');
1042  InterleaveGroupMap[A] = Group;
1043 
1044  // Set the first load in program order as the insert position.
1045  if (A->mayReadFromMemory())
1046  Group->setInsertPos(A);
1047  }
1048  } // Iteration over A accesses.
1049  } // Iteration over B accesses.
1050 
1051  // Remove interleaved store groups with gaps.
1052  for (auto *Group : StoreGroups)
1053  if (Group->getNumMembers() != Group->getFactor()) {
1054  LLVM_DEBUG(
1055  dbgs() << "LV: Invalidate candidate interleaved store group due "
1056  "to gaps.\n");
1057  releaseGroup(Group);
1058  }
1059  // Remove interleaved groups with gaps (currently only loads) whose memory
1060  // accesses may wrap around. We have to revisit the getPtrStride analysis,
1061  // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does
1062  // not check wrapping (see documentation there).
1063  // FORNOW we use Assume=false;
1064  // TODO: Change to Assume=true but making sure we don't exceed the threshold
1065  // of runtime SCEV assumptions checks (thereby potentially failing to
1066  // vectorize altogether).
1067  // Additional optional optimizations:
1068  // TODO: If we are peeling the loop and we know that the first pointer doesn't
1069  // wrap then we can deduce that all pointers in the group don't wrap.
1070  // This means that we can forcefully peel the loop in order to only have to
1071  // check the first pointer for no-wrap. When we'll change to use Assume=true
1072  // we'll only need at most one runtime check per interleaved group.
1073  for (auto *Group : LoadGroups) {
1074  // Case 1: A full group. Can Skip the checks; For full groups, if the wide
1075  // load would wrap around the address space we would do a memory access at
1076  // nullptr even without the transformation.
1077  if (Group->getNumMembers() == Group->getFactor())
1078  continue;
1079 
1080  // Case 2: If first and last members of the group don't wrap this implies
1081  // that all the pointers in the group don't wrap.
1082  // So we check only group member 0 (which is always guaranteed to exist),
1083  // and group member Factor - 1; If the latter doesn't exist we rely on
1084  // peeling (if it is a non-reversed accsess -- see Case 3).
1085  Value *FirstMemberPtr = getLoadStorePointerOperand(Group->getMember(0));
1086  if (!getPtrStride(PSE, FirstMemberPtr, TheLoop, Strides, /*Assume=*/false,
1087  /*ShouldCheckWrap=*/true)) {
1088  LLVM_DEBUG(
1089  dbgs() << "LV: Invalidate candidate interleaved group due to "
1090  "first group member potentially pointer-wrapping.\n");
1091  releaseGroup(Group);
1092  continue;
1093  }
1094  Instruction *LastMember = Group->getMember(Group->getFactor() - 1);
1095  if (LastMember) {
1096  Value *LastMemberPtr = getLoadStorePointerOperand(LastMember);
1097  if (!getPtrStride(PSE, LastMemberPtr, TheLoop, Strides, /*Assume=*/false,
1098  /*ShouldCheckWrap=*/true)) {
1099  LLVM_DEBUG(
1100  dbgs() << "LV: Invalidate candidate interleaved group due to "
1101  "last group member potentially pointer-wrapping.\n");
1102  releaseGroup(Group);
1103  }
1104  } else {
1105  // Case 3: A non-reversed interleaved load group with gaps: We need
1106  // to execute at least one scalar epilogue iteration. This will ensure
1107  // we don't speculatively access memory out-of-bounds. We only need
1108  // to look for a member at index factor - 1, since every group must have
1109  // a member at index zero.
1110  if (Group->isReverse()) {
1111  LLVM_DEBUG(
1112  dbgs() << "LV: Invalidate candidate interleaved group due to "
1113  "a reverse access with gaps.\n");
1114  releaseGroup(Group);
1115  continue;
1116  }
1117  LLVM_DEBUG(
1118  dbgs() << "LV: Interleaved group requires epilogue iteration.\n");
1119  RequiresScalarEpilogue = true;
1120  }
1121  }
1122 }
1123 
1125  // If no group had triggered the requirement to create an epilogue loop,
1126  // there is nothing to do.
1127  if (!requiresScalarEpilogue())
1128  return;
1129 
1130  // Avoid releasing a Group twice.
1132  for (auto &I : InterleaveGroupMap) {
1133  InterleaveGroup<Instruction> *Group = I.second;
1134  if (Group->requiresScalarEpilogue())
1135  DelSet.insert(Group);
1136  }
1137  for (auto *Ptr : DelSet) {
1138  LLVM_DEBUG(
1139  dbgs()
1140  << "LV: Invalidate candidate interleaved group due to gaps that "
1141  "require a scalar epilogue (not allowed under optsize) and cannot "
1142  "be masked (not enabled). \n");
1143  releaseGroup(Ptr);
1144  }
1145 
1146  RequiresScalarEpilogue = false;
1147 }
1148 
1149 template <typename InstT>
1150 void InterleaveGroup<InstT>::addMetadata(InstT *NewInst) const {
1151  llvm_unreachable("addMetadata can only be used for Instruction");
1152 }
1153 
1154 namespace llvm {
1155 template <>
1158  std::transform(Members.begin(), Members.end(), std::back_inserter(VL),
1159  [](std::pair<int, Instruction *> p) { return p.second; });
1160  propagateMetadata(NewInst, VL);
1161 }
1162 }
1163 
1165  const CallInst &CI, SmallVectorImpl<std::string> &VariantMappings) {
1166  const StringRef S =
1168  .getValueAsString();
1169  if (S.empty())
1170  return;
1171 
1172  SmallVector<StringRef, 8> ListAttr;
1173  S.split(ListAttr, ",");
1174 
1175  for (auto &S : SetVector<StringRef>(ListAttr.begin(), ListAttr.end())) {
1176 #ifndef NDEBUG
1178  assert(Info.hasValue() && "Invalid name for a VFABI variant.");
1180  "Vector function is missing.");
1181 #endif
1182  VariantMappings.push_back(S);
1183  }
1184 }
1185 
1187  for (unsigned Pos = 0, NumParams = Parameters.size(); Pos < NumParams;
1188  ++Pos) {
1189  assert(Parameters[Pos].ParamPos == Pos && "Broken parameter list.");
1190 
1191  switch (Parameters[Pos].ParamKind) {
1192  default: // Nothing to check.
1193  break;
1198  // Compile time linear steps must be non-zero.
1199  if (Parameters[Pos].LinearStepOrPos == 0)
1200  return false;
1201  break;
1206  // The runtime linear step must be referring to some other
1207  // parameters in the signature.
1208  if (Parameters[Pos].LinearStepOrPos >= int(NumParams))
1209  return false;
1210  // The linear step parameter must be marked as uniform.
1211  if (Parameters[Parameters[Pos].LinearStepOrPos].ParamKind !=
1213  return false;
1214  // The linear step parameter can't point at itself.
1215  if (Parameters[Pos].LinearStepOrPos == int(Pos))
1216  return false;
1217  break;
1219  // The global predicate must be the unique. Can be placed anywhere in the
1220  // signature.
1221  for (unsigned NextPos = Pos + 1; NextPos < NumParams; ++NextPos)
1222  if (Parameters[NextPos].ParamKind == VFParamKind::GlobalPredicate)
1223  return false;
1224  break;
1225  }
1226  }
1227  return true;
1228 }
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:267
Value * getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
Get the stride of a pointer access in a loop.
uint64_t CallInst * C
bool hasValidParameterList() const
Sanity check on the Parameters in the VFShape.
Value * stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
If the argument is a GEP, then returns the operand identified by getGEPInductionOperand.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:719
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:71
void setInsertPos(InstTy *Inst)
Definition: VectorUtils.h:503
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1620
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:566
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.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:426
void getVectorVariantNames(const CallInst &CI, SmallVectorImpl< std::string > &VariantMappings)
Populates a set of strings representing the Vector Function ABI variants associated to the CallInst C...
Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register...
const T & front() const
Return the first element of the SetVector.
Definition: SetVector.h:122
This class represents lattice values for constants.
Definition: AllocatorList.h:23
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:77
iterator begin() const
Definition: ArrayRef.h:146
const Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
reverse_iterator rend()
Definition: MapVector.h:76
Instruction * propagateMetadata(Instruction *I, ArrayRef< Value *> VL)
Specifically, let Kinds = [MD_tbaa, MD_alias_scope, MD_noalias, MD_fpmath, MD_nontemporal, MD_access_group].
The main scalar evolution driver.
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:924
This class represents a function call, abstracting a target machine&#39;s calling convention.
int64_t getPtrStride(PredicatedScalarEvolution &PSE, Value *Ptr, const Loop *Lp, const ValueToValueMap &StridesMap=ValueToValueMap(), bool Assume=false, bool ShouldCheckWrap=true)
If the pointer has a constant stride return it in units of its element size.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:90
static uint64_t round(uint64_t Acc, uint64_t Input)
Definition: xxhash.cpp:57
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
This instruction constructs a fixed permutation of two input vectors.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:744
const unsigned MaxDepth
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Metadata node.
Definition: Metadata.h:872
ThreeOps_match< V1_t, V2_t, Mask_t, Instruction::ShuffleVector > m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m)
Matches ShuffleVectorInst.
An instruction for reading from memory.
Definition: Instructions.h:169
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...
Hexagon Common GEP
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:230
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
Definition: APFloat.h:1282
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:414
This is the base class for unary cast operator classes.
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1566
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:442
Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
Intrinsic::ID getIntrinsicForCallSite(ImmutableCallSite ICS, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
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.
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:395
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:439
bool isReverse() const
Definition: VectorUtils.h:432
static Value * concatenateTwoVectors(IRBuilder<> &Builder, Value *V1, Value *V2)
A helper function for concatenating vectors.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Definition: APFloat.h:1269
bool empty() const
Definition: MapVector.h:79
RPOIterator endRPO() const
Definition: LoopIterator.h:140
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
Definition: DerivedTypes.h:398
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:157
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
This node represents multiplication of some number of SCEVs.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:784
const APInt & getAPInt() const
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1632
Constant * createSequentialMask(IRBuilder<> &Builder, unsigned Start, unsigned NumInts, unsigned NumUndefs)
Create a sequential shuffle mask.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
member_iterator member_begin(iterator I) const
This node represents a polynomial recurrence on the trip count of the specified loop.
op_range operands() const
Definition: Metadata.h:1075
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
LLVMContext & getContext() const
Definition: Metadata.h:932
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1510
static constexpr char const * MappingsAttrName
Definition: VectorUtils.h:162
bool isSplatValue(const Value *V, unsigned Depth=0)
Return true if the input value is known to be a vector with all identical elements (potentially inclu...
The group of interleaved loads/stores sharing the same stride and close to each other.
Definition: VectorUtils.h:173
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
bool maskIsAllOneOrUndef(Value *Mask)
Given a mask vector of the form <Y x="" i1>="">, Return true if all of the elements of this predicate...
static MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:911
An instruction for storing to memory.
Definition: Instructions.h:331
unsigned getLoadStoreAddressSpace(Value *I)
A helper function that returns the address space of the pointer operand of load or store instruction...
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:1108
void invalidateGroupsRequiringScalarEpilogue()
Invalidate groups that require a scalar epilogue (due to gaps).
Value * getOperand(unsigned i) const
Definition: User.h:169
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
MDNode * intersectAccessGroups(const Instruction *Inst1, const Instruction *Inst2)
Compute the access-group list of access groups that Inst1 and Inst2 are both in.
InstTy * getMember(uint32_t Index) const
Get the member with the given index Index.
Definition: VectorUtils.h:482
Class to represent pointers.
Definition: DerivedTypes.h:579
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:388
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:307
bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
Constant * getSplatValue(bool AllowUndefs=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1450
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:892
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
This instruction inserts a single (scalar) element into a VectorType value.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Value * concatenateVectors(IRBuilder<> &Builder, ArrayRef< Value *> Vecs)
Concatenate a list of vectors.
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:330
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
Constant * createReplicatedMask(IRBuilder<> &Builder, unsigned ReplicationFactor, unsigned VF)
Create a mask with replicated elements.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:486
static unsigned getScalarSizeInBits(Type *Ty)
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:370
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
member_iterator member_end() const
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:470
uint32_t getIndex(const InstTy *Instr) const
Get the index for the given member.
Definition: VectorUtils.h:493
Optional< VFInfo > tryDemangleForVFABI(StringRef MangledName)
Function to contruct a VFInfo out of a mangled names in the following format:
constexpr double e
Definition: MathExtras.h:57
const SCEV * replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE, const ValueToValueMap &PtrToStride, Value *Ptr, Value *OrigPtr=nullptr)
Return the SCEV corresponding to a pointer with the symbolic stride replaced with constant one...
Constant * createBitMaskForGaps(IRBuilder<> &Builder, unsigned VF, const InterleaveGroup< Instruction > &Group)
Create a mask that filters the members of an interleave group where there are gaps.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:74
static double log2(double V)
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
bool maskIsAllZeroOrUndef(Value *Mask)
Given a mask vector of the form <Y x="" i1>="">, Return true if all of the elements of this predicate...
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1514
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:672
MDNode * uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2)
Compute the union of two access-group lists.
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the most significant bit to the least stopping at the first 1...
Definition: MathExtras.h:225
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1224
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1258
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:62
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
unsigned getNumOperands() const
Definition: User.h:191
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
const ElemTy & getOrInsertLeaderValue(const ElemTy &V)
getOrInsertLeaderValue - Return the leader for the specified value that is in the set...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:117
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Provides information about what library functions are available for the current target.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:718
iterator end() const
Definition: ArrayRef.h:147
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:355
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2547
reverse_iterator rbegin()
Definition: MapVector.h:74
bool hasVectorInstrinsicScalarOpd(Intrinsic::ID ID, unsigned ScalarOpdIdx)
Identifies if the vector form of the intrinsic has a scalar operand.
Definition: VectorUtils.cpp:93
APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:566
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:97
Class to represent vector types.
Definition: DerivedTypes.h:432
static void addToAccessGroupList(ListT &List, MDNode *AccGroups)
Add all access groups in AccGroups to List.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
Class for arbitrary precision integers.
Definition: APInt.h:69
iterator_range< user_iterator > users()
Definition: Value.h:420
static void DFS(BasicBlock *Root, SetVector< BasicBlock *> &Set)
bool isPredicated(MCInstrInfo const &MCII, MCInst const &MCI)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
Value * getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty)
If a value has only one user that is a CastInst, return it.
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const
Get the attribute of a given kind at a position.
Definition: InstrTypes.h:1482
Constant * createStrideMask(IRBuilder<> &Builder, unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
unsigned getGEPInductionOperand(const GetElementPtrInst *Gep)
Find the operand of the GEP that should be checked for consecutive stores.
void analyzeInterleaving(bool EnableMaskedInterleavedGroup)
Analyze the interleaved accesses and collect them in interleave groups.
APInt possiblyDemandedEltsInMask(Value *Mask)
Given a mask vector of the form <Y x="" i1>="">, return an APInt (of bitwidth Y) for each lane which ...
bool hasValue() const
Definition: Optional.h:259
This class represents an analyzed expression in the program.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:220
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
const NodeList & List
Definition: RDFGraph.cpp:201
#define I(x, y, z)
Definition: MD5.cpp:58
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1233
bool mayReadFromMemory() const
Return true if this instruction may read memory.
Type * getResultElementType() const
Definition: Instructions.h:994
uint32_t getFactor() const
Definition: VectorUtils.h:433
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere...
Definition: STLExtras.h:1264
Constant * createInterleaveMask(IRBuilder<> &Builder, unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
A vector that has set insertion semantics.
Definition: SetVector.h:40
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
Definition: LoopIterator.h:136
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
This pass exposes codegen information to IR-level passes.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1083
#define LLVM_DEBUG(X)
Definition: Debug.h:122
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:931
void addMetadata(InstTy *NewInst) const
Add metadata (e.g.
MaybeAlign getLoadStoreAlignment(Value *I)
A helper function that returns the alignment of load or store instruction.
StringRef VectorName
Definition: VectorUtils.h:121
Root of the metadata hierarchy.
Definition: Metadata.h:58
uint32_t getNumMembers() const
Definition: VectorUtils.h:435
static Constant * get(ArrayRef< Constant *> V)
Definition: Constants.cpp:1161
Type * getElementType() const
Definition: DerivedTypes.h:598
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1247
bool isValidAsAccessGroup(MDNode *AccGroup)
Return whether an MDNode might represent an access group.
Definition: LoopInfo.cpp:1007
const BasicBlock * getParent() const
Definition: Instruction.h:66
This class represents a constant integer value.
bool requiresScalarEpilogue() const
Returns true if this Group requires a scalar iteration to handle gaps.
Definition: VectorUtils.h:514
gep_type_iterator gep_type_begin(const User *GEP)
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:44
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
Definition: Instruction.h:542