70#define DEBUG_TYPE "loop-accesses"
74 cl::desc(
"Sets the SIMD width. Zero is autoselect."),
80 cl::desc(
"Sets the vectorization interleave count. "
81 "Zero is autoselect."),
88 cl::desc(
"When performing memory disambiguation checks at runtime do not "
89 "generate more than this number of comparisons (default = 8)."),
96 cl::desc(
"Maximum number of comparisons done when trying to merge "
97 "runtime memory checks. (default = 100)"),
106 cl::desc(
"Maximum number of dependences collected by "
107 "loop-access analysis (default = 100)"),
123 cl::desc(
"Enable symbolic stride memory access versioning"));
128 "store-to-load-forwarding-conflict-detection",
cl::Hidden,
129 cl::desc(
"Enable conflict detection in loop-access analysis"),
134 cl::desc(
"Maximum recursion depth when finding forked SCEVs (default = 5)"),
139 cl::desc(
"Speculate that non-constant strides are unit in LAA"),
145 "Hoist inner loop runtime memory checks to outer loop if possible"),
150 return ::VectorizationInterleave.getNumOccurrences() > 0;
161 if (SI == PtrToStride.
end())
165 const SCEV *StrideSCEV = SI->second;
170 assert(isa<SCEVUnknown>(StrideSCEV) &&
"shouldn't be in map");
178 <<
" by: " << *Expr <<
"\n");
188 NeedsFreeze(RtCheck.Pointers[
Index].NeedsFreeze) {
206 Type *AccessTy,
bool WritePtr,
207 unsigned DepSetId,
unsigned ASId,
216 ScStart = ScEnd = PtrExpr;
219 assert(AR &&
"Invalid addrec expression");
228 if (
const auto *CStep = dyn_cast<SCEVConstant>(Step)) {
229 if (CStep->getValue()->isNegative())
244 Type *IdxTy =
DL.getIndexType(
Ptr->getType());
248 Pointers.emplace_back(
Ptr, ScStart, ScEnd, WritePtr, DepSetId, ASId, PtrExpr,
252void RuntimePointerChecking::tryToCreateDiffCheck(
254 if (!CanUseDiffCheck)
261 CanUseDiffCheck =
false;
272 CanUseDiffCheck =
false;
282 if (AccSrc.
size() != 1 || AccSink.
size() != 1) {
283 CanUseDiffCheck =
false;
287 if (AccSink[0] < AccSrc[0])
290 auto *SrcAR = dyn_cast<SCEVAddRecExpr>(Src->Expr);
291 auto *SinkAR = dyn_cast<SCEVAddRecExpr>(
Sink->Expr);
294 CanUseDiffCheck =
false;
304 if (isa<ScalableVectorType>(SrcTy) || isa<ScalableVectorType>(DstTy)) {
305 CanUseDiffCheck =
false;
309 SinkAR->getLoop()->getHeader()->getModule()->getDataLayout();
311 std::max(
DL.getTypeAllocSize(SrcTy),
DL.getTypeAllocSize(DstTy));
316 auto *Step = dyn_cast<SCEVConstant>(SinkAR->getStepRecurrence(*SE));
317 if (!Step || Step != SrcAR->getStepRecurrence(*SE) ||
318 Step->getAPInt().abs() != AllocSize) {
319 CanUseDiffCheck =
false;
328 if (Step->getValue()->isNegative())
333 if (isa<SCEVCouldNotCompute>(SinkStartInt) ||
334 isa<SCEVCouldNotCompute>(SrcStartInt)) {
335 CanUseDiffCheck =
false;
339 const Loop *InnerLoop = SrcAR->getLoop();
345 isa<SCEVAddRecExpr>(SinkStartInt) && isa<SCEVAddRecExpr>(SrcStartInt)) {
346 auto *SrcStartAR = cast<SCEVAddRecExpr>(SrcStartInt);
347 auto *SinkStartAR = cast<SCEVAddRecExpr>(SinkStartInt);
348 const Loop *StartARLoop = SrcStartAR->getLoop();
349 if (StartARLoop == SinkStartAR->getLoop() &&
354 SrcStartAR->getStepRecurrence(*SE) !=
355 SinkStartAR->getStepRecurrence(*SE)) {
356 LLVM_DEBUG(
dbgs() <<
"LAA: Not creating diff runtime check, since these "
357 "cannot be hoisted out of the outer loop\n");
358 CanUseDiffCheck =
false;
364 <<
"SrcStart: " << *SrcStartInt <<
'\n'
365 <<
"SinkStartInt: " << *SinkStartInt <<
'\n');
366 DiffChecks.emplace_back(SrcStartInt, SinkStartInt, AllocSize,
367 Src->NeedsFreeze ||
Sink->NeedsFreeze);
379 tryToCreateDiffCheck(CGI, CGJ);
380 Checks.
push_back(std::make_pair(&CGI, &CGJ));
387void RuntimePointerChecking::generateChecks(
390 groupChecks(DepCands, UseDependencies);
396 for (
unsigned I = 0, EI = M.Members.size(); EI !=
I; ++
I)
397 for (
unsigned J = 0, EJ =
N.Members.size(); EJ != J; ++J)
412 if (
C->getValue()->isNegative())
421 RtCheck.
Pointers[
Index].PointerValue->getType()->getPointerAddressSpace(),
430 "all pointers in a checking group must be in the same address space");
456void RuntimePointerChecking::groupChecks(
502 if (!UseDependencies) {
508 unsigned TotalComparisons = 0;
513 Iter.first->second.push_back(
Index);
542 auto PointerI = PositionMap.
find(
MI->getPointer());
544 "pointer in equivalence class not found in PositionMap");
545 for (
unsigned Pointer : PointerI->second) {
562 if (Group.addPointer(Pointer, *
this)) {
585 return (PtrToPartition[PtrIdx1] != -1 &&
586 PtrToPartition[PtrIdx1] == PtrToPartition[PtrIdx2]);
610 unsigned Depth)
const {
612 for (
const auto &
Check : Checks) {
613 const auto &
First =
Check.first->Members, &Second =
Check.second->Members;
618 for (
unsigned K = 0; K <
First.size(); ++K)
622 for (
unsigned K = 0; K < Second.size(); ++K)
637 OS.
indent(
Depth + 4) <<
"(Low: " << *CG.Low <<
" High: " << *CG.High
639 for (
unsigned J = 0; J < CG.Members.size(); ++J) {
652class AccessAnalysis {
662 : TheLoop(TheLoop), BAA(*AA), AST(BAA), LI(LI), DepCands(DA), PSE(PSE),
663 LoopAliasScopes(LoopAliasScopes) {
665 BAA.enableCrossIterationMode();
671 AST.add(adjustLoc(Loc));
672 Accesses[MemAccessInfo(
Ptr,
false)].insert(AccessTy);
674 ReadOnlyPtr.insert(
Ptr);
680 AST.add(adjustLoc(Loc));
681 Accesses[MemAccessInfo(
Ptr,
true)].insert(AccessTy);
692 MemAccessInfo Access,
Type *AccessTy,
695 Loop *TheLoop,
unsigned &RunningDepId,
696 unsigned ASId,
bool ShouldCheckStride,
bool Assume);
705 Value *&UncomputablePtr,
bool ShouldCheckWrap =
false);
709 void buildDependenceSets() {
710 processMemAccesses();
718 bool isDependencyCheckNeeded() {
return !CheckDeps.empty(); }
726 MemAccessInfoList &getDependenciesToCheck() {
return CheckDeps; }
730 return UnderlyingObjects;
755 return LoopAliasScopes.contains(cast<MDNode>(Scope));
764 void processMemAccesses();
768 PtrAccessMap Accesses;
774 MemAccessInfoList CheckDeps;
800 bool IsRTCheckAnalysisNeeded =
false;
818 const SCEV *PtrScev,
Loop *L,
bool Assume) {
842 int64_t Stride =
getPtrStride(PSE, AccessTy,
Ptr, L, Strides).value_or(0);
855 while (!WorkList.
empty()) {
859 auto *PN = dyn_cast<PHINode>(
Ptr);
863 if (PN && InnermostLoop.
contains(PN->getParent()) &&
864 PN->getParent() != InnermostLoop.
getHeader()) {
865 for (
const Use &Inc : PN->incoming_values())
898 if (isa<SCEVAddRecExpr>(Scev) || L->isLoopInvariant(
Ptr) ||
899 !isa<Instruction>(
Ptr) ||
Depth == 0) {
910 auto GetBinOpExpr = [&SE](
unsigned Opcode,
const SCEV *L,
const SCEV *R) {
912 case Instruction::Add:
914 case Instruction::Sub:
922 unsigned Opcode =
I->getOpcode();
924 case Instruction::GetElementPtr: {
926 Type *SourceTy =
GEP->getSourceElementType();
929 if (
I->getNumOperands() != 2 || SourceTy->
isVectorTy()) {
939 bool NeedsFreeze =
any_of(BaseScevs, UndefPoisonCheck) ||
940 any_of(OffsetScevs, UndefPoisonCheck);
945 if (OffsetScevs.
size() == 2 && BaseScevs.
size() == 1)
947 else if (BaseScevs.
size() == 2 && OffsetScevs.
size() == 1)
950 ScevList.emplace_back(Scev, NeedsFreeze);
968 ScevList.emplace_back(SE->
getAddExpr(get<0>(BaseScevs[0]), Scaled1),
970 ScevList.emplace_back(SE->
getAddExpr(get<0>(BaseScevs[1]), Scaled2),
974 case Instruction::Select: {
981 if (ChildScevs.
size() == 2) {
982 ScevList.push_back(ChildScevs[0]);
983 ScevList.push_back(ChildScevs[1]);
988 case Instruction::PHI: {
993 if (
I->getNumOperands() == 2) {
997 if (ChildScevs.
size() == 2) {
998 ScevList.push_back(ChildScevs[0]);
999 ScevList.push_back(ChildScevs[1]);
1004 case Instruction::Add:
1005 case Instruction::Sub: {
1013 any_of(LScevs, UndefPoisonCheck) ||
any_of(RScevs, UndefPoisonCheck);
1018 if (LScevs.
size() == 2 && RScevs.
size() == 1)
1020 else if (RScevs.
size() == 2 && LScevs.
size() == 1)
1023 ScevList.emplace_back(Scev, NeedsFreeze);
1027 ScevList.emplace_back(
1028 GetBinOpExpr(Opcode, get<0>(LScevs[0]), get<0>(RScevs[0])),
1030 ScevList.emplace_back(
1031 GetBinOpExpr(Opcode, get<0>(LScevs[1]), get<0>(RScevs[1])),
1037 LLVM_DEBUG(
dbgs() <<
"ForkedPtr unhandled instruction: " << *
I <<
"\n");
1054 if (Scevs.
size() == 2 &&
1055 (isa<SCEVAddRecExpr>(get<0>(Scevs[0])) ||
1057 (isa<SCEVAddRecExpr>(get<0>(Scevs[1])) ||
1069 MemAccessInfo Access,
Type *AccessTy,
1072 Loop *TheLoop,
unsigned &RunningDepId,
1073 unsigned ASId,
bool ShouldCheckWrap,
1080 for (
auto &
P : TranslatedPtrs) {
1081 const SCEV *PtrExpr = get<0>(
P);
1087 if (ShouldCheckWrap) {
1089 if (TranslatedPtrs.size() > 1)
1092 if (!
isNoWrap(PSE, StridesMap,
Ptr, AccessTy, TheLoop)) {
1094 if (!Assume || !isa<SCEVAddRecExpr>(Expr))
1101 if (TranslatedPtrs.size() == 1)
1106 for (
auto [PtrExpr, NeedsFreeze] : TranslatedPtrs) {
1110 if (isDependencyCheckNeeded()) {
1112 unsigned &LeaderId = DepSetId[Leader];
1114 LeaderId = RunningDepId++;
1118 DepId = RunningDepId++;
1120 bool IsWrite = Access.getInt();
1121 RtCheck.
insert(TheLoop,
Ptr, PtrExpr, AccessTy, IsWrite, DepId, ASId, PSE,
1132 Value *&UncomputablePtr,
bool ShouldCheckWrap) {
1135 bool CanDoRT =
true;
1137 bool MayNeedRTCheck =
false;
1138 if (!IsRTCheckAnalysisNeeded)
return true;
1140 bool IsDepCheckNeeded = isDependencyCheckNeeded();
1145 for (
auto &AS : AST) {
1146 int NumReadPtrChecks = 0;
1147 int NumWritePtrChecks = 0;
1148 bool CanDoAliasSetRT =
true;
1150 auto ASPointers = AS.getPointers();
1154 unsigned RunningDepId = 1;
1162 for (
const Value *Ptr_ : ASPointers) {
1164 bool IsWrite = Accesses.count(MemAccessInfo(
Ptr,
true));
1166 ++NumWritePtrChecks;
1174 if (NumWritePtrChecks == 0 ||
1175 (NumWritePtrChecks == 1 && NumReadPtrChecks == 0)) {
1176 assert((ASPointers.size() <= 1 ||
1179 MemAccessInfo AccessWrite(
const_cast<Value *
>(
Ptr),
1181 return DepCands.
findValue(AccessWrite) == DepCands.
end();
1183 "Can only skip updating CanDoRT below, if all entries in AS "
1184 "are reads or there is at most 1 entry");
1188 for (
auto &Access : AccessInfos) {
1189 for (
const auto &AccessTy : Accesses[Access]) {
1190 if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
1191 DepSetId, TheLoop, RunningDepId, ASId,
1192 ShouldCheckWrap,
false)) {
1194 << *Access.getPointer() <<
'\n');
1196 CanDoAliasSetRT =
false;
1210 bool NeedsAliasSetRTCheck = RunningDepId > 2 || !Retries.
empty();
1214 if (NeedsAliasSetRTCheck && !CanDoAliasSetRT) {
1218 CanDoAliasSetRT =
true;
1219 for (
auto Retry : Retries) {
1220 MemAccessInfo Access = Retry.first;
1221 Type *AccessTy = Retry.second;
1222 if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
1223 DepSetId, TheLoop, RunningDepId, ASId,
1224 ShouldCheckWrap,
true)) {
1225 CanDoAliasSetRT =
false;
1226 UncomputablePtr = Access.getPointer();
1232 CanDoRT &= CanDoAliasSetRT;
1233 MayNeedRTCheck |= NeedsAliasSetRTCheck;
1242 unsigned NumPointers = RtCheck.
Pointers.size();
1243 for (
unsigned i = 0; i < NumPointers; ++i) {
1244 for (
unsigned j = i + 1;
j < NumPointers; ++
j) {
1246 if (RtCheck.
Pointers[i].DependencySetId ==
1247 RtCheck.
Pointers[j].DependencySetId)
1260 dbgs() <<
"LAA: Runtime check would require comparison between"
1261 " different address spaces\n");
1267 if (MayNeedRTCheck && CanDoRT)
1271 <<
" pointer comparisons.\n");
1278 bool CanDoRTIfNeeded = !RtCheck.
Need || CanDoRT;
1279 if (!CanDoRTIfNeeded)
1281 return CanDoRTIfNeeded;
1284void AccessAnalysis::processMemAccesses() {
1291 LLVM_DEBUG(
dbgs() <<
"LAA: Accesses(" << Accesses.size() <<
"):\n");
1293 for (
auto A : Accesses)
1294 dbgs() <<
"\t" << *
A.first.getPointer() <<
" ("
1295 << (
A.first.getInt()
1297 : (ReadOnlyPtr.count(
A.first.getPointer()) ?
"read-only"
1306 for (
const auto &AS : AST) {
1310 auto ASPointers = AS.getPointers();
1312 bool SetHasWrite =
false;
1316 UnderlyingObjToAccessMap ObjToLastAccess;
1319 PtrAccessMap DeferredAccesses;
1323 for (
int SetIteration = 0; SetIteration < 2; ++SetIteration) {
1324 bool UseDeferred = SetIteration > 0;
1325 PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
1327 for (
const Value *Ptr_ : ASPointers) {
1332 for (
const auto &AC : S) {
1333 if (AC.first.getPointer() !=
Ptr)
1336 bool IsWrite = AC.first.getInt();
1340 bool IsReadOnlyPtr = ReadOnlyPtr.count(
Ptr) && !IsWrite;
1341 if (UseDeferred && !IsReadOnlyPtr)
1345 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
1346 S.count(MemAccessInfo(
Ptr,
false))) &&
1347 "Alias-set pointer not in the access set?");
1349 MemAccessInfo Access(
Ptr, IsWrite);
1357 if (!UseDeferred && IsReadOnlyPtr) {
1360 DeferredAccesses.insert({Access, {}});
1368 if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
1369 CheckDeps.push_back(Access);
1370 IsRTCheckAnalysisNeeded =
true;
1379 ValueVector TempObjects;
1381 UnderlyingObjects[
Ptr] = {};
1385 <<
"Underlying objects for pointer " << *
Ptr <<
"\n");
1386 for (
const Value *UnderlyingObj : UOs) {
1389 if (isa<ConstantPointerNull>(UnderlyingObj) &&
1395 UnderlyingObjToAccessMap::iterator Prev =
1396 ObjToLastAccess.find(UnderlyingObj);
1397 if (Prev != ObjToLastAccess.end())
1398 DepCands.
unionSets(Access, Prev->second);
1400 ObjToLastAccess[UnderlyingObj] = Access;
1429 auto *
GEP = dyn_cast<GetElementPtrInst>(
Ptr);
1430 if (!
GEP || !
GEP->isInBounds())
1434 Value *NonConstIndex =
nullptr;
1436 if (!isa<ConstantInt>(
Index)) {
1439 NonConstIndex =
Index;
1447 if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(NonConstIndex))
1448 if (OBO->hasNoSignedWrap() &&
1451 isa<ConstantInt>(OBO->getOperand(1))) {
1452 auto *OpScev = PSE.
getSCEV(OBO->getOperand(0));
1454 if (
auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpScev))
1455 return OpAR->getLoop() == L && OpAR->getNoWrapFlags(
SCEV::FlagNSW);
1466 bool Assume,
bool ShouldCheckWrap) {
1470 if (isa<ScalableVectorType>(AccessTy)) {
1471 LLVM_DEBUG(
dbgs() <<
"LAA: Bad stride - Scalable object: " << *AccessTy
1473 return std::nullopt;
1484 <<
" SCEV: " << *PtrScev <<
"\n");
1485 return std::nullopt;
1490 LLVM_DEBUG(
dbgs() <<
"LAA: Bad stride - Not striding over innermost loop "
1491 << *
Ptr <<
" SCEV: " << *AR <<
"\n");
1492 return std::nullopt;
1502 <<
" SCEV: " << *AR <<
"\n");
1503 return std::nullopt;
1507 TypeSize AllocSize =
DL.getTypeAllocSize(AccessTy);
1509 const APInt &APStepVal =
C->getAPInt();
1513 return std::nullopt;
1518 int64_t Stride = StepVal /
Size;
1519 int64_t Rem = StepVal %
Size;
1521 return std::nullopt;
1523 if (!ShouldCheckWrap)
1535 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
Ptr);
1536 GEP &&
GEP->isInBounds() && (Stride == 1 || Stride == -1))
1544 (Stride == 1 || Stride == -1))
1550 <<
"LAA: Pointer: " << *
Ptr <<
"\n"
1551 <<
"LAA: SCEV: " << *AR <<
"\n"
1552 <<
"LAA: Added an overflow assumption\n");
1556 dbgs() <<
"LAA: Bad stride - Pointer may wrap in the address space "
1557 << *
Ptr <<
" SCEV: " << *AR <<
"\n");
1558 return std::nullopt;
1566 assert(PtrA && PtrB &&
"Expected non-nullptr pointers.");
1574 return std::nullopt;
1581 return std::nullopt;
1582 unsigned IdxWidth =
DL.getIndexSizeInBits(ASA);
1584 APInt OffsetA(IdxWidth, 0), OffsetB(IdxWidth, 0);
1589 if (PtrA1 == PtrB1) {
1592 ASA = cast<PointerType>(PtrA1->
getType())->getAddressSpace();
1593 ASB = cast<PointerType>(PtrB1->
getType())->getAddressSpace();
1596 return std::nullopt;
1598 IdxWidth =
DL.getIndexSizeInBits(ASA);
1599 OffsetA = OffsetA.sextOrTrunc(IdxWidth);
1609 dyn_cast<SCEVConstant>(SE.
getMinusSCEV(PtrSCEVB, PtrSCEVA));
1611 return std::nullopt;
1612 Val = Diff->getAPInt().getSExtValue();
1614 int Size =
DL.getTypeStoreSize(ElemTyA);
1615 int Dist = Val /
Size;
1619 if (!StrictCheck || Dist *
Size == Val)
1621 return std::nullopt;
1629 "Expected list of pointer operands.");
1632 Value *Ptr0 = VL[0];
1634 using DistOrdPair = std::pair<int64_t, int>;
1636 std::set<DistOrdPair,
decltype(Compare)> Offsets(Compare);
1637 Offsets.emplace(0, 0);
1639 bool IsConsecutive =
true;
1648 auto Res = Offsets.emplace(
Offset, Cnt);
1652 IsConsecutive = IsConsecutive && std::next(Res.first) == Offsets.end();
1655 SortedIndices.
clear();
1656 if (!IsConsecutive) {
1660 for (
const std::pair<int64_t, int> &Pair : Offsets) {
1661 SortedIndices[Cnt] = Pair.second;
1677 std::optional<int> Diff =
1680 return Diff && *Diff == 1;
1686 Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
1687 InstMap.push_back(SI);
1695 Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
1696 InstMap.push_back(LI);
1724 case ForwardButPreventsForwarding:
1726 case IndirectUnsafe:
1729 case BackwardVectorizable:
1731 case BackwardVectorizableButPreventsForwarding:
1744 case ForwardButPreventsForwarding:
1749 case BackwardVectorizable:
1751 case BackwardVectorizableButPreventsForwarding:
1752 case IndirectUnsafe:
1758bool MemoryDepChecker::couldPreventStoreLoadForward(
uint64_t Distance,
1772 const uint64_t NumItersForStoreLoadThroughMemory = 8 * TypeByteSize;
1774 uint64_t MaxVFWithoutSLForwardIssues = std::min(
1778 for (
uint64_t VF = 2 * TypeByteSize; VF <= MaxVFWithoutSLForwardIssues;
1782 if (Distance % VF && Distance / VF < NumItersForStoreLoadThroughMemory) {
1783 MaxVFWithoutSLForwardIssues = (VF >> 1);
1788 if (MaxVFWithoutSLForwardIssues < 2 * TypeByteSize) {
1790 dbgs() <<
"LAA: Distance " << Distance
1791 <<
" that could cause a store-load forwarding conflict\n");
1795 if (MaxVFWithoutSLForwardIssues < MinDepDistBytes &&
1796 MaxVFWithoutSLForwardIssues !=
1798 MinDepDistBytes = MaxVFWithoutSLForwardIssues;
1820 const SCEV &BackedgeTakenCount,
1841 const uint64_t ByteStride = Stride * TypeByteSize;
1845 const SCEV *CastedDist = &Dist;
1846 const SCEV *CastedProduct = Product;
1853 if (DistTypeSizeBits > ProductTypeSizeBits)
1881 assert(Stride > 1 &&
"The stride must be greater than 1");
1882 assert(TypeByteSize > 0 &&
"The type size in byte must be non-zero");
1883 assert(Distance > 0 &&
"The distance must be non-zero");
1886 if (Distance % TypeByteSize)
1889 uint64_t ScaledDist = Distance / TypeByteSize;
1907 return ScaledDist % Stride;
1915 return any_of(UnderlyingObjects, [&SE, L](
const Value *UO) {
1921struct DepDistanceStrideAndSizeInfo {
1928 DepDistanceStrideAndSizeInfo(
const SCEV *Dist,
uint64_t Stride,
1929 uint64_t TypeByteSize,
bool AIsWrite,
1931 : Dist(Dist), Stride(Stride), TypeByteSize(TypeByteSize),
1932 AIsWrite(AIsWrite), BIsWrite(BIsWrite) {}
1943 DepDistanceStrideAndSizeInfo>
1951 auto &SE = *PSE.
getSE();
1952 auto [APtr, AIsWrite] =
A;
1953 auto [BPtr, BIsWrite] =
B;
1956 if (!AIsWrite && !BIsWrite)
1963 if (APtr->getType()->getPointerAddressSpace() !=
1964 BPtr->getType()->getPointerAddressSpace())
1967 int64_t StrideAPtr =
1968 getPtrStride(PSE, ATy, APtr, InnermostLoop, Strides,
true).value_or(0);
1969 int64_t StrideBPtr =
1970 getPtrStride(PSE, BTy, BPtr, InnermostLoop, Strides,
true).value_or(0);
1978 if (StrideAPtr < 0) {
1985 LLVM_DEBUG(
dbgs() <<
"LAA: Src Scev: " << *Src <<
"Sink Scev: " << *Sink
1986 <<
"(Induction step: " << StrideAPtr <<
")\n");
1987 LLVM_DEBUG(
dbgs() <<
"LAA: Distance for " << *AInst <<
" to " << *BInst
1988 <<
": " << *Dist <<
"\n");
2001 if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr) {
2002 LLVM_DEBUG(
dbgs() <<
"Pointer access with non-constant stride\n");
2006 uint64_t TypeByteSize =
DL.getTypeAllocSize(ATy);
2008 DL.getTypeStoreSizeInBits(ATy) ==
DL.getTypeStoreSizeInBits(BTy);
2011 uint64_t Stride = std::abs(StrideAPtr);
2012 return DepDistanceStrideAndSizeInfo(Dist, Stride, TypeByteSize, AIsWrite,
2020 &UnderlyingObjects) {
2021 assert(AIdx < BIdx &&
"Must pass arguments in program order");
2026 A, InstMap[AIdx],
B, InstMap[BIdx], Strides, UnderlyingObjects, PSE,
2028 if (std::holds_alternative<Dependence::DepType>(Res))
2029 return std::get<Dependence::DepType>(Res);
2031 const auto &[Dist, Stride, TypeByteSize, AIsWrite, BIsWrite] =
2032 std::get<DepDistanceStrideAndSizeInfo>(Res);
2033 bool HasSameSize = TypeByteSize > 0;
2037 if (!isa<SCEVCouldNotCompute>(Dist) && HasSameSize &&
2039 Stride, TypeByteSize))
2044 LLVM_DEBUG(
dbgs() <<
"LAA: Dependence because of non-constant distance\n");
2045 FoundNonConstantDistanceDependence =
true;
2049 const APInt &Val =
C->getAPInt();
2053 if (std::abs(Distance) > 0 && Stride > 1 && HasSameSize &&
2061 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
2066 (!HasSameSize || couldPreventStoreLoadForward(Val.
abs().
getZExtValue(),
2068 LLVM_DEBUG(
dbgs() <<
"LAA: Forward but may prevent st->ld forwarding\n");
2081 dbgs() <<
"LAA: Zero dependence difference but different type sizes\n");
2088 LLVM_DEBUG(
dbgs() <<
"LAA: ReadWrite-Write positive dependency with "
2089 "different type sizes\n");
2099 unsigned MinNumIter = std::max(ForcedFactor * ForcedUnroll, 2U);
2128 TypeByteSize * Stride * (MinNumIter - 1) + TypeByteSize;
2129 if (MinDistanceNeeded >
static_cast<uint64_t>(Distance)) {
2130 LLVM_DEBUG(
dbgs() <<
"LAA: Failure because of positive distance "
2131 << Distance <<
'\n');
2137 if (MinDistanceNeeded > MinDepDistBytes) {
2139 << MinDistanceNeeded <<
" size in bytes\n");
2160 std::min(
static_cast<uint64_t>(Distance), MinDepDistBytes);
2162 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
2163 uint64_t MinDepDistBytesOld = MinDepDistBytes;
2165 couldPreventStoreLoadForward(Distance, TypeByteSize)) {
2168 assert(MinDepDistBytes == MinDepDistBytesOld &&
2169 "An update to MinDepDistBytes requires an update to "
2170 "MaxSafeVectorWidthInBits");
2171 (void)MinDepDistBytesOld;
2177 uint64_t MaxVF = MinDepDistBytes / (TypeByteSize * Stride);
2179 <<
" with max VF = " << MaxVF <<
'\n');
2180 uint64_t MaxVFInBits = MaxVF * TypeByteSize * 8;
2181 MaxSafeVectorWidthInBits = std::min(MaxSafeVectorWidthInBits, MaxVFInBits);
2189 &UnderlyingObjects) {
2191 MinDepDistBytes = -1;
2194 if (Visited.
count(CurAccess))
2210 bool AIIsWrite = AI->getInt();
2214 (AIIsWrite ? AI : std::next(AI));
2217 for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
2218 I1E = Accesses[*AI].
end(); I1 != I1E; ++I1)
2221 for (std::vector<unsigned>::iterator
2222 I2 = (OI == AI ? std::next(I1) : Accesses[*OI].begin()),
2223 I2E = (OI == AI ? I1E : Accesses[*OI].end());
2225 auto A = std::make_pair(&*AI, *I1);
2226 auto B = std::make_pair(&*OI, *I2);
2233 isDependent(*
A.first,
A.second, *
B.first,
B.second, Strides,
2241 if (RecordDependences) {
2246 RecordDependences =
false;
2247 Dependences.clear();
2249 <<
"Too many dependences, stopped recording\n");
2261 LLVM_DEBUG(
dbgs() <<
"Total Dependences: " << Dependences.size() <<
"\n");
2268 auto &IndexVector = Accesses.find(Access)->second;
2272 std::back_inserter(Insts),
2273 [&](
unsigned Idx) {
return this->InstMap[
Idx]; });
2282 "ForwardButPreventsForwarding",
2284 "BackwardVectorizable",
2285 "BackwardVectorizableButPreventsForwarding"};
2295bool LoopAccessInfo::canAnalyzeLoop() {
2304 recordAnalysis(
"NotInnerMostLoop") <<
"loop is not the innermost loop";
2311 dbgs() <<
"LAA: loop control flow is not understood by analyzer\n");
2312 recordAnalysis(
"CFGNotUnderstood")
2313 <<
"loop control flow is not understood by analyzer";
2319 if (isa<SCEVCouldNotCompute>(ExitCount)) {
2320 recordAnalysis(
"CantComputeNumberOfIterations")
2321 <<
"could not determine number of loop iterations";
2322 LLVM_DEBUG(
dbgs() <<
"LAA: SCEV could not compute the loop exit count.\n");
2338 unsigned NumReads = 0;
2339 unsigned NumReadWrites = 0;
2341 bool HasComplexMemInst =
false;
2344 HasConvergentOp =
false;
2346 PtrRtChecking->Pointers.
clear();
2347 PtrRtChecking->Need =
false;
2351 const bool EnableMemAccessVersioningOfLoop =
2363 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
2364 if (
Call->isConvergent())
2365 HasConvergentOp =
true;
2370 if (HasComplexMemInst && HasConvergentOp) {
2376 if (HasComplexMemInst)
2380 if (
auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&
I))
2381 for (
Metadata *
Op : Decl->getScopeList()->operands())
2382 LoopAliasScopes.
insert(cast<MDNode>(
Op));
2387 auto *
Call = dyn_cast<CallInst>(&
I);
2394 if (
I.mayReadFromMemory()) {
2397 if (Call && !
Call->isNoBuiltin() &&
Call->getCalledFunction() &&
2401 auto *Ld = dyn_cast<LoadInst>(&
I);
2403 recordAnalysis(
"CantVectorizeInstruction", Ld)
2404 <<
"instruction cannot be vectorized";
2405 HasComplexMemInst =
true;
2408 if (!Ld->isSimple() && !IsAnnotatedParallel) {
2409 recordAnalysis(
"NonSimpleLoad", Ld)
2410 <<
"read with atomic ordering or volatile read";
2412 HasComplexMemInst =
true;
2418 if (EnableMemAccessVersioningOfLoop)
2419 collectStridedAccess(Ld);
2424 if (
I.mayWriteToMemory()) {
2425 auto *St = dyn_cast<StoreInst>(&
I);
2427 recordAnalysis(
"CantVectorizeInstruction", St)
2428 <<
"instruction cannot be vectorized";
2429 HasComplexMemInst =
true;
2432 if (!St->isSimple() && !IsAnnotatedParallel) {
2433 recordAnalysis(
"NonSimpleStore", St)
2434 <<
"write with atomic ordering or volatile write";
2436 HasComplexMemInst =
true;
2442 if (EnableMemAccessVersioningOfLoop)
2443 collectStridedAccess(St);
2448 if (HasComplexMemInst) {
2458 if (!Stores.
size()) {
2465 AccessAnalysis Accesses(TheLoop, AA, LI, DependentAccesses, *PSE,
2482 if (isInvariant(
Ptr)) {
2484 StoresToInvariantAddresses.push_back(ST);
2485 HasDependenceInvolvingLoopInvariantAddress |=
2492 if (Seen.
insert({Ptr, AccessTy}).second) {
2499 if (blockNeedsPredication(
ST->getParent(), TheLoop, DT))
2503 [&Accesses, AccessTy, Loc](
Value *
Ptr) {
2504 MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
2505 Accesses.addStore(NewLoc, AccessTy);
2510 if (IsAnnotatedParallel) {
2512 dbgs() <<
"LAA: A loop annotated parallel, ignore memory dependency "
2528 bool IsReadOnlyPtr =
false;
2530 if (Seen.
insert({Ptr, AccessTy}).second ||
2531 !
getPtrStride(*PSE,
LD->getType(),
Ptr, TheLoop, SymbolicStrides).value_or(0)) {
2533 IsReadOnlyPtr =
true;
2539 LLVM_DEBUG(
dbgs() <<
"LAA: Found an unsafe dependency between a uniform "
2540 "load and uniform store to the same address!\n");
2541 HasDependenceInvolvingLoopInvariantAddress =
true;
2548 if (blockNeedsPredication(
LD->getParent(), TheLoop, DT))
2552 [&Accesses, AccessTy, Loc, IsReadOnlyPtr](
Value *
Ptr) {
2553 MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
2554 Accesses.addLoad(NewLoc, AccessTy, IsReadOnlyPtr);
2560 if (NumReadWrites == 1 && NumReads == 0) {
2568 Accesses.buildDependenceSets();
2572 Value *UncomputablePtr =
nullptr;
2573 bool CanDoRTIfNeeded =
2574 Accesses.canCheckPtrAtRT(*PtrRtChecking, PSE->
getSE(), TheLoop,
2575 SymbolicStrides, UncomputablePtr,
false);
2576 if (!CanDoRTIfNeeded) {
2577 auto *
I = dyn_cast_or_null<Instruction>(UncomputablePtr);
2578 recordAnalysis(
"CantIdentifyArrayBounds",
I)
2579 <<
"cannot identify array bounds";
2580 LLVM_DEBUG(
dbgs() <<
"LAA: We can't vectorize because we can't find "
2581 <<
"the array bounds.\n");
2587 dbgs() <<
"LAA: May be able to perform a memory runtime check if needed.\n");
2590 if (Accesses.isDependencyCheckNeeded()) {
2593 DependentAccesses, Accesses.getDependenciesToCheck(), SymbolicStrides,
2594 Accesses.getUnderlyingObjects());
2600 Accesses.resetDepChecks(*DepChecker);
2602 PtrRtChecking->reset();
2603 PtrRtChecking->Need =
true;
2605 auto *SE = PSE->
getSE();
2606 UncomputablePtr =
nullptr;
2607 CanDoRTIfNeeded = Accesses.canCheckPtrAtRT(
2608 *PtrRtChecking, SE, TheLoop, SymbolicStrides, UncomputablePtr,
true);
2611 if (!CanDoRTIfNeeded) {
2612 auto *
I = dyn_cast_or_null<Instruction>(UncomputablePtr);
2613 recordAnalysis(
"CantCheckMemDepsAtRunTime",
I)
2614 <<
"cannot check memory dependencies at runtime";
2615 LLVM_DEBUG(
dbgs() <<
"LAA: Can't vectorize with memory checks\n");
2624 if (HasConvergentOp) {
2625 recordAnalysis(
"CantInsertRuntimeCheckWithConvergent")
2626 <<
"cannot add control dependency to convergent operation";
2627 LLVM_DEBUG(
dbgs() <<
"LAA: We can't vectorize because a runtime check "
2628 "would be needed with a convergent operation\n");
2635 dbgs() <<
"LAA: No unsafe dependent memory operations in loop. We"
2636 << (PtrRtChecking->Need ?
"" :
" don't")
2637 <<
" need runtime memory checks.\n");
2639 emitUnsafeDependenceRemark();
2642void LoopAccessInfo::emitUnsafeDependenceRemark() {
2643 auto Deps = getDepChecker().getDependences();
2650 if (Found == Deps->end())
2654 LLVM_DEBUG(
dbgs() <<
"LAA: unsafe dependent memory operations in loop\n");
2657 bool HasForcedDistribution =
false;
2658 std::optional<const MDOperand *>
Value =
2662 assert(
Op && mdconst::hasa<ConstantInt>(*
Op) &&
"invalid metadata");
2663 HasForcedDistribution = mdconst::extract<ConstantInt>(*Op)->getZExtValue();
2666 const std::string
Info =
2667 HasForcedDistribution
2668 ?
"unsafe dependent memory operations in loop."
2669 :
"unsafe dependent memory operations in loop. Use "
2670 "#pragma clang loop distribute(enable) to allow loop distribution "
2671 "to attempt to isolate the offending operations into a separate "
2682 R <<
"\nBackward loop carried data dependence.";
2685 R <<
"\nForward loop carried data dependence that prevents "
2686 "store-to-load forwarding.";
2689 R <<
"\nBackward loop carried data dependence that prevents "
2690 "store-to-load forwarding.";
2693 R <<
"\nUnsafe indirect dependence.";
2696 R <<
"\nUnknown data dependence.";
2703 SourceLoc = DD->getDebugLoc();
2705 R <<
" Memory location is the same as accessed at "
2706 <<
ore::NV(
"Location", SourceLoc);
2721 assert(!Report &&
"Multiple reports generated");
2727 CodeRegion =
I->getParent();
2730 if (
I->getDebugLoc())
2731 DL =
I->getDebugLoc();
2734 Report = std::make_unique<OptimizationRemarkAnalysis>(
DEBUG_TYPE, RemarkName,
DL,
2740 auto *SE = PSE->
getSE();
2761 std::advance(GEPTI, LastOperand - 2);
2768 if (ElemSize != GEPAllocSize)
2788 for (
unsigned i = 0, e =
GEP->getNumOperands(); i != e; ++i)
2789 if (i != InductionOperand &&
2792 return GEP->getOperand(InductionOperand);
2797 Value *UniqueCast =
nullptr;
2798 for (
User *U :
Ptr->users()) {
2799 CastInst *CI = dyn_cast<CastInst>(U);
2800 if (CI && CI->
getType() == Ty) {
2813 auto *PtrTy = dyn_cast<PointerType>(
Ptr->getType());
2814 if (!PtrTy || PtrTy->isAggregateType())
2823 int64_t PtrAccessSize = 1;
2831 V =
C->getOperand();
2848 if (OrigPtr ==
Ptr) {
2849 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
2850 if (M->getOperand(0)->getSCEVType() !=
scConstant)
2853 const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
2860 if (PtrAccessSize != StepVal)
2862 V = M->getOperand(1);
2874 const auto *
C = dyn_cast<SCEVIntegralCastExpr>(V);
2877 U = dyn_cast<SCEVUnknown>(
C->getOperand());
2889void LoopAccessInfo::collectStridedAccess(
Value *MemAccess) {
2904 LLVM_DEBUG(
dbgs() <<
"LAA: Found a strided access that is a candidate for "
2909 LLVM_DEBUG(
dbgs() <<
" Chose not to due to -laa-speculate-unit-stride\n");
2934 const SCEV *CastedStride = StrideExpr;
2935 const SCEV *CastedBECount = BETakenCount;
2937 if (BETypeSizeBits >= StrideTypeSizeBits)
2941 const SCEV *StrideMinusBETaken = SE->
getMinusSCEV(CastedStride, CastedBECount);
2947 dbgs() <<
"LAA: Stride>=TripCount; No point in versioning as the "
2948 "Stride==1 predicate will imply that the loop executes "
2952 LLVM_DEBUG(
dbgs() <<
"LAA: Found a strided access that we can version.\n");
2956 const SCEV *StrideBase = StrideExpr;
2957 if (
const auto *
C = dyn_cast<SCEVIntegralCastExpr>(StrideBase))
2958 StrideBase =
C->getOperand();
2959 SymbolicStrides[
Ptr] = cast<SCEVUnknown>(StrideBase);
2966 PtrRtChecking(nullptr),
2968 PtrRtChecking = std::make_unique<RuntimePointerChecking>(*DepChecker, SE);
2969 if (canAnalyzeLoop()) {
2970 analyzeLoop(AA, LI, TLI, DT);
2979 OS <<
" with a maximum safe vector width of "
2981 if (PtrRtChecking->Need)
2982 OS <<
" with run-time checks";
2986 if (HasConvergentOp)
2994 for (
const auto &Dep : *Dependences) {
3002 PtrRtChecking->print(
OS,
Depth);
3005 OS.
indent(
Depth) <<
"Non vectorizable stores to invariant address were "
3006 << (HasDependenceInvolvingLoopInvariantAddress ?
"" :
"not ")
3007 <<
"found in loop.\n";
3019 auto I = LoopAccessInfoMap.insert({&L,
nullptr});
3023 std::make_unique<LoopAccessInfo>(&L, &SE, TLI, &AA, &DT, &LI);
3025 return *
I.first->second;
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Analysis containing CSE Info
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
static cl::opt< unsigned > MaxDependences("max-dependences", cl::Hidden, cl::desc("Maximum number of dependences collected by " "loop-access analysis (default = 100)"), cl::init(100))
We collect dependences up to this threshold.
static cl::opt< bool > EnableForwardingConflictDetection("store-to-load-forwarding-conflict-detection", cl::Hidden, cl::desc("Enable conflict detection in loop-access analysis"), cl::init(true))
Enable store-to-load forwarding conflict detection.
static void findForkedSCEVs(ScalarEvolution *SE, const Loop *L, Value *Ptr, SmallVectorImpl< PointerIntPair< const SCEV *, 1, bool > > &ScevList, unsigned Depth)
static std::variant< MemoryDepChecker::Dependence::DepType, DepDistanceStrideAndSizeInfo > getDependenceDistanceStrideAndSize(const AccessAnalysis::MemAccessInfo &A, Instruction *AInst, const AccessAnalysis::MemAccessInfo &B, Instruction *BInst, const DenseMap< Value *, const SCEV * > &Strides, const DenseMap< Value *, SmallVector< const Value *, 16 > > &UnderlyingObjects, PredicatedScalarEvolution &PSE, const Loop *InnermostLoop)
static bool hasComputableBounds(PredicatedScalarEvolution &PSE, Value *Ptr, const SCEV *PtrScev, Loop *L, bool Assume)
Check whether a pointer can participate in a runtime bounds check.
static cl::opt< unsigned > MemoryCheckMergeThreshold("memory-check-merge-threshold", cl::Hidden, cl::desc("Maximum number of comparisons done when trying to merge " "runtime memory checks. (default = 100)"), cl::init(100))
The maximum iterations used to merge memory checks.
static bool isNoWrap(PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &Strides, Value *Ptr, Type *AccessTy, Loop *L)
Check whether a pointer address cannot wrap.
static const SCEV * getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
Get the stride of a pointer access in a loop.
static unsigned getGEPInductionOperand(const GetElementPtrInst *Gep)
Find the operand of the GEP that should be checked for consecutive stores.
static cl::opt< unsigned, true > VectorizationInterleave("force-vector-interleave", cl::Hidden, cl::desc("Sets the vectorization interleave count. " "Zero is autoselect."), cl::location(VectorizerParams::VectorizationInterleave))
static bool isLoopVariantIndirectAddress(ArrayRef< const Value * > UnderlyingObjects, ScalarEvolution &SE, const Loop *L)
Returns true if any of the underlying objects has a loop varying address, i.e.
static Value * getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty)
If a value has only one user that is a CastInst, return it.
static cl::opt< bool, true > HoistRuntimeChecks("hoist-runtime-checks", cl::Hidden, cl::desc("Hoist inner loop runtime memory checks to outer loop if possible"), cl::location(VectorizerParams::HoistRuntimeChecks), cl::init(true))
static cl::opt< unsigned, true > VectorizationFactor("force-vector-width", cl::Hidden, cl::desc("Sets the SIMD width. Zero is autoselect."), cl::location(VectorizerParams::VectorizationFactor))
static cl::opt< unsigned, true > RuntimeMemoryCheckThreshold("runtime-memory-check-threshold", cl::Hidden, cl::desc("When performing memory disambiguation checks at runtime do not " "generate more than this number of comparisons (default = 8)."), cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8))
static void visitPointers(Value *StartPtr, const Loop &InnermostLoop, function_ref< void(Value *)> AddPointer)
static bool isNoWrapAddRec(Value *Ptr, const SCEVAddRecExpr *AR, PredicatedScalarEvolution &PSE, const Loop *L)
Return true if an AddRec pointer Ptr is unsigned non-wrapping, i.e.
static Value * stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
If the argument is a GEP, then returns the operand identified by getGEPInductionOperand.
static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE, const SCEV &BackedgeTakenCount, const SCEV &Dist, uint64_t Stride, uint64_t TypeByteSize)
Given a dependence-distance Dist between two memory accesses, that have the same stride whose absolut...
static bool areStridedAccessesIndependent(uint64_t Distance, uint64_t Stride, uint64_t TypeByteSize)
Check the dependence for two accesses with the same stride Stride.
static const SCEV * getMinFromExprs(const SCEV *I, const SCEV *J, ScalarEvolution *SE)
Compare I and J and return the minimum.
static cl::opt< unsigned > MaxForkedSCEVDepth("max-forked-scev-depth", cl::Hidden, cl::desc("Maximum recursion depth when finding forked SCEVs (default = 5)"), cl::init(5))
static cl::opt< bool > SpeculateUnitStride("laa-speculate-unit-stride", cl::Hidden, cl::desc("Speculate that non-constant strides are unit in LAA"), cl::init(true))
static SmallVector< PointerIntPair< const SCEV *, 1, bool > > findForkedPointer(PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &StridesMap, Value *Ptr, const Loop *L)
static cl::opt< bool > EnableMemAccessVersioning("enable-mem-access-versioning", cl::init(true), cl::Hidden, cl::desc("Enable symbolic stride memory access versioning"))
This enables versioning on the strides of symbolically striding memory accesses in code like the foll...
This header provides classes for managing per-loop analyses.
This file provides utility analysis objects describing memory locations.
FunctionAnalysisManager FAM
This header defines various interfaces for pass management in LLVM.
This file defines the PointerIntPair class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckType(MVT::SimpleValueType VT, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
static const X86InstrFMA3Group Groups[]
A manager for alias analyses.
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
APInt abs() const
Get the absolute value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool isNegative() const
Determine sign of this APInt.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
int64_t getSExtValue() const
Get sign extended value.
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
API to communicate dependencies between analyses during invalidation.
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This is the base class for all instructions that perform data casts.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
iterator findValue(const ElemTy &V) const
findValue - Return an iterator to the specified value.
iterator insert(const ElemTy &Data)
insert - Insert a new value into the union/find set, ignoring the request if the value already exists...
member_iterator member_end() const
typename std::set< ECValue, ECValueComparator >::const_iterator iterator
iterator* - Provides a way to iterate over all values in the set.
member_iterator member_begin(iterator I) const
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...
const ElemTy & getLeaderValue(const ElemTy &V) const
getLeaderValue - Return the leader for the specified value that is in the set.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Type * getResultElementType() const
PointerType * getType() const
Global values are always pointers.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class to represent integer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
An instruction for reading from memory.
Value * getPointerOperand()
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
This analysis provides dependence information for the memory accesses of a loop.
Result run(Function &F, FunctionAnalysisManager &AM)
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
const LoopAccessInfo & getInfo(Loop &L)
Drive the analysis of memory accesses in the loop.
const MemoryDepChecker & getDepChecker() const
the Memory Dependence Checker which can determine the loop-independent and loop-carried dependences b...
bool isInvariant(Value *V) const
Returns true if value V is loop invariant.
void print(raw_ostream &OS, unsigned Depth=0) const
Print the information about the memory accesses in the loop.
LoopAccessInfo(Loop *L, ScalarEvolution *SE, const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT, LoopInfo *LI)
static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop, DominatorTree *DT)
Return true if the block BB needs to be predicated in order for the loop to be vectorized.
Analysis pass that exposes the LoopInfo for a function.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
BlockT * getHeader() const
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
Represents a single loop in the control flow graph.
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
ArrayRef< MDOperand > operands() const
Tracking metadata reference owned by Metadata.
This class implements a map that also provides access to all stored values in a deterministic order.
Checks memory dependences among accesses to the same underlying object to determine whether there vec...
ArrayRef< unsigned > getOrderForAccess(Value *Ptr, bool IsWrite) const
Return the program order indices for the access location (Ptr, IsWrite).
bool isSafeForAnyVectorWidth() const
Return true if the number of elements that are safe to operate on simultaneously is not bounded.
bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps, const DenseMap< Value *, const SCEV * > &Strides, const DenseMap< Value *, SmallVector< const Value *, 16 > > &UnderlyingObjects)
Check whether the dependencies between the accesses are safe.
const SmallVectorImpl< Instruction * > & getMemoryInstructions() const
The vector of memory access instructions.
const Loop * getInnermostLoop() const
uint64_t getMaxSafeVectorWidthInBits() const
Return the number of elements that are safe to operate on simultaneously, multiplied by the size of t...
bool isSafeForVectorization() const
No memory dependence was encountered that would inhibit vectorization.
const SmallVectorImpl< Dependence > * getDependences() const
Returns the memory dependences.
SmallVector< Instruction *, 4 > getInstructionsForAccess(Value *Ptr, bool isWrite) const
Find the set of instructions that read or write via Ptr.
VectorizationSafetyStatus
Type to keep track of the status of the dependence check.
@ PossiblySafeWithRtChecks
bool shouldRetryWithRuntimeCheck() const
In same cases when the dependency check fails we can still vectorize the loop with a dynamic array ac...
void addAccess(StoreInst *SI)
Register the location (instructions are given increasing numbers) of a write access.
PointerIntPair< Value *, 1, bool > MemAccessInfo
Representation for a specific memory location.
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
const SCEVPredicate & getPredicate() const
bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Returns true if we've proved that V doesn't wrap by means of a SCEV predicate.
void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Proves that V doesn't overflow by adding SCEV predicate.
void print(raw_ostream &OS, unsigned Depth) const
Print the SCEV mappings done by the Predicated Scalar Evolution.
const SCEVAddRecExpr * getAsAddRec(Value *V)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
A set of analyses that are preserved following a run of a transformation pass.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Holds information about the memory runtime legality checks to verify that a group of pointers do not ...
bool Need
This flag indicates if we need to add the runtime check.
void reset()
Reset the state of the pointer runtime information.
unsigned getNumberOfChecks() const
Returns the number of run-time checks required according to needsChecking.
void printChecks(raw_ostream &OS, const SmallVectorImpl< RuntimePointerCheck > &Checks, unsigned Depth=0) const
Print Checks.
bool needsChecking(const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const
Decide if we need to add a check between two groups of pointers, according to needsChecking.
void print(raw_ostream &OS, unsigned Depth=0) const
Print the list run-time memory checks necessary.
SmallVector< RuntimeCheckingPtrGroup, 2 > CheckingGroups
Holds a partitioning of pointers into "check groups".
void generateChecks(MemoryDepChecker::DepCandidates &DepCands, bool UseDependencies)
Generate the checks and store it.
friend struct RuntimeCheckingPtrGroup
static bool arePointersInSamePartition(const SmallVectorImpl< int > &PtrToPartition, unsigned PtrIdx1, unsigned PtrIdx2)
Check if pointers are in the same partition.
SmallVector< PointerInfo, 2 > Pointers
Information about the pointers that may require checking.
void insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr, Type *AccessTy, bool WritePtr, unsigned DepSetId, unsigned ASId, PredicatedScalarEvolution &PSE, bool NeedsFreeze)
Insert a pointer and calculate the start and end SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
const SCEV * getStart() const
const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const Loop * getLoop() const
This class represents a constant integer value.
This is the base class for unary integral cast operator classes.
This node represents multiplication of some number of SCEVs.
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
virtual void print(raw_ostream &OS, unsigned Depth=0) const =0
Prints a textual representation of this predicate with an indentation of Depth.
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
const SCEVPredicate * getEqualPredicate(const SCEV *LHS, const SCEV *RHS)
const SCEV * getConstant(ConstantInt *V)
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
const SCEV * getPtrToIntExpr(const SCEV *Op, Type *Ty)
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
const SCEV * getUMinExpr(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
const SCEV * getSizeOfExpr(Type *IntTy, TypeSize Size)
Return an expression for a TypeSize.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isPointerTy() const
True if this is an instance of PointerType.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
static SmallVector< VFInfo, 8 > getMappings(const CallInst &CI)
Retrieve all the VFInfo instances associated to the CallInst CI.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
StringRef getName() const
Return a constant reference to the value's name.
constexpr ScalarTy getFixedValue() const
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
Type * getIndexedType() const
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
friend const_iterator end(StringRef path)
Get end iterator over path.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
bool match(Val *V, const Pattern &P)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
std::optional< int > getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB, Value *PtrB, const DataLayout &DL, ScalarEvolution &SE, bool StrictCheck=false, bool CheckType=true)
Returns the distance between the pointers PtrA and PtrB iff they are compatible and it is possible to...
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
unsigned getPointerAddressSpace(const Type *T)
std::optional< const MDOperand * > findStringMetadataForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for loop.
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isPointerTy(const Type *T)
std::optional< int64_t > getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp, const DenseMap< Value *, const SCEV * > &StridesMap=DenseMap< Value *, const SCEV * >(), bool Assume=false, bool ShouldCheckWrap=true)
If the pointer has a constant stride return it in units of the access type size.
bool sortPtrAccesses(ArrayRef< Value * > VL, Type *ElemTy, const DataLayout &DL, ScalarEvolution &SE, SmallVectorImpl< unsigned > &SortedIndices)
Attempt to sort the pointers in VL and return the sorted indices in SortedIndices,...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
const SCEV * replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &PtrToStride, Value *Ptr)
Return the SCEV corresponding to a pointer with the symbolic stride replaced with constant one,...
bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL, ScalarEvolution &SE, bool CheckType=true)
Returns true if the memory operations A and B are consecutive.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
OutputIt copy(R &&Range, OutputIt Out)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
gep_type_iterator gep_type_begin(const User *GEP)
Type * getLoadStoreType(Value *I)
A helper function that returns the type of a load or store instruction.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
MDNode * Scope
The tag for alias scope specification (used with noalias).
MDNode * TBAA
The tag for type-based alias analysis.
MDNode * NoAlias
The tag specifying the noalias scope.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Dependece between memory access instructions.
DepType Type
The type of the dependence.
bool isPossiblyBackward() const
May be a lexically backward dependence type (includes Unknown).
bool isForward() const
Lexically forward dependence.
bool isBackward() const
Lexically backward dependence.
void print(raw_ostream &OS, unsigned Depth, const SmallVectorImpl< Instruction * > &Instrs) const
Print the dependence.
Instruction * getDestination(const LoopAccessInfo &LAI) const
Return the destination instruction of the dependence.
Instruction * getSource(const LoopAccessInfo &LAI) const
Return the source instruction of the dependence.
DepType
The type of the dependence.
@ BackwardVectorizableButPreventsForwarding
@ ForwardButPreventsForwarding
static const char * DepName[]
String version of the types.
static VectorizationSafetyStatus isSafeForVectorization(DepType Type)
Dependence types that don't prevent vectorization.
unsigned AddressSpace
Address space of the involved pointers.
bool addPointer(unsigned Index, RuntimePointerChecking &RtCheck)
Tries to add the pointer recorded in RtCheck at index Index to this pointer checking group.
bool NeedsFreeze
Whether the pointer needs to be frozen after expansion, e.g.
const SCEV * High
The SCEV expression which represents the upper bound of all the pointers in this group.
SmallVector< unsigned, 2 > Members
Indices of all the pointers that constitute this grouping.
RuntimeCheckingPtrGroup(unsigned Index, RuntimePointerChecking &RtCheck)
Create a new pointer checking group containing a single pointer, with index Index in RtCheck.
const SCEV * Low
The SCEV expression which represents the lower bound of all the pointers in this group.
bool IsWritePtr
Holds the information if this pointer is used for writing to memory.
unsigned DependencySetId
Holds the id of the set of pointers that could be dependent because of a shared underlying object.
unsigned AliasSetId
Holds the id of the disjoint alias set to which this pointer belongs.
static const unsigned MaxVectorWidth
Maximum SIMD width.
static unsigned VectorizationFactor
VF as overridden by the user.
static unsigned RuntimeMemoryCheckThreshold
\When performing memory disambiguation checks at runtime do not make more than this number of compari...
static bool isInterleaveForced()
True if force-vector-interleave was specified by the user.
static unsigned VectorizationInterleave
Interleave factor as overridden by the user.
static bool HoistRuntimeChecks
Function object to check whether the first component of a container supported by std::get (like std::...