Skip to content

Commit f5026c3

Browse files
committed
[LAA] refactor program logic (NFC)
Implement NFC improvements spotted during a cursory reading of LoopAccessAnalysis.
1 parent 7efafb0 commit f5026c3

File tree

1 file changed

+47
-58
lines changed

1 file changed

+47
-58
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ void RuntimePointerChecking::generateChecks(
389389

390390
bool RuntimePointerChecking::needsChecking(
391391
const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const {
392-
for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I)
393-
for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J)
394-
if (needsChecking(M.Members[I], N.Members[J]))
392+
for (auto &I : M.Members)
393+
for (auto &J : N.Members)
394+
if (needsChecking(I, J))
395395
return true;
396396
return false;
397397
}
@@ -405,9 +405,7 @@ static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
405405

406406
if (!C)
407407
return nullptr;
408-
if (C->getValue()->isNegative())
409-
return J;
410-
return I;
408+
return C->getValue()->isNegative() ? J : I;
411409
}
412410

413411
bool RuntimeCheckingPtrGroup::addPointer(unsigned Index,
@@ -505,8 +503,8 @@ void RuntimePointerChecking::groupChecks(
505503

506504
DenseMap<Value *, SmallVector<unsigned>> PositionMap;
507505
for (unsigned Index = 0; Index < Pointers.size(); ++Index) {
508-
auto Iter = PositionMap.insert({Pointers[Index].PointerValue, {}});
509-
Iter.first->second.push_back(Index);
506+
auto [It, _] = PositionMap.insert({Pointers[Index].PointerValue, {}});
507+
It->second.push_back(Index);
510508
}
511509

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

611609
OS.indent(Depth) << "Check " << N++ << ":\n";
612610

613-
OS.indent(Depth + 2) << "Comparing group (" << Check.first << "):\n";
611+
OS.indent(Depth + 2) << "Comparing group (" << Check1 << "):\n";
614612
for (unsigned K = 0; K < First.size(); ++K)
615613
OS.indent(Depth + 2) << *Pointers[First[K]].PointerValue << "\n";
616614

617-
OS.indent(Depth + 2) << "Against group (" << Check.second << "):\n";
615+
OS.indent(Depth + 2) << "Against group (" << Check2 << "):\n";
618616
for (unsigned K = 0; K < Second.size(); ++K)
619617
OS.indent(Depth + 2) << *Pointers[Second[K]].PointerValue << "\n";
620618
}
@@ -1155,8 +1153,8 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
11551153
// First, count how many write and read accesses are in the alias set. Also
11561154
// collect MemAccessInfos for later.
11571155
SmallVector<MemAccessInfo, 4> AccessInfos;
1158-
for (const Value *Ptr_ : ASPointers) {
1159-
Value *Ptr = const_cast<Value *>(Ptr_);
1156+
for (const Value *ConstPtr : ASPointers) {
1157+
Value *Ptr = const_cast<Value *>(ConstPtr);
11601158
bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
11611159
if (IsWrite)
11621160
++NumWritePtrChecks;
@@ -1212,9 +1210,7 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
12121210
// We know that we need these checks, so we can now be more aggressive
12131211
// and add further checks if required (overflow checks).
12141212
CanDoAliasSetRT = true;
1215-
for (auto Retry : Retries) {
1216-
MemAccessInfo Access = Retry.first;
1217-
Type *AccessTy = Retry.second;
1213+
for (const auto &[Access, AccessTy] : Retries) {
12181214
if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
12191215
DepSetId, TheLoop, RunningDepId, ASId,
12201216
ShouldCheckWrap, /*Assume=*/true)) {
@@ -1286,12 +1282,11 @@ void AccessAnalysis::processMemAccesses() {
12861282
LLVM_DEBUG(dbgs() << " AST: "; AST.dump());
12871283
LLVM_DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
12881284
LLVM_DEBUG({
1289-
for (auto A : Accesses)
1290-
dbgs() << "\t" << *A.first.getPointer() << " ("
1291-
<< (A.first.getInt()
1292-
? "write"
1293-
: (ReadOnlyPtr.count(A.first.getPointer()) ? "read-only"
1294-
: "read"))
1285+
for (const auto &[A, _] : Accesses)
1286+
dbgs() << "\t" << *A.getPointer() << " ("
1287+
<< (A.getInt() ? "write"
1288+
: (ReadOnlyPtr.count(A.getPointer()) ? "read-only"
1289+
: "read"))
12951290
<< ")\n";
12961291
});
12971292

@@ -1320,16 +1315,16 @@ void AccessAnalysis::processMemAccesses() {
13201315
bool UseDeferred = SetIteration > 0;
13211316
PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
13221317

1323-
for (const Value *Ptr_ : ASPointers) {
1324-
Value *Ptr = const_cast<Value *>(Ptr_);
1318+
for (const Value *ConstPtr : ASPointers) {
1319+
Value *Ptr = const_cast<Value *>(ConstPtr);
13251320

13261321
// For a single memory access in AliasSetTracker, Accesses may contain
13271322
// both read and write, and they both need to be handled for CheckDeps.
1328-
for (const auto &AC : S) {
1329-
if (AC.first.getPointer() != Ptr)
1323+
for (const auto &[AC, _] : S) {
1324+
if (AC.getPointer() != Ptr)
13301325
continue;
13311326

1332-
bool IsWrite = AC.first.getInt();
1327+
bool IsWrite = AC.getInt();
13331328

13341329
// If we're using the deferred access set, then it contains only
13351330
// reads.
@@ -1856,10 +1851,7 @@ static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
18561851
// (If so, then we have proven (**) because |Dist| >= -1*Dist)
18571852
const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
18581853
Minus = SE.getMinusSCEV(NegDist, CastedProduct);
1859-
if (SE.isKnownPositive(Minus))
1860-
return true;
1861-
1862-
return false;
1854+
return SE.isKnownPositive(Minus);
18631855
}
18641856

18651857
/// Check the dependence for two accesses with the same stride \p Stride.
@@ -2030,7 +2022,7 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20302022
if (isa<SCEVCouldNotCompute>(Dist)) {
20312023
// TODO: Relax requirement that there is a common stride to retry with
20322024
// non-constant distance dependencies.
2033-
FoundNonConstantDistanceDependence |= !!CommonStride;
2025+
FoundNonConstantDistanceDependence |= CommonStride.has_value();
20342026
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20352027
return Dependence::Unknown;
20362028
}
@@ -2070,14 +2062,12 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20702062
// Negative distances are not plausible dependencies.
20712063
if (SE.isKnownNonPositive(Dist)) {
20722064
if (SE.isKnownNonNegative(Dist)) {
2073-
if (HasSameSize) {
2065+
if (HasSameSize)
20742066
// Write to the same location with the same size.
20752067
return Dependence::Forward;
2076-
} else {
2077-
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2078-
"different type sizes\n");
2079-
return Dependence::Unknown;
2080-
}
2068+
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2069+
"different type sizes\n");
2070+
return Dependence::Unknown;
20812071
}
20822072

20832073
bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
@@ -2323,7 +2313,7 @@ bool MemoryDepChecker::areDepsSafe(
23232313
}
23242314
++OI;
23252315
}
2326-
AI++;
2316+
++AI;
23272317
}
23282318
}
23292319

@@ -2332,8 +2322,8 @@ bool MemoryDepChecker::areDepsSafe(
23322322
}
23332323

23342324
SmallVector<Instruction *, 4>
2335-
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
2336-
MemAccessInfo Access(Ptr, isWrite);
2325+
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool IsWrite) const {
2326+
MemAccessInfo Access(Ptr, IsWrite);
23372327
auto &IndexVector = Accesses.find(Access)->second;
23382328

23392329
SmallVector<Instruction *, 4> Insts;
@@ -2709,13 +2699,14 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
27092699
}
27102700

27112701
void LoopAccessInfo::emitUnsafeDependenceRemark() {
2712-
auto Deps = getDepChecker().getDependences();
2702+
const auto *Deps = getDepChecker().getDependences();
27132703
if (!Deps)
27142704
return;
2715-
auto Found = llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2716-
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2717-
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2718-
});
2705+
const auto *Found =
2706+
llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2707+
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2708+
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2709+
});
27192710
if (Found == Deps->end())
27202711
return;
27212712
MemoryDepChecker::Dependence Dep = *Found;
@@ -2854,9 +2845,9 @@ static Value *stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
28542845

28552846
// Check that all of the gep indices are uniform except for our induction
28562847
// operand.
2857-
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
2858-
if (i != InductionOperand &&
2859-
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
2848+
for (unsigned I = 0, E = GEP->getNumOperands(); I != E; ++I)
2849+
if (I != InductionOperand &&
2850+
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(I)), Lp))
28602851
return Ptr;
28612852
return GEP->getOperand(InductionOperand);
28622853
}
@@ -3038,11 +3029,10 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30383029
if (TTI) {
30393030
TypeSize FixedWidth =
30403031
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
3041-
if (FixedWidth.isNonZero()) {
3032+
if (FixedWidth.isNonZero())
30423033
// Scale the vector width by 2 as rough estimate to also consider
30433034
// interleaving.
30443035
MaxTargetVectorWidthInBits = FixedWidth.getFixedValue() * 2;
3045-
}
30463036

30473037
TypeSize ScalableWidth =
30483038
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_ScalableVector);
@@ -3052,9 +3042,8 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30523042
DepChecker =
30533043
std::make_unique<MemoryDepChecker>(*PSE, L, MaxTargetVectorWidthInBits);
30543044
PtrRtChecking = std::make_unique<RuntimePointerChecking>(*DepChecker, SE);
3055-
if (canAnalyzeLoop()) {
3045+
if (canAnalyzeLoop())
30563046
analyzeLoop(AA, LI, TLI, DT);
3057-
}
30583047
}
30593048

30603049
void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
@@ -3106,13 +3095,13 @@ void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
31063095
}
31073096

31083097
const LoopAccessInfo &LoopAccessInfoManager::getInfo(Loop &L) {
3109-
auto I = LoopAccessInfoMap.insert({&L, nullptr});
3098+
auto [It, Inserted] = LoopAccessInfoMap.insert({&L, nullptr});
31103099

3111-
if (I.second)
3112-
I.first->second =
3100+
if (Inserted)
3101+
It->second =
31133102
std::make_unique<LoopAccessInfo>(&L, &SE, TTI, TLI, &AA, &DT, &LI);
31143103

3115-
return *I.first->second;
3104+
return *It->second;
31163105
}
31173106

31183107
bool LoopAccessInfoManager::invalidate(

0 commit comments

Comments
 (0)