Skip to content

Commit 6296dd2

Browse files
authored
[LoopIdiom] Use m_scev_AffineAddRec with Loop matcher (NFC) (#141660)
1 parent 5f39be5 commit 6296dd2

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
455455
// random store we can't handle.
456456
const SCEV *StoreEv = SE->getSCEV(StorePtr);
457457
const SCEVConstant *Stride;
458-
if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride))) ||
459-
cast<SCEVAddRecExpr>(StoreEv)->getLoop() != CurLoop)
458+
if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride),
459+
m_SpecificLoop(CurLoop))))
460460
return LegalStoreKind::None;
461461

462462
// See if the store can be turned into a memset.
@@ -512,9 +512,8 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
512512
const SCEV *LoadEv = SE->getSCEV(LI->getPointerOperand());
513513

514514
// The store and load must share the same stride.
515-
if (!match(LoadEv,
516-
m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride))) ||
517-
cast<SCEVAddRecExpr>(LoadEv)->getLoop() != CurLoop)
515+
if (!match(LoadEv, m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride),
516+
m_SpecificLoop(CurLoop))))
518517
return LegalStoreKind::None;
519518

520519
// Success. This store can be converted into a memcpy.
@@ -787,25 +786,22 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
787786
// See if the load and store pointer expressions are AddRec like {base,+,1} on
788787
// the current loop, which indicates a strided load and store. If we have
789788
// something else, it's a random load or store we can't handle.
790-
const SCEVAddRecExpr *StoreEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Dest));
791-
if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
792-
return false;
793-
const SCEVAddRecExpr *LoadEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Source));
794-
if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
789+
const SCEV *StoreEv = SE->getSCEV(Dest);
790+
const SCEV *LoadEv = SE->getSCEV(Source);
791+
const APInt *StoreStrideValue, *LoadStrideValue;
792+
if (!match(StoreEv,
793+
m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(StoreStrideValue),
794+
m_SpecificLoop(CurLoop))) ||
795+
!match(LoadEv,
796+
m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(LoadStrideValue),
797+
m_SpecificLoop(CurLoop))))
795798
return false;
796799

797800
// Reject memcpys that are so large that they overflow an unsigned.
798801
uint64_t SizeInBytes = cast<ConstantInt>(MCI->getLength())->getZExtValue();
799802
if ((SizeInBytes >> 32) != 0)
800803
return false;
801804

802-
// Check if the stride matches the size of the memcpy. If so, then we know
803-
// that every byte is touched in the loop.
804-
const APInt *StoreStrideValue, *LoadStrideValue;
805-
if (!match(StoreEv->getOperand(1), m_scev_APInt(StoreStrideValue)) ||
806-
!match(LoadEv->getOperand(1), m_scev_APInt(LoadStrideValue)))
807-
return false;
808-
809805
// Huge stride value - give up
810806
if (StoreStrideValue->getBitWidth() > 64 ||
811807
LoadStrideValue->getBitWidth() > 64)
@@ -830,8 +826,8 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
830826

831827
return processLoopStoreOfLoopLoad(
832828
Dest, Source, SE->getConstant(Dest->getType(), SizeInBytes),
833-
MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI, StoreEv, LoadEv,
834-
BECount);
829+
MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI,
830+
cast<SCEVAddRecExpr>(StoreEv), cast<SCEVAddRecExpr>(LoadEv), BECount);
835831
}
836832

837833
/// processLoopMemSet - See if this memset can be promoted to a large memset.
@@ -852,12 +848,11 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
852848
// random store we can't handle.
853849
const SCEV *Ev = SE->getSCEV(Pointer);
854850
const SCEV *PointerStrideSCEV;
855-
if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV)))) {
851+
if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV),
852+
m_SpecificLoop(CurLoop)))) {
856853
LLVM_DEBUG(dbgs() << " Pointer is not affine, abort\n");
857854
return false;
858855
}
859-
if (cast<SCEVAddRecExpr>(Ev)->getLoop() != CurLoop)
860-
return false;
861856

862857
const SCEV *MemsetSizeSCEV = SE->getSCEV(MSI->getLength());
863858

0 commit comments

Comments
 (0)