Skip to content

Commit 32d34c4

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

File tree

2 files changed

+42
-69
lines changed

2 files changed

+42
-69
lines changed

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ class RuntimePointerChecking {
540540
/// Try to create add a new (pointer-difference, access size) pair to
541541
/// DiffCheck for checking groups \p CGI and \p CGJ. If pointer-difference
542542
/// checks cannot be used for the groups, set CanUseDiffCheck to false.
543-
void tryToCreateDiffCheck(const RuntimeCheckingPtrGroup &CGI,
543+
bool tryToCreateDiffCheck(const RuntimeCheckingPtrGroup &CGI,
544544
const RuntimeCheckingPtrGroup &CGJ);
545545

546546
MemoryDepChecker &DC;

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -250,62 +250,51 @@ void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr,
250250
NeedsFreeze);
251251
}
252252

253-
void RuntimePointerChecking::tryToCreateDiffCheck(
253+
bool RuntimePointerChecking::tryToCreateDiffCheck(
254254
const RuntimeCheckingPtrGroup &CGI, const RuntimeCheckingPtrGroup &CGJ) {
255-
if (!CanUseDiffCheck)
256-
return;
257-
258255
// If either group contains multiple different pointers, bail out.
259256
// TODO: Support multiple pointers by using the minimum or maximum pointer,
260257
// depending on src & sink.
261-
if (CGI.Members.size() != 1 || CGJ.Members.size() != 1) {
262-
CanUseDiffCheck = false;
263-
return;
264-
}
258+
if (CGI.Members.size() != 1 || CGJ.Members.size() != 1)
259+
return false;
265260

266261
PointerInfo *Src = &Pointers[CGI.Members[0]];
267262
PointerInfo *Sink = &Pointers[CGJ.Members[0]];
268263

269264
// If either pointer is read and written, multiple checks may be needed. Bail
270265
// out.
271266
if (!DC.getOrderForAccess(Src->PointerValue, !Src->IsWritePtr).empty() ||
272-
!DC.getOrderForAccess(Sink->PointerValue, !Sink->IsWritePtr).empty()) {
273-
CanUseDiffCheck = false;
274-
return;
275-
}
267+
!DC.getOrderForAccess(Sink->PointerValue, !Sink->IsWritePtr).empty())
268+
return false;
276269

277270
ArrayRef<unsigned> AccSrc =
278271
DC.getOrderForAccess(Src->PointerValue, Src->IsWritePtr);
279272
ArrayRef<unsigned> AccSink =
280273
DC.getOrderForAccess(Sink->PointerValue, Sink->IsWritePtr);
281274
// If either pointer is accessed multiple times, there may not be a clear
282275
// src/sink relation. Bail out for now.
283-
if (AccSrc.size() != 1 || AccSink.size() != 1) {
284-
CanUseDiffCheck = false;
285-
return;
286-
}
276+
if (AccSrc.size() != 1 || AccSink.size() != 1)
277+
return false;
278+
287279
// If the sink is accessed before src, swap src/sink.
288280
if (AccSink[0] < AccSrc[0])
289281
std::swap(Src, Sink);
290282

291283
auto *SrcAR = dyn_cast<SCEVAddRecExpr>(Src->Expr);
292284
auto *SinkAR = dyn_cast<SCEVAddRecExpr>(Sink->Expr);
293285
if (!SrcAR || !SinkAR || SrcAR->getLoop() != DC.getInnermostLoop() ||
294-
SinkAR->getLoop() != DC.getInnermostLoop()) {
295-
CanUseDiffCheck = false;
296-
return;
297-
}
286+
SinkAR->getLoop() != DC.getInnermostLoop())
287+
return false;
298288

299289
SmallVector<Instruction *, 4> SrcInsts =
300290
DC.getInstructionsForAccess(Src->PointerValue, Src->IsWritePtr);
301291
SmallVector<Instruction *, 4> SinkInsts =
302292
DC.getInstructionsForAccess(Sink->PointerValue, Sink->IsWritePtr);
303293
Type *SrcTy = getLoadStoreType(SrcInsts[0]);
304294
Type *DstTy = getLoadStoreType(SinkInsts[0]);
305-
if (isa<ScalableVectorType>(SrcTy) || isa<ScalableVectorType>(DstTy)) {
306-
CanUseDiffCheck = false;
307-
return;
308-
}
295+
if (isa<ScalableVectorType>(SrcTy) || isa<ScalableVectorType>(DstTy))
296+
return false;
297+
309298
const DataLayout &DL =
310299
SinkAR->getLoop()->getHeader()->getModule()->getDataLayout();
311300
unsigned AllocSize =
@@ -316,10 +305,8 @@ void RuntimePointerChecking::tryToCreateDiffCheck(
316305
// future.
317306
auto *Step = dyn_cast<SCEVConstant>(SinkAR->getStepRecurrence(*SE));
318307
if (!Step || Step != SrcAR->getStepRecurrence(*SE) ||
319-
Step->getAPInt().abs() != AllocSize) {
320-
CanUseDiffCheck = false;
321-
return;
322-
}
308+
Step->getAPInt().abs() != AllocSize)
309+
return false;
323310

324311
IntegerType *IntTy =
325312
IntegerType::get(Src->PointerValue->getContext(),
@@ -332,10 +319,8 @@ void RuntimePointerChecking::tryToCreateDiffCheck(
332319
const SCEV *SinkStartInt = SE->getPtrToIntExpr(SinkAR->getStart(), IntTy);
333320
const SCEV *SrcStartInt = SE->getPtrToIntExpr(SrcAR->getStart(), IntTy);
334321
if (isa<SCEVCouldNotCompute>(SinkStartInt) ||
335-
isa<SCEVCouldNotCompute>(SrcStartInt)) {
336-
CanUseDiffCheck = false;
337-
return;
338-
}
322+
isa<SCEVCouldNotCompute>(SrcStartInt))
323+
return false;
339324

340325
const Loop *InnerLoop = SrcAR->getLoop();
341326
// If the start values for both Src and Sink also vary according to an outer
@@ -356,8 +341,7 @@ void RuntimePointerChecking::tryToCreateDiffCheck(
356341
SinkStartAR->getStepRecurrence(*SE)) {
357342
LLVM_DEBUG(dbgs() << "LAA: Not creating diff runtime check, since these "
358343
"cannot be hoisted out of the outer loop\n");
359-
CanUseDiffCheck = false;
360-
return;
344+
return false;
361345
}
362346
}
363347

@@ -366,6 +350,7 @@ void RuntimePointerChecking::tryToCreateDiffCheck(
366350
<< "SinkStartInt: " << *SinkStartInt << '\n');
367351
DiffChecks.emplace_back(SrcStartInt, SinkStartInt, AllocSize,
368352
Src->NeedsFreeze || Sink->NeedsFreeze);
353+
return true;
369354
}
370355

371356
SmallVector<RuntimePointerCheck, 4> RuntimePointerChecking::generateChecks() {
@@ -377,7 +362,7 @@ SmallVector<RuntimePointerCheck, 4> RuntimePointerChecking::generateChecks() {
377362
const RuntimeCheckingPtrGroup &CGJ = CheckingGroups[J];
378363

379364
if (needsChecking(CGI, CGJ)) {
380-
tryToCreateDiffCheck(CGI, CGJ);
365+
CanUseDiffCheck = CanUseDiffCheck && tryToCreateDiffCheck(CGI, CGJ);
381366
Checks.push_back(std::make_pair(&CGI, &CGJ));
382367
}
383368
}
@@ -394,9 +379,9 @@ void RuntimePointerChecking::generateChecks(
394379

395380
bool RuntimePointerChecking::needsChecking(
396381
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]))
382+
for (auto &I : M.Members)
383+
for (auto &J : N.Members)
384+
if (needsChecking(I, J))
400385
return true;
401386
return false;
402387
}
@@ -410,9 +395,7 @@ static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
410395

411396
if (!C)
412397
return nullptr;
413-
if (C->getValue()->isNegative())
414-
return J;
415-
return I;
398+
return C->getValue()->isNegative() ? J : I;
416399
}
417400

418401
bool RuntimeCheckingPtrGroup::addPointer(unsigned Index,
@@ -1646,22 +1629,19 @@ bool llvm::sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
16461629

16471630
// Check if the pointer with the same offset is found.
16481631
int64_t Offset = *Diff;
1649-
auto Res = Offsets.emplace(Offset, Cnt);
1650-
if (!Res.second)
1632+
auto [It, IsInserted] = Offsets.emplace(Offset, Cnt);
1633+
if (!IsInserted)
16511634
return false;
16521635
// Consecutive order if the inserted element is the last one.
1653-
IsConsecutive = IsConsecutive && std::next(Res.first) == Offsets.end();
1636+
IsConsecutive = IsConsecutive && std::next(It) == Offsets.end();
16541637
++Cnt;
16551638
}
16561639
SortedIndices.clear();
16571640
if (!IsConsecutive) {
16581641
// Fill SortedIndices array only if it is non-consecutive.
16591642
SortedIndices.resize(VL.size());
1660-
Cnt = 0;
1661-
for (const std::pair<int64_t, int> &Pair : Offsets) {
1662-
SortedIndices[Cnt] = Pair.second;
1663-
++Cnt;
1664-
}
1643+
for (const std::pair<int64_t, int> &Pair : Offsets)
1644+
SortedIndices.push_back(Pair.second);
16651645
}
16661646
return true;
16671647
}
@@ -1866,10 +1846,7 @@ static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
18661846
// (If so, then we have proven (**) because |Dist| >= -1*Dist)
18671847
const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
18681848
Minus = SE.getMinusSCEV(NegDist, CastedProduct);
1869-
if (SE.isKnownPositive(Minus))
1870-
return true;
1871-
1872-
return false;
1849+
return SE.isKnownPositive(Minus);
18731850
}
18741851

18751852
/// Check the dependence for two accesses with the same stride \p Stride.
@@ -2043,7 +2020,7 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20432020
if (isa<SCEVCouldNotCompute>(Dist)) {
20442021
// TODO: Relax requirement that there is a common stride to retry with
20452022
// non-constant distance dependencies.
2046-
FoundNonConstantDistanceDependence |= !!CommonStride;
2023+
FoundNonConstantDistanceDependence |= CommonStride.has_value();
20472024
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20482025
return Dependence::Unknown;
20492026
}
@@ -2082,14 +2059,12 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
20822059
// Negative distances are not plausible dependencies.
20832060
if (SE.isKnownNonPositive(Dist)) {
20842061
if (SE.isKnownNonNegative(Dist)) {
2085-
if (HasSameSize) {
2062+
if (HasSameSize)
20862063
// Write to the same location with the same size.
20872064
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-
}
2065+
LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
2066+
"different type sizes\n");
2067+
return Dependence::Unknown;
20932068
}
20942069

20952070
bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
@@ -2335,7 +2310,7 @@ bool MemoryDepChecker::areDepsSafe(
23352310
}
23362311
++OI;
23372312
}
2338-
AI++;
2313+
++AI;
23392314
}
23402315
}
23412316

@@ -2344,8 +2319,8 @@ bool MemoryDepChecker::areDepsSafe(
23442319
}
23452320

23462321
SmallVector<Instruction *, 4>
2347-
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
2348-
MemAccessInfo Access(Ptr, isWrite);
2322+
MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool IsWrite) const {
2323+
MemAccessInfo Access(Ptr, IsWrite);
23492324
auto &IndexVector = Accesses.find(Access)->second;
23502325

23512326
SmallVector<Instruction *, 4> Insts;
@@ -2656,7 +2631,7 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
26562631
SymbolicStrides, UncomputablePtr, false);
26572632
if (!CanDoRTIfNeeded) {
26582633
auto *I = dyn_cast_or_null<Instruction>(UncomputablePtr);
2659-
recordAnalysis("CantIdentifyArrayBounds", I)
2634+
recordAnalysis("CantIdentifyArrayBounds", I)
26602635
<< "cannot identify array bounds";
26612636
LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
26622637
<< "the array bounds.\n");
@@ -3050,11 +3025,10 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30503025
if (TTI) {
30513026
TypeSize FixedWidth =
30523027
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
3053-
if (FixedWidth.isNonZero()) {
3028+
if (FixedWidth.isNonZero())
30543029
// Scale the vector width by 2 as rough estimate to also consider
30553030
// interleaving.
30563031
MaxTargetVectorWidthInBits = FixedWidth.getFixedValue() * 2;
3057-
}
30583032

30593033
TypeSize ScalableWidth =
30603034
TTI->getRegisterBitWidth(TargetTransformInfo::RGK_ScalableVector);
@@ -3064,9 +3038,8 @@ LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
30643038
DepChecker =
30653039
std::make_unique<MemoryDepChecker>(*PSE, L, MaxTargetVectorWidthInBits);
30663040
PtrRtChecking = std::make_unique<RuntimePointerChecking>(*DepChecker, SE);
3067-
if (canAnalyzeLoop()) {
3041+
if (canAnalyzeLoop())
30683042
analyzeLoop(AA, LI, TLI, DT);
3069-
}
30703043
}
30713044

30723045
void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {

0 commit comments

Comments
 (0)