Skip to content

Commit b646876

Browse files
authored
[LAA] refactor program logic (NFC) (#92101)
Implement NFC improvements spotted during a cursory reading of LoopAccessAnalysis.
1 parent 964079d commit b646876

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ void RuntimePointerChecking::generateChecks(
392392

393393
bool RuntimePointerChecking::needsChecking(
394394
const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const {
395-
for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I)
396-
for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J)
397-
if (needsChecking(M.Members[I], N.Members[J]))
395+
for (const auto &I : M.Members)
396+
for (const auto &J : N.Members)
397+
if (needsChecking(I, J))
398398
return true;
399399
return false;
400400
}
@@ -408,9 +408,7 @@ static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
408408

409409
if (!C)
410410
return nullptr;
411-
if (C->getValue()->isNegative())
412-
return J;
413-
return I;
411+
return C->getValue()->isNegative() ? J : I;
414412
}
415413

416414
bool RuntimeCheckingPtrGroup::addPointer(unsigned Index,
@@ -508,8 +506,8 @@ void RuntimePointerChecking::groupChecks(
508506

509507
DenseMap<Value *, SmallVector<unsigned>> PositionMap;
510508
for (unsigned Index = 0; Index < Pointers.size(); ++Index) {
511-
auto Iter = PositionMap.insert({Pointers[Index].PointerValue, {}});
512-
Iter.first->second.push_back(Index);
509+
auto [It, _] = PositionMap.insert({Pointers[Index].PointerValue, {}});
510+
It->second.push_back(Index);
513511
}
514512

515513
// We need to keep track of what pointers we've already seen so we
@@ -608,16 +606,16 @@ void RuntimePointerChecking::printChecks(
608606
raw_ostream &OS, const SmallVectorImpl<RuntimePointerCheck> &Checks,
609607
unsigned Depth) const {
610608
unsigned N = 0;
611-
for (const auto &Check : Checks) {
612-
const auto &First = Check.first->Members, &Second = Check.second->Members;
609+
for (const auto &[Check1, Check2] : Checks) {
610+
const auto &First = Check1->Members, &Second = Check2->Members;
613611

614612
OS.indent(Depth) << "Check " << N++ << ":\n";
615613

616-
OS.indent(Depth + 2) << "Comparing group (" << Check.first << "):\n";
614+
OS.indent(Depth + 2) << "Comparing group (" << Check1 << "):\n";
617615
for (unsigned K = 0; K < First.size(); ++K)
618616
OS.indent(Depth + 2) << *Pointers[First[K]].PointerValue << "\n";
619617

620-
OS.indent(Depth + 2) << "Against group (" << Check.second << "):\n";
618+
OS.indent(Depth + 2) << "Against group (" << Check2 << "):\n";
621619
for (unsigned K = 0; K < Second.size(); ++K)
622620
OS.indent(Depth + 2) << *Pointers[Second[K]].PointerValue << "\n";
623621
}
@@ -1158,8 +1156,8 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
11581156
// First, count how many write and read accesses are in the alias set. Also
11591157
// collect MemAccessInfos for later.
11601158
SmallVector<MemAccessInfo, 4> AccessInfos;
1161-
for (const Value *Ptr_ : ASPointers) {
1162-
Value *Ptr = const_cast<Value *>(Ptr_);
1159+
for (const Value *ConstPtr : ASPointers) {
1160+
Value *Ptr = const_cast<Value *>(ConstPtr);
11631161
bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
11641162
if (IsWrite)
11651163
++NumWritePtrChecks;
@@ -1215,9 +1213,7 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
12151213
// We know that we need these checks, so we can now be more aggressive
12161214
// and add further checks if required (overflow checks).
12171215
CanDoAliasSetRT = true;
1218-
for (auto Retry : Retries) {
1219-
MemAccessInfo Access = Retry.first;
1220-
Type *AccessTy = Retry.second;
1216+
for (const auto &[Access, AccessTy] : Retries) {
12211217
if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
12221218
DepSetId, TheLoop, RunningDepId, ASId,
12231219
ShouldCheckWrap, /*Assume=*/true)) {
@@ -1289,12 +1285,11 @@ void AccessAnalysis::processMemAccesses() {
12891285
LLVM_DEBUG(dbgs() << " AST: "; AST.dump());
12901286
LLVM_DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
12911287
LLVM_DEBUG({
1292-
for (auto A : Accesses)
1293-
dbgs() << "\t" << *A.first.getPointer() << " ("
1294-
<< (A.first.getInt()
1295-
? "write"
1296-
: (ReadOnlyPtr.count(A.first.getPointer()) ? "read-only"
1297-
: "read"))
1288+
for (const auto &[A, _] : Accesses)
1289+
dbgs() << "\t" << *A.getPointer() << " ("
1290+
<< (A.getInt() ? "write"
1291+
: (ReadOnlyPtr.count(A.getPointer()) ? "read-only"
1292+
: "read"))
12981293
<< ")\n";
12991294
});
13001295

@@ -1323,16 +1318,16 @@ void AccessAnalysis::processMemAccesses() {
13231318
bool UseDeferred = SetIteration > 0;
13241319
PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
13251320

1326-
for (const Value *Ptr_ : ASPointers) {
1327-
Value *Ptr = const_cast<Value *>(Ptr_);
1321+
for (const Value *ConstPtr : ASPointers) {
1322+
Value *Ptr = const_cast<Value *>(ConstPtr);
13281323

13291324
// For a single memory access in AliasSetTracker, Accesses may contain
13301325
// both read and write, and they both need to be handled for CheckDeps.
1331-
for (const auto &AC : S) {
1332-
if (AC.first.getPointer() != Ptr)
1326+
for (const auto &[AC, _] : S) {
1327+
if (AC.getPointer() != Ptr)
13331328
continue;
13341329

1335-
bool IsWrite = AC.first.getInt();
1330+
bool IsWrite = AC.getInt();
13361331

13371332
// If we're using the deferred access set, then it contains only
13381333
// reads.
@@ -1859,10 +1854,7 @@ static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
18591854
// (If so, then we have proven (**) because |Dist| >= -1*Dist)
18601855
const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
18611856
Minus = SE.getMinusSCEV(NegDist, CastedProduct);
1862-
if (SE.isKnownPositive(Minus))
1863-
return true;
1864-
1865-
return false;
1857+
return SE.isKnownPositive(Minus);
18661858
}
18671859

18681860
/// Check the dependence for two accesses with the same stride \p Stride.
@@ -2050,7 +2042,7 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20502042
if (isa<SCEVCouldNotCompute>(Dist)) {
20512043
// TODO: Relax requirement that there is a common stride to retry with
20522044
// non-constant distance dependencies.
2053-
FoundNonConstantDistanceDependence |= !!CommonStride;
2045+
FoundNonConstantDistanceDependence |= CommonStride.has_value();
20542046
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20552047
return Dependence::Unknown;
20562048
}
@@ -2093,11 +2085,10 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20932085
if (HasSameSize) {
20942086
// Write to the same location with the same size.
20952087
return Dependence::Forward;
2096-
} else {
2097-
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2098-
"different type sizes\n");
2099-
return Dependence::Unknown;
21002088
}
2089+
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2090+
"different type sizes\n");
2091+
return Dependence::Unknown;
21012092
}
21022093

21032094
bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
@@ -2343,7 +2334,7 @@ bool MemoryDepChecker::areDepsSafe(
23432334
}
23442335
++OI;
23452336
}
2346-
AI++;
2337+
++AI;
23472338
}
23482339
}
23492340

@@ -2352,8 +2343,8 @@ bool MemoryDepChecker::areDepsSafe(
23522343
}
23532344

23542345
SmallVector<Instruction *, 4>
2355-
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
2356-
MemAccessInfo Access(Ptr, isWrite);
2346+
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool IsWrite) const {
2347+
MemAccessInfo Access(Ptr, IsWrite);
23572348
auto &IndexVector = Accesses.find(Access)->second;
23582349

23592350
SmallVector<Instruction *, 4> Insts;
@@ -2729,13 +2720,14 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
27292720
}
27302721

27312722
void LoopAccessInfo::emitUnsafeDependenceRemark() {
2732-
auto Deps = getDepChecker().getDependences();
2723+
const auto *Deps = getDepChecker().getDependences();
27332724
if (!Deps)
27342725
return;
2735-
auto Found = llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2736-
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2737-
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2738-
});
2726+
const auto *Found =
2727+
llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2728+
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2729+
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2730+
});
27392731
if (Found == Deps->end())
27402732
return;
27412733
MemoryDepChecker::Dependence Dep = *Found;
@@ -2874,9 +2866,9 @@ static Value *stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
28742866

28752867
// Check that all of the gep indices are uniform except for our induction
28762868
// operand.
2877-
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
2878-
if (i != InductionOperand &&
2879-
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
2869+
for (unsigned I = 0, E = GEP->getNumOperands(); I != E; ++I)
2870+
if (I != InductionOperand &&
2871+
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(I)), Lp))
28802872
return Ptr;
28812873
return GEP->getOperand(InductionOperand);
28822874
}
@@ -3072,9 +3064,8 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30723064
DepChecker =
30733065
std::make_unique<MemoryDepChecker>(*PSE, L, MaxTargetVectorWidthInBits);
30743066
PtrRtChecking = std::make_unique<RuntimePointerChecking>(*DepChecker, SE);
3075-
if (canAnalyzeLoop()) {
3067+
if (canAnalyzeLoop())
30763068
analyzeLoop(AA, LI, TLI, DT);
3077-
}
30783069
}
30793070

30803071
void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
@@ -3126,13 +3117,13 @@ void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
31263117
}
31273118

31283119
const LoopAccessInfo &LoopAccessInfoManager::getInfo(Loop &L) {
3129-
auto I = LoopAccessInfoMap.insert({&L, nullptr});
3120+
auto [It, Inserted] = LoopAccessInfoMap.insert({&L, nullptr});
31303121

3131-
if (I.second)
3132-
I.first->second =
3122+
if (Inserted)
3123+
It->second =
31333124
std::make_unique<LoopAccessInfo>(&L, &SE, TTI, TLI, &AA, &DT, &LI);
31343125

3135-
return *I.first->second;
3126+
return *It->second;
31363127
}
31373128

31383129
bool LoopAccessInfoManager::invalidate(

0 commit comments

Comments
 (0)