Skip to content

Commit c274ab8

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

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ void RuntimePointerChecking::generateChecks(
394394

395395
bool RuntimePointerChecking::needsChecking(
396396
const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const {
397-
for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I)
398-
for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J)
399-
if (needsChecking(M.Members[I], N.Members[J]))
397+
for (auto &I : M.Members)
398+
for (auto &J : N.Members)
399+
if (needsChecking(I, J))
400400
return true;
401401
return false;
402402
}
@@ -410,9 +410,7 @@ static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
410410

411411
if (!C)
412412
return nullptr;
413-
if (C->getValue()->isNegative())
414-
return J;
415-
return I;
413+
return C->getValue()->isNegative() ? J : I;
416414
}
417415

418416
bool RuntimeCheckingPtrGroup::addPointer(unsigned Index,
@@ -510,8 +508,8 @@ void RuntimePointerChecking::groupChecks(
510508

511509
DenseMap<Value *, SmallVector<unsigned>> PositionMap;
512510
for (unsigned Index = 0; Index < Pointers.size(); ++Index) {
513-
auto Iter = PositionMap.insert({Pointers[Index].PointerValue, {}});
514-
Iter.first->second.push_back(Index);
511+
auto [It, _] = PositionMap.insert({Pointers[Index].PointerValue, {}});
512+
It->second.push_back(Index);
515513
}
516514

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

616614
OS.indent(Depth) << "Check " << N++ << ":\n";
617615

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

622-
OS.indent(Depth + 2) << "Against group (" << Check.second << "):\n";
620+
OS.indent(Depth + 2) << "Against group (" << Check2 << "):\n";
623621
for (unsigned K = 0; K < Second.size(); ++K)
624622
OS.indent(Depth + 2) << *Pointers[Second[K]].PointerValue << "\n";
625623
}
@@ -1160,8 +1158,8 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
11601158
// First, count how many write and read accesses are in the alias set. Also
11611159
// collect MemAccessInfos for later.
11621160
SmallVector<MemAccessInfo, 4> AccessInfos;
1163-
for (const Value *Ptr_ : ASPointers) {
1164-
Value *Ptr = const_cast<Value *>(Ptr_);
1161+
for (const Value *ConstPtr : ASPointers) {
1162+
Value *Ptr = const_cast<Value *>(ConstPtr);
11651163
bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
11661164
if (IsWrite)
11671165
++NumWritePtrChecks;
@@ -1217,9 +1215,7 @@ bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
12171215
// We know that we need these checks, so we can now be more aggressive
12181216
// and add further checks if required (overflow checks).
12191217
CanDoAliasSetRT = true;
1220-
for (auto Retry : Retries) {
1221-
MemAccessInfo Access = Retry.first;
1222-
Type *AccessTy = Retry.second;
1218+
for (const auto &[Access, AccessTy] : Retries) {
12231219
if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
12241220
DepSetId, TheLoop, RunningDepId, ASId,
12251221
ShouldCheckWrap, /*Assume=*/true)) {
@@ -1291,12 +1287,11 @@ void AccessAnalysis::processMemAccesses() {
12911287
LLVM_DEBUG(dbgs() << " AST: "; AST.dump());
12921288
LLVM_DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
12931289
LLVM_DEBUG({
1294-
for (auto A : Accesses)
1295-
dbgs() << "\t" << *A.first.getPointer() << " ("
1296-
<< (A.first.getInt()
1297-
? "write"
1298-
: (ReadOnlyPtr.count(A.first.getPointer()) ? "read-only"
1299-
: "read"))
1290+
for (const auto &[A, _] : Accesses)
1291+
dbgs() << "\t" << *A.getPointer() << " ("
1292+
<< (A.getInt() ? "write"
1293+
: (ReadOnlyPtr.count(A.getPointer()) ? "read-only"
1294+
: "read"))
13001295
<< ")\n";
13011296
});
13021297

@@ -1325,16 +1320,16 @@ void AccessAnalysis::processMemAccesses() {
13251320
bool UseDeferred = SetIteration > 0;
13261321
PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
13271322

1328-
for (const Value *Ptr_ : ASPointers) {
1329-
Value *Ptr = const_cast<Value *>(Ptr_);
1323+
for (const Value *ConstPtr : ASPointers) {
1324+
Value *Ptr = const_cast<Value *>(ConstPtr);
13301325

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

1337-
bool IsWrite = AC.first.getInt();
1332+
bool IsWrite = AC.getInt();
13381333

13391334
// If we're using the deferred access set, then it contains only
13401335
// reads.
@@ -1866,10 +1861,7 @@ static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
18661861
// (If so, then we have proven (**) because |Dist| >= -1*Dist)
18671862
const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
18681863
Minus = SE.getMinusSCEV(NegDist, CastedProduct);
1869-
if (SE.isKnownPositive(Minus))
1870-
return true;
1871-
1872-
return false;
1864+
return SE.isKnownPositive(Minus);
18731865
}
18741866

18751867
/// Check the dependence for two accesses with the same stride \p Stride.
@@ -2043,7 +2035,7 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20432035
if (isa<SCEVCouldNotCompute>(Dist)) {
20442036
// TODO: Relax requirement that there is a common stride to retry with
20452037
// non-constant distance dependencies.
2046-
FoundNonConstantDistanceDependence |= !!CommonStride;
2038+
FoundNonConstantDistanceDependence |= CommonStride.has_value();
20472039
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20482040
return Dependence::Unknown;
20492041
}
@@ -2082,14 +2074,12 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20822074
// Negative distances are not plausible dependencies.
20832075
if (SE.isKnownNonPositive(Dist)) {
20842076
if (SE.isKnownNonNegative(Dist)) {
2085-
if (HasSameSize) {
2077+
if (HasSameSize)
20862078
// Write to the same location with the same size.
20872079
return Dependence::Forward;
2088-
} else {
2089-
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2090-
"different type sizes\n");
2091-
return Dependence::Unknown;
2092-
}
2080+
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2081+
"different type sizes\n");
2082+
return Dependence::Unknown;
20932083
}
20942084

20952085
bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
@@ -2335,7 +2325,7 @@ bool MemoryDepChecker::areDepsSafe(
23352325
}
23362326
++OI;
23372327
}
2338-
AI++;
2328+
++AI;
23392329
}
23402330
}
23412331

@@ -2344,8 +2334,8 @@ bool MemoryDepChecker::areDepsSafe(
23442334
}
23452335

23462336
SmallVector<Instruction *, 4>
2347-
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
2348-
MemAccessInfo Access(Ptr, isWrite);
2337+
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool IsWrite) const {
2338+
MemAccessInfo Access(Ptr, IsWrite);
23492339
auto &IndexVector = Accesses.find(Access)->second;
23502340

23512341
SmallVector<Instruction *, 4> Insts;
@@ -2656,7 +2646,7 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
26562646
SymbolicStrides, UncomputablePtr, false);
26572647
if (!CanDoRTIfNeeded) {
26582648
auto *I = dyn_cast_or_null<Instruction>(UncomputablePtr);
2659-
recordAnalysis("CantIdentifyArrayBounds", I)
2649+
recordAnalysis("CantIdentifyArrayBounds", I)
26602650
<< "cannot identify array bounds";
26612651
LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
26622652
<< "the array bounds.\n");
@@ -2721,13 +2711,14 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
27212711
}
27222712

27232713
void LoopAccessInfo::emitUnsafeDependenceRemark() {
2724-
auto Deps = getDepChecker().getDependences();
2714+
const auto *Deps = getDepChecker().getDependences();
27252715
if (!Deps)
27262716
return;
2727-
auto Found = llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2728-
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2729-
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2730-
});
2717+
const auto *Found =
2718+
llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
2719+
return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2720+
MemoryDepChecker::VectorizationSafetyStatus::Safe;
2721+
});
27312722
if (Found == Deps->end())
27322723
return;
27332724
MemoryDepChecker::Dependence Dep = *Found;
@@ -2866,9 +2857,9 @@ static Value *stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
28662857

28672858
// Check that all of the gep indices are uniform except for our induction
28682859
// operand.
2869-
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
2870-
if (i != InductionOperand &&
2871-
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
2860+
for (unsigned I = 0, E = GEP->getNumOperands(); I != E; ++I)
2861+
if (I != InductionOperand &&
2862+
!SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(I)), Lp))
28722863
return Ptr;
28732864
return GEP->getOperand(InductionOperand);
28742865
}
@@ -3050,11 +3041,10 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30503041
if (TTI) {
30513042
TypeSize FixedWidth =
30523043
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
3053-
if (FixedWidth.isNonZero()) {
3044+
if (FixedWidth.isNonZero())
30543045
// Scale the vector width by 2 as rough estimate to also consider
30553046
// interleaving.
30563047
MaxTargetVectorWidthInBits = FixedWidth.getFixedValue() * 2;
3057-
}
30583048

30593049
TypeSize ScalableWidth =
30603050
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_ScalableVector);
@@ -3064,9 +3054,8 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30643054
DepChecker =
30653055
std::make_unique<MemoryDepChecker>(*PSE, L, MaxTargetVectorWidthInBits);
30663056
PtrRtChecking = std::make_unique<RuntimePointerChecking>(*DepChecker, SE);
3067-
if (canAnalyzeLoop()) {
3057+
if (canAnalyzeLoop())
30683058
analyzeLoop(AA, LI, TLI, DT);
3069-
}
30703059
}
30713060

30723061
void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
@@ -3118,13 +3107,13 @@ void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
31183107
}
31193108

31203109
const LoopAccessInfo &LoopAccessInfoManager::getInfo(Loop &L) {
3121-
auto I = LoopAccessInfoMap.insert({&L, nullptr});
3110+
auto [It, Inserted] = LoopAccessInfoMap.insert({&L, nullptr});
31223111

3123-
if (I.second)
3124-
I.first->second =
3112+
if (Inserted)
3113+
It->second =
31253114
std::make_unique<LoopAccessInfo>(&L, &SE, TTI, TLI, &AA, &DT, &LI);
31263115

3127-
return *I.first->second;
3116+
return *It->second;
31283117
}
31293118

31303119
bool LoopAccessInfoManager::invalidate(

0 commit comments

Comments
 (0)