Skip to content

Commit e06c148

Browse files
authored
[IVDesc] Use SCEVPatternMatch to improve code (NFC) (#168397)
1 parent af3af8e commit e06c148

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/Analysis/LoopInfo.h"
1616
#include "llvm/Analysis/ScalarEvolution.h"
1717
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18+
#include "llvm/Analysis/ScalarEvolutionPatternMatch.h"
1819
#include "llvm/Analysis/ValueTracking.h"
1920
#include "llvm/IR/Dominators.h"
2021
#include "llvm/IR/Instructions.h"
@@ -25,6 +26,7 @@
2526

2627
using namespace llvm;
2728
using namespace llvm::PatternMatch;
29+
using namespace llvm::SCEVPatternMatch;
2830

2931
#define DEBUG_TYPE "iv-descriptors"
3032

@@ -721,11 +723,12 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
721723
if (!SE.isSCEVable(Ty))
722724
return std::nullopt;
723725

724-
auto *AR = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(V));
725-
if (!AR || AR->getLoop() != TheLoop)
726+
auto *AR = SE.getSCEV(V);
727+
const SCEV *Step;
728+
if (!match(AR, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step),
729+
m_SpecificLoop(TheLoop))))
726730
return std::nullopt;
727731

728-
const SCEV *Step = AR->getStepRecurrence(SE);
729732
if ((isFindFirstIVRecurrenceKind(Kind) && !SE.isKnownNegative(Step)) ||
730733
(isFindLastIVRecurrenceKind(Kind) && !SE.isKnownPositive(Step)))
731734
return std::nullopt;
@@ -1616,41 +1619,31 @@ bool InductionDescriptor::isInductionPHI(
16161619

16171620
// Check that the PHI is consecutive.
16181621
const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
1619-
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
1622+
const SCEV *Step;
16201623

1621-
if (!AR) {
1622-
LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1623-
return false;
1624-
}
1625-
1626-
if (AR->getLoop() != TheLoop) {
1627-
// FIXME: We should treat this as a uniform. Unfortunately, we
1628-
// don't currently know how to handled uniform PHIs.
1624+
// FIXME: We are currently matching the specific loop TheLoop; if it doesn't
1625+
// match, we should treat it as a uniform. Unfortunately, we don't currently
1626+
// know how to handled uniform PHIs.
1627+
if (!match(PhiScev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step),
1628+
m_SpecificLoop(TheLoop)))) {
16291629
LLVM_DEBUG(
1630-
dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
1630+
dbgs() << "LV: PHI is not a poly recurrence for requested loop.\n");
16311631
return false;
16321632
}
16331633

16341634
// This function assumes that InductionPhi is called only on Phi nodes
16351635
// present inside loop headers. Check for the same, and throw an assert if
16361636
// the current Phi is not present inside the loop header.
1637-
assert(Phi->getParent() == AR->getLoop()->getHeader()
1638-
&& "Invalid Phi node, not present in loop header");
1637+
assert(Phi->getParent() == TheLoop->getHeader() &&
1638+
"Invalid Phi node, not present in loop header");
16391639

16401640
Value *StartValue =
1641-
Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader());
1641+
Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
16421642

1643-
BasicBlock *Latch = AR->getLoop()->getLoopLatch();
1643+
BasicBlock *Latch = TheLoop->getLoopLatch();
16441644
if (!Latch)
16451645
return false;
16461646

1647-
const SCEV *Step = AR->getStepRecurrence(*SE);
1648-
// Calculate the pointer stride and check if it is consecutive.
1649-
// The stride may be a constant or a loop invariant integer value.
1650-
const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step);
1651-
if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop))
1652-
return false;
1653-
16541647
if (PhiTy->isIntegerTy()) {
16551648
BinaryOperator *BOp =
16561649
dyn_cast<BinaryOperator>(Phi->getIncomingValueForBlock(Latch));

0 commit comments

Comments
 (0)