-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[LoopIdiom] Use m_scev_AffineAddRec with Loop matcher (NFC) #141660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Ramkumar Ramachandra (artagnon) ChangesFull diff: https://github.com/llvm/llvm-project/pull/141660.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index 0d5e0156b22be..e539ad24cb8ea 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -455,8 +455,8 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
// random store we can't handle.
const SCEV *StoreEv = SE->getSCEV(StorePtr);
const SCEVConstant *Stride;
- if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride))) ||
- cast<SCEVAddRecExpr>(StoreEv)->getLoop() != CurLoop)
+ if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride),
+ m_SpecificLoop(CurLoop))))
return LegalStoreKind::None;
// See if the store can be turned into a memset.
@@ -512,9 +512,8 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
const SCEV *LoadEv = SE->getSCEV(LI->getPointerOperand());
// The store and load must share the same stride.
- if (!match(LoadEv,
- m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride))) ||
- cast<SCEVAddRecExpr>(LoadEv)->getLoop() != CurLoop)
+ if (!match(LoadEv, m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride),
+ m_SpecificLoop(CurLoop))))
return LegalStoreKind::None;
// Success. This store can be converted into a memcpy.
@@ -787,11 +786,15 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
// See if the load and store pointer expressions are AddRec like {base,+,1} on
// the current loop, which indicates a strided load and store. If we have
// something else, it's a random load or store we can't handle.
- const SCEVAddRecExpr *StoreEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Dest));
- if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
- return false;
- const SCEVAddRecExpr *LoadEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Source));
- if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
+ const SCEV *StoreEv = SE->getSCEV(Dest);
+ const SCEV *LoadEv = SE->getSCEV(Source);
+ const APInt *StoreStrideValue, *LoadStrideValue;
+ if (!match(StoreEv,
+ m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(StoreStrideValue),
+ m_SpecificLoop(CurLoop))) ||
+ !match(LoadEv,
+ m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(LoadStrideValue),
+ m_SpecificLoop(CurLoop))))
return false;
// Reject memcpys that are so large that they overflow an unsigned.
@@ -799,13 +802,6 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
if ((SizeInBytes >> 32) != 0)
return false;
- // Check if the stride matches the size of the memcpy. If so, then we know
- // that every byte is touched in the loop.
- const APInt *StoreStrideValue, *LoadStrideValue;
- if (!match(StoreEv->getOperand(1), m_scev_APInt(StoreStrideValue)) ||
- !match(LoadEv->getOperand(1), m_scev_APInt(LoadStrideValue)))
- return false;
-
// Huge stride value - give up
if (StoreStrideValue->getBitWidth() > 64 ||
LoadStrideValue->getBitWidth() > 64)
@@ -830,8 +826,8 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
return processLoopStoreOfLoopLoad(
Dest, Source, SE->getConstant(Dest->getType(), SizeInBytes),
- MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI, StoreEv, LoadEv,
- BECount);
+ MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI,
+ cast<SCEVAddRecExpr>(StoreEv), cast<SCEVAddRecExpr>(LoadEv), BECount);
}
/// processLoopMemSet - See if this memset can be promoted to a large memset.
@@ -852,12 +848,11 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
// random store we can't handle.
const SCEV *Ev = SE->getSCEV(Pointer);
const SCEV *PointerStrideSCEV;
- if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV)))) {
+ if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV),
+ m_SpecificLoop(CurLoop)))) {
LLVM_DEBUG(dbgs() << " Pointer is not affine, abort\n");
return false;
}
- if (cast<SCEVAddRecExpr>(Ev)->getLoop() != CurLoop)
- return false;
const SCEV *MemsetSizeSCEV = SE->getSCEV(MSI->getLength());
if (!PointerStrideSCEV || !MemsetSizeSCEV)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, LGTM, thanks!
No description provided.