Skip to content

Commit 9b9a8f9

Browse files
committed
[LoopIdiom] Use m_scev_AffineAddRec
1 parent 778a0ae commit 9b9a8f9

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,10 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
453453
// See if the pointer expression is an AddRec like {base,+,1} on the current
454454
// loop, which indicates a strided store. If we have something else, it's a
455455
// random store we can't handle.
456-
const SCEVAddRecExpr *StoreEv =
457-
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
458-
if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
459-
return LegalStoreKind::None;
460-
461-
// Check to see if we have a constant stride.
462-
if (!isa<SCEVConstant>(StoreEv->getOperand(1)))
456+
const SCEV *StoreEv = SE->getSCEV(StorePtr);
457+
const SCEVConstant *Stride;
458+
if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride))) ||
459+
cast<SCEVAddRecExpr>(StoreEv)->getLoop() != CurLoop)
463460
return LegalStoreKind::None;
464461

465462
// See if the store can be turned into a memset.
@@ -494,9 +491,9 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
494491
if (HasMemcpy && !DisableLIRP::Memcpy) {
495492
// Check to see if the stride matches the size of the store. If so, then we
496493
// know that every byte is touched in the loop.
497-
APInt Stride = getStoreStride(StoreEv);
498494
unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
499-
if (StoreSize != Stride && StoreSize != -Stride)
495+
APInt StrideAP = Stride->getAPInt();
496+
if (StoreSize != StrideAP && StoreSize != -StrideAP)
500497
return LegalStoreKind::None;
501498

502499
// The store must be feeding a non-volatile load.
@@ -512,13 +509,12 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
512509
// See if the pointer expression is an AddRec like {base,+,1} on the current
513510
// loop, which indicates a strided load. If we have something else, it's a
514511
// random load we can't handle.
515-
const SCEVAddRecExpr *LoadEv =
516-
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
517-
if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
518-
return LegalStoreKind::None;
512+
const SCEV *LoadEv = SE->getSCEV(LI->getPointerOperand());
519513

520514
// The store and load must share the same stride.
521-
if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
515+
if (!match(LoadEv,
516+
m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride))) ||
517+
cast<SCEVAddRecExpr>(LoadEv)->getLoop() != CurLoop)
522518
return LegalStoreKind::None;
523519

524520
// Success. This store can be converted into a memcpy.
@@ -854,15 +850,15 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
854850
// See if the pointer expression is an AddRec like {base,+,1} on the current
855851
// loop, which indicates a strided store. If we have something else, it's a
856852
// random store we can't handle.
857-
const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
858-
if (!Ev || Ev->getLoop() != CurLoop)
859-
return false;
860-
if (!Ev->isAffine()) {
853+
const SCEV *Ev = SE->getSCEV(Pointer);
854+
const SCEV *PointerStrideSCEV;
855+
if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV)))) {
861856
LLVM_DEBUG(dbgs() << " Pointer is not affine, abort\n");
862857
return false;
863858
}
859+
if (cast<SCEVAddRecExpr>(Ev)->getLoop() != CurLoop)
860+
return false;
864861

865-
const SCEV *PointerStrideSCEV = Ev->getOperand(1);
866862
const SCEV *MemsetSizeSCEV = SE->getSCEV(MSI->getLength());
867863
if (!PointerStrideSCEV || !MemsetSizeSCEV)
868864
return false;
@@ -877,7 +873,7 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
877873
LLVM_DEBUG(dbgs() << " memset size is constant\n");
878874
uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
879875
const APInt *Stride;
880-
if (!match(Ev->getOperand(1), m_scev_APInt(Stride)))
876+
if (!match(PointerStrideSCEV, m_scev_APInt(Stride)))
881877
return false;
882878

883879
if (SizeInBytes != *Stride && SizeInBytes != -*Stride)
@@ -940,8 +936,9 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
940936
SmallPtrSet<Instruction *, 1> MSIs;
941937
MSIs.insert(MSI);
942938
return processLoopStridedStore(Pointer, SE->getSCEV(MSI->getLength()),
943-
MSI->getDestAlign(), SplatValue, MSI, MSIs, Ev,
944-
BECount, IsNegStride, /*IsLoopMemset=*/true);
939+
MSI->getDestAlign(), SplatValue, MSI, MSIs,
940+
cast<SCEVAddRecExpr>(Ev), BECount, IsNegStride,
941+
/*IsLoopMemset=*/true);
945942
}
946943

947944
/// mayLoopAccessLocation - Return true if the specified loop might access the
@@ -1575,18 +1572,14 @@ class StrlenVerifier {
15751572
// See if the pointer expression is an AddRec with constant step a of form
15761573
// ({n,+,a}) where a is the width of the char type.
15771574
Value *IncPtr = LoopLoad->getPointerOperand();
1578-
const SCEVAddRecExpr *LoadEv =
1579-
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(IncPtr));
1580-
if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
1575+
const SCEV *LoadEv = SE->getSCEV(IncPtr);
1576+
const APInt *Step;
1577+
if (!match(LoadEv,
1578+
m_scev_AffineAddRec(m_SCEV(LoadBaseEv), m_scev_APInt(Step))))
15811579
return false;
1582-
LoadBaseEv = LoadEv->getStart();
15831580

15841581
LLVM_DEBUG(dbgs() << "pointer load scev: " << *LoadEv << "\n");
15851582

1586-
const APInt *Step;
1587-
if (!match(LoadEv->getStepRecurrence(*SE), m_scev_APInt(Step)))
1588-
return false;
1589-
15901583
unsigned StepSize = Step->getZExtValue();
15911584

15921585
// Verify that StepSize is consistent with platform char width.

0 commit comments

Comments
 (0)