Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/VectorUtils.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvm/ADT/EquivalenceClasses.h"
15
#include "llvm/Analysis/DemandedBits.h"
16
#include "llvm/Analysis/LoopInfo.h"
17
#include "llvm/Analysis/LoopIterator.h"
18
#include "llvm/Analysis/ScalarEvolution.h"
19
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
20
#include "llvm/Analysis/TargetTransformInfo.h"
21
#include "llvm/Analysis/ValueTracking.h"
22
#include "llvm/IR/Constants.h"
23
#include "llvm/IR/GetElementPtrTypeIterator.h"
24
#include "llvm/IR/IRBuilder.h"
25
#include "llvm/IR/PatternMatch.h"
26
#include "llvm/IR/Value.h"
27
28
#define DEBUG_TYPE "vectorutils"
29
30
using namespace llvm;
31
using namespace llvm::PatternMatch;
32
33
/// Maximum factor for an interleaved memory access.
34
static cl::opt<unsigned> MaxInterleaveGroupFactor(
35
    "max-interleave-group-factor", cl::Hidden,
36
    cl::desc("Maximum factor for an interleaved access group (default = 8)"),
37
    cl::init(8));
38
39
/// Return true if all of the intrinsic's arguments and return type are scalars
40
/// for the scalar form of the intrinsic, and vectors for the vector form of the
41
/// intrinsic (except operands that are marked as always being scalar by
42
/// hasVectorInstrinsicScalarOpd).
43
38.9k
bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
44
38.9k
  switch (ID) {
45
38.9k
  case Intrinsic::bswap: // Begin integer bit-manipulation.
46
29.3k
  case Intrinsic::bitreverse:
47
29.3k
  case Intrinsic::ctpop:
48
29.3k
  case Intrinsic::ctlz:
49
29.3k
  case Intrinsic::cttz:
50
29.3k
  case Intrinsic::fshl:
51
29.3k
  case Intrinsic::fshr:
52
29.3k
  case Intrinsic::sadd_sat:
53
29.3k
  case Intrinsic::ssub_sat:
54
29.3k
  case Intrinsic::uadd_sat:
55
29.3k
  case Intrinsic::usub_sat:
56
29.3k
  case Intrinsic::smul_fix:
57
29.3k
  case Intrinsic::smul_fix_sat:
58
29.3k
  case Intrinsic::umul_fix:
59
29.3k
  case Intrinsic::sqrt: // Begin floating-point.
60
29.3k
  case Intrinsic::sin:
61
29.3k
  case Intrinsic::cos:
62
29.3k
  case Intrinsic::exp:
63
29.3k
  case Intrinsic::exp2:
64
29.3k
  case Intrinsic::log:
65
29.3k
  case Intrinsic::log10:
66
29.3k
  case Intrinsic::log2:
67
29.3k
  case Intrinsic::fabs:
68
29.3k
  case Intrinsic::minnum:
69
29.3k
  case Intrinsic::maxnum:
70
29.3k
  case Intrinsic::minimum:
71
29.3k
  case Intrinsic::maximum:
72
29.3k
  case Intrinsic::copysign:
73
29.3k
  case Intrinsic::floor:
74
29.3k
  case Intrinsic::ceil:
75
29.3k
  case Intrinsic::trunc:
76
29.3k
  case Intrinsic::rint:
77
29.3k
  case Intrinsic::nearbyint:
78
29.3k
  case Intrinsic::round:
79
29.3k
  case Intrinsic::pow:
80
29.3k
  case Intrinsic::fma:
81
29.3k
  case Intrinsic::fmuladd:
82
29.3k
  case Intrinsic::powi:
83
29.3k
  case Intrinsic::canonicalize:
84
29.3k
    return true;
85
29.3k
  default:
86
9.52k
    return false;
87
38.9k
  }
88
38.9k
}
89
90
/// Identifies if the vector form of the intrinsic has a scalar operand.
91
bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
92
37.9k
                                        unsigned ScalarOpdIdx) {
93
37.9k
  switch (ID) {
94
37.9k
  case Intrinsic::ctlz:
95
4.87k
  case Intrinsic::cttz:
96
4.87k
  case Intrinsic::powi:
97
4.87k
    return (ScalarOpdIdx == 1);
98
8.43k
  case Intrinsic::smul_fix:
99
8.43k
  case Intrinsic::smul_fix_sat:
100
8.43k
  case Intrinsic::umul_fix:
101
8.43k
    return (ScalarOpdIdx == 2);
102
24.6k
  default:
103
24.6k
    return false;
104
37.9k
  }
105
37.9k
}
106
107
/// Returns intrinsic ID for call.
108
/// For the input call instruction it finds mapping intrinsic and returns
109
/// its ID, in case it does not found it return not_intrinsic.
110
Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI,
111
71.9k
                                                const TargetLibraryInfo *TLI) {
112
71.9k
  Intrinsic::ID ID = getIntrinsicForCallSite(CI, TLI);
113
71.9k
  if (ID == Intrinsic::not_intrinsic)
114
39.8k
    return Intrinsic::not_intrinsic;
115
32.1k
116
32.1k
  if (isTriviallyVectorizable(ID) || 
ID == Intrinsic::lifetime_start5.84k
||
117
32.1k
      
ID == Intrinsic::lifetime_end4.66k
||
ID == Intrinsic::assume4.61k
||
118
32.1k
      
ID == Intrinsic::sideeffect4.59k
)
119
27.5k
    return ID;
120
4.58k
  return Intrinsic::not_intrinsic;
121
4.58k
}
122
123
/// Find the operand of the GEP that should be checked for consecutive
124
/// stores. This ignores trailing indices that have no effect on the final
125
/// pointer.
126
149k
unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) {
127
149k
  const DataLayout &DL = Gep->getModule()->getDataLayout();
128
149k
  unsigned LastOperand = Gep->getNumOperands() - 1;
129
149k
  unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());
130
149k
131
149k
  // Walk backwards and try to peel off zeros.
132
154k
  while (LastOperand > 1 && 
match(Gep->getOperand(LastOperand), m_Zero())74.0k
) {
133
11.6k
    // Find the type we're currently indexing into.
134
11.6k
    gep_type_iterator GEPTI = gep_type_begin(Gep);
135
11.6k
    std::advance(GEPTI, LastOperand - 2);
136
11.6k
137
11.6k
    // If it's a type with the same allocation size as the result of the GEP we
138
11.6k
    // can peel off the zero index.
139
11.6k
    if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize)
140
7.34k
      break;
141
4.32k
    --LastOperand;
142
4.32k
  }
143
149k
144
149k
  return LastOperand;
145
149k
}
146
147
/// If the argument is a GEP, then returns the operand identified by
148
/// getGEPInductionOperand. However, if there is some other non-loop-invariant
149
/// operand, it returns that instead.
150
225k
Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
151
225k
  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
152
225k
  if (!GEP)
153
76.0k
    return Ptr;
154
149k
155
149k
  unsigned InductionOperand = getGEPInductionOperand(GEP);
156
149k
157
149k
  // Check that all of the gep indices are uniform except for our induction
158
149k
  // operand.
159
440k
  for (unsigned i = 0, e = GEP->getNumOperands(); i != e; 
++i290k
)
160
343k
    if (i != InductionOperand &&
161
343k
        
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp)246k
)
162
53.0k
      return Ptr;
163
149k
  
return GEP->getOperand(InductionOperand)96.7k
;
164
149k
}
165
166
/// If a value has only one user that is a CastInst, return it.
167
1.62k
Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) {
168
1.62k
  Value *UniqueCast = nullptr;
169
5.06k
  for (User *U : Ptr->users()) {
170
5.06k
    CastInst *CI = dyn_cast<CastInst>(U);
171
5.06k
    if (CI && 
CI->getType() == Ty2.04k
) {
172
2.04k
      if (!UniqueCast)
173
1.62k
        UniqueCast = CI;
174
418
      else
175
418
        return nullptr;
176
2.04k
    }
177
5.06k
  }
178
1.62k
  
return UniqueCast1.20k
;
179
1.62k
}
180
181
/// Get the stride of a pointer access in a loop. Looks for symbolic
182
/// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
183
225k
Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
184
225k
  auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
185
225k
  if (!PtrTy || PtrTy->isAggregateType())
186
0
    return nullptr;
187
225k
188
225k
  // Try to remove a gep instruction to make the pointer (actually index at this
189
225k
  // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the
190
225k
  // pointer, otherwise, we are analyzing the index.
191
225k
  Value *OrigPtr = Ptr;
192
225k
193
225k
  // The size of the pointer access.
194
225k
  int64_t PtrAccessSize = 1;
195
225k
196
225k
  Ptr = stripGetElementPtr(Ptr, SE, Lp);
197
225k
  const SCEV *V = SE->getSCEV(Ptr);
198
225k
199
225k
  if (Ptr != OrigPtr)
200
96.7k
    // Strip off casts.
201
103k
    
while (const SCEVCastExpr *96.7k
C = dyn_cast<SCEVCastExpr>(V))
202
6.87k
      V = C->getOperand();
203
225k
204
225k
  const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
205
225k
  if (!S)
206
51.1k
    return nullptr;
207
174k
208
174k
  V = S->getStepRecurrence(*SE);
209
174k
  if (!V)
210
0
    return nullptr;
211
174k
212
174k
  // Strip off the size of access multiplication if we are still analyzing the
213
174k
  // pointer.
214
174k
  if (OrigPtr == Ptr) {
215
91.6k
    if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
216
10.8k
      if (M->getOperand(0)->getSCEVType() != scConstant)
217
0
        return nullptr;
218
10.8k
219
10.8k
      const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
220
10.8k
221
10.8k
      // Huge step value - give up.
222
10.8k
      if (APStepVal.getBitWidth() > 64)
223
0
        return nullptr;
224
10.8k
225
10.8k
      int64_t StepVal = APStepVal.getSExtValue();
226
10.8k
      if (PtrAccessSize != StepVal)
227
10.8k
        return nullptr;
228
0
      V = M->getOperand(1);
229
0
    }
230
91.6k
  }
231
174k
232
174k
  // Strip off casts.
233
174k
  Type *StripedOffRecurrenceCast = nullptr;
234
163k
  if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
235
1.79k
    StripedOffRecurrenceCast = C->getType();
236
1.79k
    V = C->getOperand();
237
1.79k
  }
238
163k
239
163k
  // Look for the loop invariant symbolic value.
240
163k
  const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
241
163k
  if (!U)
242
162k
    return nullptr;
243
1.67k
244
1.67k
  Value *Stride = U->getValue();
245
1.67k
  if (!Lp->isLoopInvariant(Stride))
246
0
    return nullptr;
247
1.67k
248
1.67k
  // If we have stripped off the recurrence cast we have to make sure that we
249
1.67k
  // return the value that is used in this loop so that we can replace it later.
250
1.67k
  if (StripedOffRecurrenceCast)
251
1.62k
    Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
252
1.67k
253
1.67k
  return Stride;
254
1.67k
}
255
256
/// Given a vector and an element number, see if the scalar value is
257
/// already around as a register, for example if it were inserted then extracted
258
/// from the vector.
259
179k
Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
260
179k
  assert(V->getType()->isVectorTy() && "Not looking at a vector?");
261
179k
  VectorType *VTy = cast<VectorType>(V->getType());
262
179k
  unsigned Width = VTy->getNumElements();
263
179k
  if (EltNo >= Width)  // Out of range access.
264
0
    return UndefValue::get(VTy->getElementType());
265
179k
266
179k
  if (Constant *C = dyn_cast<Constant>(V))
267
24
    return C->getAggregateElement(EltNo);
268
179k
269
179k
  if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
270
19.7k
    // If this is an insert to a variable element, we don't know what it is.
271
19.7k
    if (!isa<ConstantInt>(III->getOperand(2)))
272
3.13k
      return nullptr;
273
16.5k
    unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
274
16.5k
275
16.5k
    // If this is an insert to the element we are looking for, return the
276
16.5k
    // inserted value.
277
16.5k
    if (EltNo == IIElt)
278
326
      return III->getOperand(1);
279
16.2k
280
16.2k
    // Otherwise, the insertelement doesn't modify the value, recurse on its
281
16.2k
    // vector input.
282
16.2k
    return findScalarElement(III->getOperand(0), EltNo);
283
16.2k
  }
284
160k
285
160k
  if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
286
907
    unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
287
907
    int InEl = SVI->getMaskValue(EltNo);
288
907
    if (InEl < 0)
289
0
      return UndefValue::get(VTy->getElementType());
290
907
    if (InEl < (int)LHSWidth)
291
596
      return findScalarElement(SVI->getOperand(0), InEl);
292
311
    return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
293
311
  }
294
159k
295
159k
  // Extract a value from a vector add operation with a constant zero.
296
159k
  // TODO: Use getBinOpIdentity() to generalize this.
297
159k
  Value *Val; Constant *C;
298
159k
  if (match(V, m_Add(m_Value(Val), m_Constant(C))))
299
675
    if (Constant *Elt = C->getAggregateElement(EltNo))
300
674
      if (Elt->isNullValue())
301
6
        return findScalarElement(Val, EltNo);
302
159k
303
159k
  // Otherwise, we don't know.
304
159k
  return nullptr;
305
159k
}
306
307
/// Get splat value if the input is a splat vector or return nullptr.
308
/// This function is not fully general. It checks only 2 cases:
309
/// the input value is (1) a splat constant vector or (2) a sequence
310
/// of instructions that broadcasts a scalar at element 0.
311
3.51M
const llvm::Value *llvm::getSplatValue(const Value *V) {
312
3.51M
  if (isa<VectorType>(V->getType()))
313
22.7k
    if (auto *C = dyn_cast<Constant>(V))
314
12.3k
      return C->getSplatValue();
315
3.50M
316
3.50M
  // shuf (inselt ?, Splat, 0), ?, <0, undef, 0, ...>
317
3.50M
  Value *Splat;
318
3.50M
  if (match(V, m_ShuffleVector(m_InsertElement(m_Value(), m_Value(Splat),
319
3.50M
                                               m_ZeroInt()),
320
3.50M
                               m_Value(), m_ZeroInt())))
321
645
    return Splat;
322
3.50M
323
3.50M
  return nullptr;
324
3.50M
}
325
326
// This setting is based on its counterpart in value tracking, but it could be
327
// adjusted if needed.
328
const unsigned MaxDepth = 6;
329
330
80
bool llvm::isSplatValue(const Value *V, unsigned Depth) {
331
80
  assert(Depth <= MaxDepth && "Limit Search Depth");
332
80
333
80
  if (isa<VectorType>(V->getType())) {
334
78
    if (isa<UndefValue>(V))
335
1
      return true;
336
77
    // FIXME: Constant splat analysis does not allow undef elements.
337
77
    if (auto *C = dyn_cast<Constant>(V))
338
4
      return C->getSplatValue() != nullptr;
339
75
  }
340
75
341
75
  // FIXME: Constant splat analysis does not allow undef elements.
342
75
  Constant *Mask;
343
75
  if (match(V, m_ShuffleVector(m_Value(), m_Value(), m_Constant(Mask))))
344
59
    return Mask->getSplatValue() != nullptr;
345
16
346
16
  // The remaining tests are all recursive, so bail out if we hit the limit.
347
16
  if (Depth++ == MaxDepth)
348
0
    return false;
349
16
350
16
  // If both operands of a binop are splats, the result is a splat.
351
16
  Value *X, *Y, *Z;
352
16
  if (match(V, m_BinOp(m_Value(X), m_Value(Y))))
353
5
    return isSplatValue(X, Depth) && 
isSplatValue(Y, Depth)4
;
354
11
355
11
  // If all operands of a select are splats, the result is a splat.
356
11
  if (match(V, m_Select(m_Value(X), m_Value(Y), m_Value(Z))))
357
6
    return isSplatValue(X, Depth) && 
isSplatValue(Y, Depth)5
&&
358
6
           
isSplatValue(Z, Depth)4
;
359
5
360
5
  // TODO: Add support for unary ops (fneg), casts, intrinsics (overflow ops).
361
5
362
5
  return false;
363
5
}
364
365
MapVector<Instruction *, uint64_t>
366
llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
367
19.9k
                               const TargetTransformInfo *TTI) {
368
19.9k
369
19.9k
  // DemandedBits will give us every value's live-out bits. But we want
370
19.9k
  // to ensure no extra casts would need to be inserted, so every DAG
371
19.9k
  // of connected values must have the same minimum bitwidth.
372
19.9k
  EquivalenceClasses<Value *> ECs;
373
19.9k
  SmallVector<Value *, 16> Worklist;
374
19.9k
  SmallPtrSet<Value *, 4> Roots;
375
19.9k
  SmallPtrSet<Value *, 16> Visited;
376
19.9k
  DenseMap<Value *, uint64_t> DBits;
377
19.9k
  SmallPtrSet<Instruction *, 4> InstructionSet;
378
19.9k
  MapVector<Instruction *, uint64_t> MinBWs;
379
19.9k
380
19.9k
  // Determine the roots. We work bottom-up, from truncs or icmps.
381
19.9k
  bool SeenExtFromIllegalType = false;
382
19.9k
  for (auto *BB : Blocks)
383
251k
    
for (auto &I : *BB)21.9k
{
384
251k
      InstructionSet.insert(&I);
385
251k
386
251k
      if (TTI && (isa<ZExtInst>(&I) || 
isa<SExtInst>(&I)249k
) &&
387
251k
          
!TTI->isTypeLegal(I.getOperand(0)->getType())3.40k
)
388
1.96k
        SeenExtFromIllegalType = true;
389
251k
390
251k
      // Only deal with non-vector integers up to 64-bits wide.
391
251k
      if ((isa<TruncInst>(&I) || 
isa<ICmpInst>(&I)240k
) &&
392
251k
          
!I.getType()->isVectorTy()33.3k
&&
393
251k
          
I.getOperand(0)->getType()->getScalarSizeInBits() <= 6433.3k
) {
394
33.3k
        // Don't make work for ourselves. If we know the loaded type is legal,
395
33.3k
        // don't add it to the worklist.
396
33.3k
        if (TTI && isa<TruncInst>(&I) && 
TTI->isTypeLegal(I.getType())10.8k
)
397
9.80k
          continue;
398
23.5k
399
23.5k
        Worklist.push_back(&I);
400
23.5k
        Roots.insert(&I);
401
23.5k
      }
402
251k
    }
403
19.9k
  // Early exit.
404
19.9k
  if (Worklist.empty() || 
(19.8k
TTI19.8k
&&
!SeenExtFromIllegalType19.8k
))
405
18.9k
    return MinBWs;
406
906
407
906
  // Now proceed breadth-first, unioning values together.
408
9.57k
  
while (906
!Worklist.empty()) {
409
8.67k
    Value *Val = Worklist.pop_back_val();
410
8.67k
    Value *Leader = ECs.getOrInsertLeaderValue(Val);
411
8.67k
412
8.67k
    if (Visited.count(Val))
413
1.27k
      continue;
414
7.39k
    Visited.insert(Val);
415
7.39k
416
7.39k
    // Non-instructions terminate a chain successfully.
417
7.39k
    if (!isa<Instruction>(Val))
418
1.36k
      continue;
419
6.02k
    Instruction *I = cast<Instruction>(Val);
420
6.02k
421
6.02k
    // If we encounter a type that is larger than 64 bits, we can't represent
422
6.02k
    // it so bail out.
423
6.02k
    if (DB.getDemandedBits(I).getBitWidth() > 64)
424
0
      return MapVector<Instruction *, uint64_t>();
425
6.02k
426
6.02k
    uint64_t V = DB.getDemandedBits(I).getZExtValue();
427
6.02k
    DBits[Leader] |= V;
428
6.02k
    DBits[I] = V;
429
6.02k
430
6.02k
    // Casts, loads and instructions outside of our range terminate a chain
431
6.02k
    // successfully.
432
6.02k
    if (isa<SExtInst>(I) || 
isa<ZExtInst>(I)5.70k
||
isa<LoadInst>(I)5.14k
||
433
6.02k
        
!InstructionSet.count(I)4.72k
)
434
1.56k
      continue;
435
4.46k
436
4.46k
    // Unsafe casts terminate a chain unsuccessfully. We can't do anything
437
4.46k
    // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
438
4.46k
    // transform anything that relies on them.
439
4.46k
    if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
440
4.46k
        !I->getType()->isIntegerTy()) {
441
58
      DBits[Leader] |= ~0ULL;
442
58
      continue;
443
58
    }
444
4.41k
445
4.41k
    // We don't modify the types of PHIs. Reductions will already have been
446
4.41k
    // truncated if possible, and inductions' sizes will have been chosen by
447
4.41k
    // indvars.
448
4.41k
    if (isa<PHINode>(I))
449
237
      continue;
450
4.17k
451
4.17k
    if (DBits[Leader] == ~0ULL)
452
719
      // All bits demanded, no point continuing.
453
719
      continue;
454
3.45k
455
6.74k
    
for (Value *O : cast<User>(I)->operands())3.45k
{
456
6.74k
      ECs.unionSets(Leader, O);
457
6.74k
      Worklist.push_back(O);
458
6.74k
    }
459
3.45k
  }
460
906
461
906
  // Now we've discovered all values, walk them to see if there are
462
906
  // any users we didn't see. If there are, we can't optimize that
463
906
  // chain.
464
906
  for (auto &I : DBits)
465
6.02k
    for (auto *U : I.first->users())
466
10.0k
      if (U->getType()->isIntegerTy() && 
DBits.count(U) == 08.43k
)
467
3.17k
        DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
468
906
469
8.30k
  for (auto I = ECs.begin(), E = ECs.end(); I != E; 
++I7.39k
) {
470
7.39k
    uint64_t LeaderDemandedBits = 0;
471
14.7k
    for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; 
++MI7.39k
)
472
7.39k
      LeaderDemandedBits |= DBits[*MI];
473
7.39k
474
7.39k
    uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) -
475
7.39k
                     llvm::countLeadingZeros(LeaderDemandedBits);
476
7.39k
    // Round up to a power of 2
477
7.39k
    if (!isPowerOf2_64((uint64_t)MinBW))
478
6.05k
      MinBW = NextPowerOf2(MinBW);
479
7.39k
480
7.39k
    // We don't modify the types of PHIs. Reductions will already have been
481
7.39k
    // truncated if possible, and inductions' sizes will have been chosen by
482
7.39k
    // indvars.
483
7.39k
    // If we are required to shrink a PHI, abandon this entire equivalence class.
484
7.39k
    bool Abort = false;
485
14.7k
    for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; 
++MI7.39k
)
486
7.39k
      if (isa<PHINode>(*MI) && 
MinBW < (*MI)->getType()->getScalarSizeInBits()274
) {
487
0
        Abort = true;
488
0
        break;
489
0
      }
490
7.39k
    if (Abort)
491
0
      continue;
492
7.39k
493
14.7k
    
for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); 7.39k
MI != ME;
++MI7.39k
) {
494
7.39k
      if (!isa<Instruction>(*MI))
495
1.36k
        continue;
496
6.02k
      Type *Ty = (*MI)->getType();
497
6.02k
      if (Roots.count(*MI))
498
1.93k
        Ty = cast<Instruction>(*MI)->getOperand(0)->getType();
499
6.02k
      if (MinBW < Ty->getScalarSizeInBits())
500
198
        MinBWs[cast<Instruction>(*MI)] = MinBW;
501
6.02k
    }
502
7.39k
  }
503
906
504
906
  return MinBWs;
505
906
}
506
507
/// Add all access groups in @p AccGroups to @p List.
508
template <typename ListT>
509
3
static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
510
3
  // Interpret an access group as a list containing itself.
511
3
  if (AccGroups->getNumOperands() == 0) {
512
2
    assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
513
2
    List.insert(AccGroups);
514
2
    return;
515
2
  }
516
1
517
2
  
for (auto &AccGroupListOp : AccGroups->operands())1
{
518
2
    auto *Item = cast<MDNode>(AccGroupListOp.get());
519
2
    assert(isValidAsAccessGroup(Item) && "List item must be an access group");
520
2
    List.insert(Item);
521
2
  }
522
1
}
VectorUtils.cpp:void addToAccessGroupList<llvm::SmallSetVector<llvm::Metadata*, 4u> >(llvm::SmallSetVector<llvm::Metadata*, 4u>&, llvm::MDNode*)
Line
Count
Source
509
2
static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
510
2
  // Interpret an access group as a list containing itself.
511
2
  if (AccGroups->getNumOperands() == 0) {
512
2
    assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
513
2
    List.insert(AccGroups);
514
2
    return;
515
2
  }
516
0
517
0
  for (auto &AccGroupListOp : AccGroups->operands()) {
518
0
    auto *Item = cast<MDNode>(AccGroupListOp.get());
519
0
    assert(isValidAsAccessGroup(Item) && "List item must be an access group");
520
0
    List.insert(Item);
521
0
  }
522
0
}
VectorUtils.cpp:void addToAccessGroupList<llvm::SmallPtrSet<llvm::Metadata*, 4u> >(llvm::SmallPtrSet<llvm::Metadata*, 4u>&, llvm::MDNode*)
Line
Count
Source
509
1
static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
510
1
  // Interpret an access group as a list containing itself.
511
1
  if (AccGroups->getNumOperands() == 0) {
512
0
    assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
513
0
    List.insert(AccGroups);
514
0
    return;
515
0
  }
516
1
517
2
  
for (auto &AccGroupListOp : AccGroups->operands())1
{
518
2
    auto *Item = cast<MDNode>(AccGroupListOp.get());
519
2
    assert(isValidAsAccessGroup(Item) && "List item must be an access group");
520
2
    List.insert(Item);
521
2
  }
522
1
}
523
524
13
MDNode *llvm::uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2) {
525
13
  if (!AccGroups1)
526
9
    return AccGroups2;
527
4
  if (!AccGroups2)
528
0
    return AccGroups1;
529
4
  if (AccGroups1 == AccGroups2)
530
3
    return AccGroups1;
531
1
532
1
  SmallSetVector<Metadata *, 4> Union;
533
1
  addToAccessGroupList(Union, AccGroups1);
534
1
  addToAccessGroupList(Union, AccGroups2);
535
1
536
1
  if (Union.size() == 0)
537
0
    return nullptr;
538
1
  if (Union.size() == 1)
539
0
    return cast<MDNode>(Union.front());
540
1
541
1
  LLVMContext &Ctx = AccGroups1->getContext();
542
1
  return MDNode::get(Ctx, Union.getArrayRef());
543
1
}
544
545
MDNode *llvm::intersectAccessGroups(const Instruction *Inst1,
546
15
                                    const Instruction *Inst2) {
547
15
  bool MayAccessMem1 = Inst1->mayReadOrWriteMemory();
548
15
  bool MayAccessMem2 = Inst2->mayReadOrWriteMemory();
549
15
550
15
  if (!MayAccessMem1 && 
!MayAccessMem20
)
551
0
    return nullptr;
552
15
  if (!MayAccessMem1)
553
0
    return Inst2->getMetadata(LLVMContext::MD_access_group);
554
15
  if (!MayAccessMem2)
555
0
    return Inst1->getMetadata(LLVMContext::MD_access_group);
556
15
557
15
  MDNode *MD1 = Inst1->getMetadata(LLVMContext::MD_access_group);
558
15
  MDNode *MD2 = Inst2->getMetadata(LLVMContext::MD_access_group);
559
15
  if (!MD1 || !MD2)
560
0
    return nullptr;
561
15
  if (MD1 == MD2)
562
14
    return MD1;
563
1
564
1
  // Use set for scalable 'contains' check.
565
1
  SmallPtrSet<Metadata *, 4> AccGroupSet2;
566
1
  addToAccessGroupList(AccGroupSet2, MD2);
567
1
568
1
  SmallVector<Metadata *, 4> Intersection;
569
1
  if (MD1->getNumOperands() == 0) {
570
0
    assert(isValidAsAccessGroup(MD1) && "Node must be an access group");
571
0
    if (AccGroupSet2.count(MD1))
572
0
      Intersection.push_back(MD1);
573
1
  } else {
574
2
    for (const MDOperand &Node : MD1->operands()) {
575
2
      auto *Item = cast<MDNode>(Node.get());
576
2
      assert(isValidAsAccessGroup(Item) && "List item must be an access group");
577
2
      if (AccGroupSet2.count(Item))
578
1
        Intersection.push_back(Item);
579
2
    }
580
1
  }
581
1
582
1
  if (Intersection.size() == 0)
583
0
    return nullptr;
584
1
  if (Intersection.size() == 1)
585
1
    return cast<MDNode>(Intersection.front());
586
0
587
0
  LLVMContext &Ctx = Inst1->getContext();
588
0
  return MDNode::get(Ctx, Intersection);
589
0
}
590
591
/// \returns \p I after propagating metadata from \p VL.
592
209k
Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
593
209k
  Instruction *I0 = cast<Instruction>(VL[0]);
594
209k
  SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
595
209k
  I0->getAllMetadataOtherThanDebugLoc(Metadata);
596
209k
597
209k
  for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
598
209k
                    LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
599
209k
                    LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
600
1.46M
                    LLVMContext::MD_access_group}) {
601
1.46M
    MDNode *MD = I0->getMetadata(Kind);
602
1.46M
603
1.52M
    for (int J = 1, E = VL.size(); MD && 
J != E141k
;
++J62.3k
) {
604
62.3k
      const Instruction *IJ = cast<Instruction>(VL[J]);
605
62.3k
      MDNode *IMD = IJ->getMetadata(Kind);
606
62.3k
      switch (Kind) {
607
62.3k
      case LLVMContext::MD_tbaa:
608
46.6k
        MD = MDNode::getMostGenericTBAA(MD, IMD);
609
46.6k
        break;
610
62.3k
      case LLVMContext::MD_alias_scope:
611
826
        MD = MDNode::getMostGenericAliasScope(MD, IMD);
612
826
        break;
613
62.3k
      case LLVMContext::MD_fpmath:
614
2
        MD = MDNode::getMostGenericFPMath(MD, IMD);
615
2
        break;
616
62.3k
      case LLVMContext::MD_noalias:
617
14.9k
      case LLVMContext::MD_nontemporal:
618
14.9k
      case LLVMContext::MD_invariant_load:
619
14.9k
        MD = MDNode::intersect(MD, IMD);
620
14.9k
        break;
621
14.9k
      case LLVMContext::MD_access_group:
622
0
        MD = intersectAccessGroups(Inst, IJ);
623
0
        break;
624
14.9k
      default:
625
0
        llvm_unreachable("unhandled metadata");
626
62.3k
      }
627
62.3k
    }
628
1.46M
629
1.46M
    Inst->setMetadata(Kind, MD);
630
1.46M
  }
631
209k
632
209k
  return Inst;
633
209k
}
634
635
Constant *
636
llvm::createBitMaskForGaps(IRBuilder<> &Builder, unsigned VF,
637
5
                           const InterleaveGroup<Instruction> &Group) {
638
5
  // All 1's means mask is not needed.
639
5
  if (Group.getNumMembers() == Group.getFactor())
640
0
    return nullptr;
641
5
642
5
  // TODO: support reversed access.
643
5
  assert(!Group.isReverse() && "Reversed group not supported.");
644
5
645
5
  SmallVector<Constant *, 16> Mask;
646
45
  for (unsigned i = 0; i < VF; 
i++40
)
647
128
    
for (unsigned j = 0; 40
j < Group.getFactor();
++j88
) {
648
88
      unsigned HasMember = Group.getMember(j) ? 
140
:
048
;
649
88
      Mask.push_back(Builder.getInt1(HasMember));
650
88
    }
651
5
652
5
  return ConstantVector::get(Mask);
653
5
}
654
655
Constant *llvm::createReplicatedMask(IRBuilder<> &Builder, 
656
11
                                     unsigned ReplicationFactor, unsigned VF) {
657
11
  SmallVector<Constant *, 16> MaskVec;
658
99
  for (unsigned i = 0; i < VF; 
i++88
)
659
272
    
for (unsigned j = 0; 88
j < ReplicationFactor;
j++184
)
660
184
      MaskVec.push_back(Builder.getInt32(i));
661
11
662
11
  return ConstantVector::get(MaskVec);
663
11
}
664
665
Constant *llvm::createInterleaveMask(IRBuilder<> &Builder, unsigned VF,
666
577
                                     unsigned NumVecs) {
667
577
  SmallVector<Constant *, 16> Mask;
668
2.59k
  for (unsigned i = 0; i < VF; 
i++2.01k
)
669
6.73k
    
for (unsigned j = 0; 2.01k
j < NumVecs;
j++4.71k
)
670
4.71k
      Mask.push_back(Builder.getInt32(j * VF + i));
671
577
672
577
  return ConstantVector::get(Mask);
673
577
}
674
675
Constant *llvm::createStrideMask(IRBuilder<> &Builder, unsigned Start,
676
712
                                 unsigned Stride, unsigned VF) {
677
712
  SmallVector<Constant *, 16> Mask;
678
3.30k
  for (unsigned i = 0; i < VF; 
i++2.59k
)
679
2.59k
    Mask.push_back(Builder.getInt32(Start + i * Stride));
680
712
681
712
  return ConstantVector::get(Mask);
682
712
}
683
684
Constant *llvm::createSequentialMask(IRBuilder<> &Builder, unsigned Start,
685
2.13k
                                     unsigned NumInts, unsigned NumUndefs) {
686
2.13k
  SmallVector<Constant *, 16> Mask;
687
24.8k
  for (unsigned i = 0; i < NumInts; 
i++22.6k
)
688
22.6k
    Mask.push_back(Builder.getInt32(Start + i));
689
2.13k
690
2.13k
  Constant *Undef = UndefValue::get(Builder.getInt32Ty());
691
3.30k
  for (unsigned i = 0; i < NumUndefs; 
i++1.16k
)
692
1.16k
    Mask.push_back(Undef);
693
2.13k
694
2.13k
  return ConstantVector::get(Mask);
695
2.13k
}
696
697
/// A helper function for concatenating vectors. This function concatenates two
698
/// vectors having the same element type. If the second vector has fewer
699
/// elements than the first, it is padded with undefs.
700
static Value *concatenateTwoVectors(IRBuilder<> &Builder, Value *V1,
701
894
                                    Value *V2) {
702
894
  VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType());
703
894
  VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType());
704
894
  assert(VecTy1 && VecTy2 &&
705
894
         VecTy1->getScalarType() == VecTy2->getScalarType() &&
706
894
         "Expect two vectors with the same element type");
707
894
708
894
  unsigned NumElts1 = VecTy1->getNumElements();
709
894
  unsigned NumElts2 = VecTy2->getNumElements();
710
894
  assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements");
711
894
712
894
  if (NumElts1 > NumElts2) {
713
133
    // Extend with UNDEFs.
714
133
    Constant *ExtMask =
715
133
        createSequentialMask(Builder, 0, NumElts2, NumElts1 - NumElts2);
716
133
    V2 = Builder.CreateShuffleVector(V2, UndefValue::get(VecTy2), ExtMask);
717
133
  }
718
894
719
894
  Constant *Mask = createSequentialMask(Builder, 0, NumElts1 + NumElts2, 0);
720
894
  return Builder.CreateShuffleVector(V1, V2, Mask);
721
894
}
722
723
649
Value *llvm::concatenateVectors(IRBuilder<> &Builder, ArrayRef<Value *> Vecs) {
724
649
  unsigned NumVecs = Vecs.size();
725
649
  assert(NumVecs > 1 && "Should be at least two vectors");
726
649
727
649
  SmallVector<Value *, 8> ResList;
728
649
  ResList.append(Vecs.begin(), Vecs.end());
729
836
  do {
730
836
    SmallVector<Value *, 8> TmpList;
731
1.73k
    for (unsigned i = 0; i < NumVecs - 1; 
i += 2894
) {
732
894
      Value *V0 = ResList[i], *V1 = ResList[i + 1];
733
894
      assert((V0->getType() == V1->getType() || i == NumVecs - 2) &&
734
894
             "Only the last vector may have a different type");
735
894
736
894
      TmpList.push_back(concatenateTwoVectors(Builder, V0, V1));
737
894
    }
738
836
739
836
    // Push the last vector if the total number of vectors is odd.
740
836
    if (NumVecs % 2 != 0)
741
133
      TmpList.push_back(ResList[NumVecs - 1]);
742
836
743
836
    ResList = TmpList;
744
836
    NumVecs = ResList.size();
745
836
  } while (NumVecs > 1);
746
649
747
649
  return ResList[0];
748
649
}
749
750
990
bool llvm::maskIsAllZeroOrUndef(Value *Mask) {
751
990
  auto *ConstMask = dyn_cast<Constant>(Mask);
752
990
  if (!ConstMask)
753
584
    return false;
754
406
  if (ConstMask->isNullValue() || 
isa<UndefValue>(ConstMask)403
)
755
5
    return true;
756
447
  
for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); 401
I != E;
757
447
       
++I46
) {
758
447
    if (auto *MaskElt = ConstMask->getAggregateElement(I))
759
447
      if (MaskElt->isNullValue() || 
isa<UndefValue>(MaskElt)401
)
760
46
        continue;
761
401
    return false;
762
401
  }
763
401
  
return true0
;
764
401
}
765
766
767
91
bool llvm::maskIsAllOneOrUndef(Value *Mask) {
768
91
  auto *ConstMask = dyn_cast<Constant>(Mask);
769
91
  if (!ConstMask)
770
66
    return false;
771
25
  if (ConstMask->isAllOnesValue() || 
isa<UndefValue>(ConstMask)23
)
772
2
    return true;
773
39
  
for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); 23
I != E;
774
38
       
++I16
) {
775
38
    if (auto *MaskElt = ConstMask->getAggregateElement(I))
776
38
      if (MaskElt->isAllOnesValue() || 
isa<UndefValue>(MaskElt)23
)
777
16
        continue;
778
22
    return false;
779
22
  }
780
23
  
return true1
;
781
23
}
782
783
/// TODO: This is a lot like known bits, but for
784
/// vectors.  Is there something we can common this with?
785
17
APInt llvm::possiblyDemandedEltsInMask(Value *Mask) {
786
17
787
17
  const unsigned VWidth = cast<VectorType>(Mask->getType())->getNumElements();
788
17
  APInt DemandedElts = APInt::getAllOnesValue(VWidth);
789
17
  if (auto *CV = dyn_cast<ConstantVector>(Mask))
790
71
    
for (unsigned i = 0; 17
i < VWidth;
i++54
)
791
54
      if (CV->getAggregateElement(i)->isNullValue())
792
30
        DemandedElts.clearBit(i);
793
17
  return DemandedElts;
794
17
}
795
796
561k
bool InterleavedAccessInfo::isStrided(int Stride) {
797
561k
  unsigned Factor = std::abs(Stride);
798
561k
  return Factor >= 2 && 
Factor <= MaxInterleaveGroupFactor399k
;
799
561k
}
800
801
void InterleavedAccessInfo::collectConstStrideAccesses(
802
    MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo,
803
19.4k
    const ValueToValueMap &Strides) {
804
19.4k
  auto &DL = TheLoop->getHeader()->getModule()->getDataLayout();
805
19.4k
806
19.4k
  // Since it's desired that the load/store instructions be maintained in
807
19.4k
  // "program order" for the interleaved access analysis, we have to visit the
808
19.4k
  // blocks in the loop in reverse postorder (i.e., in a topological order).
809
19.4k
  // Such an ordering will ensure that any load/store that may be executed
810
19.4k
  // before a second load/store will precede the second load/store in
811
19.4k
  // AccessStrideInfo.
812
19.4k
  LoopBlocksDFS DFS(TheLoop);
813
19.4k
  DFS.perform(LI);
814
19.4k
  for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO()))
815
250k
    
for (auto &I : *BB)21.4k
{
816
250k
      auto *LI = dyn_cast<LoadInst>(&I);
817
250k
      auto *SI = dyn_cast<StoreInst>(&I);
818
250k
      if (!LI && 
!SI230k
)
819
209k
        continue;
820
41.1k
821
41.1k
      Value *Ptr = getLoadStorePointerOperand(&I);
822
41.1k
      // We don't check wrapping here because we don't know yet if Ptr will be
823
41.1k
      // part of a full group or a group with gaps. Checking wrapping for all
824
41.1k
      // pointers (even those that end up in groups with no gaps) will be overly
825
41.1k
      // conservative. For full groups, wrapping should be ok since if we would
826
41.1k
      // wrap around the address space we would do a memory access at nullptr
827
41.1k
      // even without the transformation. The wrapping checks are therefore
828
41.1k
      // deferred until after we've formed the interleaved groups.
829
41.1k
      int64_t Stride = getPtrStride(PSE, Ptr, TheLoop, Strides,
830
41.1k
                                    /*Assume=*/true, /*ShouldCheckWrap=*/false);
831
41.1k
832
41.1k
      const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
833
41.1k
      PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
834
41.1k
      uint64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
835
41.1k
836
41.1k
      // An alignment of 0 means target ABI alignment.
837
41.1k
      unsigned Align = getLoadStoreAlignment(&I);
838
41.1k
      if (!Align)
839
38
        Align = DL.getABITypeAlignment(PtrTy->getElementType());
840
41.1k
841
41.1k
      AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size, Align);
842
41.1k
    }
843
19.4k
}
844
845
// Analyze interleaved accesses and collect them into interleaved load and
846
// store groups.
847
//
848
// When generating code for an interleaved load group, we effectively hoist all
849
// loads in the group to the location of the first load in program order. When
850
// generating code for an interleaved store group, we sink all stores to the
851
// location of the last store. This code motion can change the order of load
852
// and store instructions and may break dependences.
853
//
854
// The code generation strategy mentioned above ensures that we won't violate
855
// any write-after-read (WAR) dependences.
856
//
857
// E.g., for the WAR dependence:  a = A[i];      // (1)
858
//                                A[i] = b;      // (2)
859
//
860
// The store group of (2) is always inserted at or below (2), and the load
861
// group of (1) is always inserted at or above (1). Thus, the instructions will
862
// never be reordered. All other dependences are checked to ensure the
863
// correctness of the instruction reordering.
864
//
865
// The algorithm visits all memory accesses in the loop in bottom-up program
866
// order. Program order is established by traversing the blocks in the loop in
867
// reverse postorder when collecting the accesses.
868
//
869
// We visit the memory accesses in bottom-up order because it can simplify the
870
// construction of store groups in the presence of write-after-write (WAW)
871
// dependences.
872
//
873
// E.g., for the WAW dependence:  A[i] = a;      // (1)
874
//                                A[i] = b;      // (2)
875
//                                A[i + 1] = c;  // (3)
876
//
877
// We will first create a store group with (3) and (2). (1) can't be added to
878
// this group because it and (2) are dependent. However, (1) can be grouped
879
// with other accesses that may precede it in program order. Note that a
880
// bottom-up order does not imply that WAW dependences should not be checked.
881
void InterleavedAccessInfo::analyzeInterleaving(
882
19.4k
                                 bool EnablePredicatedInterleavedMemAccesses) {
883
19.4k
  LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n");
884
19.4k
  const ValueToValueMap &Strides = LAI->getSymbolicStrides();
885
19.4k
886
19.4k
  // Holds all accesses with a constant stride.
887
19.4k
  MapVector<Instruction *, StrideDescriptor> AccessStrideInfo;
888
19.4k
  collectConstStrideAccesses(AccessStrideInfo, Strides);
889
19.4k
890
19.4k
  if (AccessStrideInfo.empty())
891
732
    return;
892
18.7k
893
18.7k
  // Collect the dependences in the loop.
894
18.7k
  collectDependences();
895
18.7k
896
18.7k
  // Holds all interleaved store groups temporarily.
897
18.7k
  SmallSetVector<InterleaveGroup<Instruction> *, 4> StoreGroups;
898
18.7k
  // Holds all interleaved load groups temporarily.
899
18.7k
  SmallSetVector<InterleaveGroup<Instruction> *, 4> LoadGroups;
900
18.7k
901
18.7k
  // Search in bottom-up program order for pairs of accesses (A and B) that can
902
18.7k
  // form interleaved load or store groups. In the algorithm below, access A
903
18.7k
  // precedes access B in program order. We initialize a group for B in the
904
18.7k
  // outer loop of the algorithm, and then in the inner loop, we attempt to
905
18.7k
  // insert each A into B's group if:
906
18.7k
  //
907
18.7k
  //  1. A and B have the same stride,
908
18.7k
  //  2. A and B have the same memory object size, and
909
18.7k
  //  3. A belongs in B's group according to its distance from B.
910
18.7k
  //
911
18.7k
  // Special care is taken to ensure group formation will not break any
912
18.7k
  // dependences.
913
18.7k
  for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend();
914
59.8k
       BI != E; 
++BI41.1k
) {
915
41.1k
    Instruction *B = BI->first;
916
41.1k
    StrideDescriptor DesB = BI->second;
917
41.1k
918
41.1k
    // Initialize a group for B if it has an allowable stride. Even if we don't
919
41.1k
    // create a group for B, we continue with the bottom-up algorithm to ensure
920
41.1k
    // we don't break any of B's dependences.
921
41.1k
    InterleaveGroup<Instruction> *Group = nullptr;
922
41.1k
    if (isStrided(DesB.Stride) && 
923
41.1k
        
(4.83k
!isPredicated(B->getParent())4.83k
||
EnablePredicatedInterleavedMemAccesses178
)) {
924
4.67k
      Group = getInterleaveGroup(B);
925
4.67k
      if (!Group) {
926
2.15k
        LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B
927
2.15k
                          << '\n');
928
2.15k
        Group = createInterleaveGroup(B, DesB.Stride, DesB.Align);
929
2.15k
      }
930
4.67k
      if (B->mayWriteToMemory())
931
2.17k
        StoreGroups.insert(Group);
932
2.49k
      else
933
2.49k
        LoadGroups.insert(Group);
934
4.67k
    }
935
41.1k
936
307k
    for (auto AI = std::next(BI); AI != E; 
++AI266k
) {
937
266k
      Instruction *A = AI->first;
938
266k
      StrideDescriptor DesA = AI->second;
939
266k
940
266k
      // Our code motion strategy implies that we can't have dependences
941
266k
      // between accesses in an interleaved group and other accesses located
942
266k
      // between the first and last member of the group. Note that this also
943
266k
      // means that a group can't have more than one member at a given offset.
944
266k
      // The accesses in a group can have dependences with other accesses, but
945
266k
      // we must ensure we don't extend the boundaries of the group such that
946
266k
      // we encompass those dependent accesses.
947
266k
      //
948
266k
      // For example, assume we have the sequence of accesses shown below in a
949
266k
      // stride-2 loop:
950
266k
      //
951
266k
      //  (1, 2) is a group | A[i]   = a;  // (1)
952
266k
      //                    | A[i-1] = b;  // (2) |
953
266k
      //                      A[i-3] = c;  // (3)
954
266k
      //                      A[i]   = d;  // (4) | (2, 4) is not a group
955
266k
      //
956
266k
      // Because accesses (2) and (3) are dependent, we can group (2) with (1)
957
266k
      // but not with (4). If we did, the dependent access (3) would be within
958
266k
      // the boundaries of the (2, 4) group.
959
266k
      if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI)) {
960
65
        // If a dependence exists and A is already in a group, we know that A
961
65
        // must be a store since A precedes B and WAR dependences are allowed.
962
65
        // Thus, A would be sunk below B. We release A's group to prevent this
963
65
        // illegal code motion. A will then be free to form another group with
964
65
        // instructions that precede it.
965
65
        if (isInterleaved(A)) {
966
12
          InterleaveGroup<Instruction> *StoreGroup = getInterleaveGroup(A);
967
12
          StoreGroups.remove(StoreGroup);
968
12
          releaseGroup(StoreGroup);
969
12
        }
970
65
971
65
        // If a dependence exists and A is not already in a group (or it was
972
65
        // and we just released it), B might be hoisted above A (if B is a
973
65
        // load) or another store might be sunk below A (if B is a store). In
974
65
        // either case, we can't add additional instructions to B's group. B
975
65
        // will only form a group with instructions that it precedes.
976
65
        break;
977
65
      }
978
266k
979
266k
      // At this point, we've checked for illegal code motion. If either A or B
980
266k
      // isn't strided, there's nothing left to do.
981
266k
      if (!isStrided(DesA.Stride) || 
!isStrided(DesB.Stride)16.6k
)
982
253k
        continue;
983
12.7k
984
12.7k
      // Ignore A if it's already in a group or isn't the same kind of memory
985
12.7k
      // operation as B.
986
12.7k
      // Note that mayReadFromMemory() isn't mutually exclusive to
987
12.7k
      // mayWriteToMemory in the case of atomic loads. We shouldn't see those
988
12.7k
      // here, canVectorizeMemory() should have returned false - except for the
989
12.7k
      // case we asked for optimization remarks.
990
12.7k
      if (isInterleaved(A) ||
991
12.7k
          
(A->mayReadFromMemory() != B->mayReadFromMemory())8.14k
||
992
12.7k
          
(A->mayWriteToMemory() != B->mayWriteToMemory())4.73k
)
993
7.98k
        continue;
994
4.72k
995
4.72k
      // Check rules 1 and 2. Ignore A if its stride or size is different from
996
4.72k
      // that of B.
997
4.72k
      if (DesA.Stride != DesB.Stride || 
DesA.Size != DesB.Size4.34k
)
998
419
        continue;
999
4.30k
1000
4.30k
      // Ignore A if the memory object of A and B don't belong to the same
1001
4.30k
      // address space
1002
4.30k
      if (getLoadStoreAddressSpace(A) != getLoadStoreAddressSpace(B))
1003
1
        continue;
1004
4.30k
1005
4.30k
      // Calculate the distance from A to B.
1006
4.30k
      const SCEVConstant *DistToB = dyn_cast<SCEVConstant>(
1007
4.30k
          PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev));
1008
4.30k
      if (!DistToB)
1009
1.06k
        continue;
1010
3.24k
      int64_t DistanceToB = DistToB->getAPInt().getSExtValue();
1011
3.24k
1012
3.24k
      // Check rule 3. Ignore A if its distance to B is not a multiple of the
1013
3.24k
      // size.
1014
3.24k
      if (DistanceToB % static_cast<int64_t>(DesB.Size))
1015
0
        continue;
1016
3.24k
1017
3.24k
      // All members of a predicated interleave-group must have the same predicate,
1018
3.24k
      // and currently must reside in the same BB.
1019
3.24k
      BasicBlock *BlockA = A->getParent();  
1020
3.24k
      BasicBlock *BlockB = B->getParent();  
1021
3.24k
      if ((isPredicated(BlockA) || 
isPredicated(BlockB)2.97k
) &&
1022
3.24k
          
(299
!EnablePredicatedInterleavedMemAccesses299
||
BlockA != BlockB6
))
1023
295
        continue;
1024
2.94k
1025
2.94k
      // The index of A is the index of B plus A's distance to B in multiples
1026
2.94k
      // of the size.
1027
2.94k
      int IndexA =
1028
2.94k
          Group->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size);
1029
2.94k
1030
2.94k
      // Try to insert A into B's group.
1031
2.94k
      if (Group->insertMember(A, IndexA, DesA.Align)) {
1032
2.53k
        LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n'
1033
2.53k
                          << "    into the interleave group with" << *B
1034
2.53k
                          << '\n');
1035
2.53k
        InterleaveGroupMap[A] = Group;
1036
2.53k
1037
2.53k
        // Set the first load in program order as the insert position.
1038
2.53k
        if (A->mayReadFromMemory())
1039
1.33k
          Group->setInsertPos(A);
1040
2.53k
      }
1041
2.94k
    } // Iteration over A accesses.
1042
41.1k
  }   // Iteration over B accesses.
1043
18.7k
1044
18.7k
  // Remove interleaved store groups with gaps.
1045
18.7k
  for (auto *Group : StoreGroups)
1046
989
    if (Group->getNumMembers() != Group->getFactor()) {
1047
350
      LLVM_DEBUG(
1048
350
          dbgs() << "LV: Invalidate candidate interleaved store group due "
1049
350
                    "to gaps.\n");
1050
350
      releaseGroup(Group);
1051
350
    }
1052
18.7k
  // Remove interleaved groups with gaps (currently only loads) whose memory
1053
18.7k
  // accesses may wrap around. We have to revisit the getPtrStride analysis,
1054
18.7k
  // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does
1055
18.7k
  // not check wrapping (see documentation there).
1056
18.7k
  // FORNOW we use Assume=false;
1057
18.7k
  // TODO: Change to Assume=true but making sure we don't exceed the threshold
1058
18.7k
  // of runtime SCEV assumptions checks (thereby potentially failing to
1059
18.7k
  // vectorize altogether).
1060
18.7k
  // Additional optional optimizations:
1061
18.7k
  // TODO: If we are peeling the loop and we know that the first pointer doesn't
1062
18.7k
  // wrap then we can deduce that all pointers in the group don't wrap.
1063
18.7k
  // This means that we can forcefully peel the loop in order to only have to
1064
18.7k
  // check the first pointer for no-wrap. When we'll change to use Assume=true
1065
18.7k
  // we'll only need at most one runtime check per interleaved group.
1066
18.7k
  for (auto *Group : LoadGroups) {
1067
1.15k
    // Case 1: A full group. Can Skip the checks; For full groups, if the wide
1068
1.15k
    // load would wrap around the address space we would do a memory access at
1069
1.15k
    // nullptr even without the transformation.
1070
1.15k
    if (Group->getNumMembers() == Group->getFactor())
1071
691
      continue;
1072
467
1073
467
    // Case 2: If first and last members of the group don't wrap this implies
1074
467
    // that all the pointers in the group don't wrap.
1075
467
    // So we check only group member 0 (which is always guaranteed to exist),
1076
467
    // and group member Factor - 1; If the latter doesn't exist we rely on
1077
467
    // peeling (if it is a non-reversed accsess -- see Case 3).
1078
467
    Value *FirstMemberPtr = getLoadStorePointerOperand(Group->getMember(0));
1079
467
    if (!getPtrStride(PSE, FirstMemberPtr, TheLoop, Strides, /*Assume=*/false,
1080
467
                      /*ShouldCheckWrap=*/true)) {
1081
164
      LLVM_DEBUG(
1082
164
          dbgs() << "LV: Invalidate candidate interleaved group due to "
1083
164
                    "first group member potentially pointer-wrapping.\n");
1084
164
      releaseGroup(Group);
1085
164
      continue;
1086
164
    }
1087
303
    Instruction *LastMember = Group->getMember(Group->getFactor() - 1);
1088
303
    if (LastMember) {
1089
4
      Value *LastMemberPtr = getLoadStorePointerOperand(LastMember);
1090
4
      if (!getPtrStride(PSE, LastMemberPtr, TheLoop, Strides, /*Assume=*/false,
1091
4
                        /*ShouldCheckWrap=*/true)) {
1092
0
        LLVM_DEBUG(
1093
0
            dbgs() << "LV: Invalidate candidate interleaved group due to "
1094
0
                      "last group member potentially pointer-wrapping.\n");
1095
0
        releaseGroup(Group);
1096
0
      }
1097
299
    } else {
1098
299
      // Case 3: A non-reversed interleaved load group with gaps: We need
1099
299
      // to execute at least one scalar epilogue iteration. This will ensure
1100
299
      // we don't speculatively access memory out-of-bounds. We only need
1101
299
      // to look for a member at index factor - 1, since every group must have
1102
299
      // a member at index zero.
1103
299
      if (Group->isReverse()) {
1104
17
        LLVM_DEBUG(
1105
17
            dbgs() << "LV: Invalidate candidate interleaved group due to "
1106
17
                      "a reverse access with gaps.\n");
1107
17
        releaseGroup(Group);
1108
17
        continue;
1109
17
      }
1110
282
      LLVM_DEBUG(
1111
282
          dbgs() << "LV: Interleaved group requires epilogue iteration.\n");
1112
282
      RequiresScalarEpilogue = true;
1113
282
    }
1114
303
  }
1115
18.7k
}
1116
1117
153
void InterleavedAccessInfo::invalidateGroupsRequiringScalarEpilogue() {
1118
153
  // If no group had triggered the requirement to create an epilogue loop,
1119
153
  // there is nothing to do.
1120
153
  if (!requiresScalarEpilogue())
1121
130
    return;
1122
23
1123
23
  // Avoid releasing a Group twice.
1124
23
  SmallPtrSet<InterleaveGroup<Instruction> *, 4> DelSet;
1125
80
  for (auto &I : InterleaveGroupMap) {
1126
80
    InterleaveGroup<Instruction> *Group = I.second;
1127
80
    if (Group->requiresScalarEpilogue())
1128
42
      DelSet.insert(Group);
1129
80
  }
1130
41
  for (auto *Ptr : DelSet) {
1131
41
    LLVM_DEBUG(
1132
41
        dbgs()
1133
41
        << "LV: Invalidate candidate interleaved group due to gaps that "
1134
41
           "require a scalar epilogue (not allowed under optsize) and cannot "
1135
41
           "be masked (not enabled). \n");
1136
41
    releaseGroup(Ptr);
1137
41
  }
1138
23
1139
23
  RequiresScalarEpilogue = false;
1140
23
}
1141
1142
template <typename InstT>
1143
void InterleaveGroup<InstT>::addMetadata(InstT *NewInst) const {
1144
  llvm_unreachable("addMetadata can only be used for Instruction");
1145
}
1146
1147
namespace llvm {
1148
template <>
1149
1.09k
void InterleaveGroup<Instruction>::addMetadata(Instruction *NewInst) const {
1150
1.09k
  SmallVector<Value *, 4> VL;
1151
1.09k
  std::transform(Members.begin(), Members.end(), std::back_inserter(VL),
1152
2.51k
                 [](std::pair<int, Instruction *> p) { return p.second; });
1153
1.09k
  propagateMetadata(NewInst, VL);
1154
1.09k
}
1155
}