aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ScalarEvolution.cpp
diff options
context:
space:
mode:
authorFlorian Hahn <flo@fhahn.com>2024-12-17 12:12:56 +0000
committerGitHub <noreply@github.com>2024-12-17 12:12:56 +0000
commit8ea9576d94ec6b15a2a3ba181af15d136283bde4 (patch)
tree66cf979be10de90b25c9c9b351241dc61e8c429e /llvm/lib/Analysis/ScalarEvolution.cpp
parentc1f5937eb4bf4002b8205873189f900364868fd5 (diff)
downloadllvm-8ea9576d94ec6b15a2a3ba181af15d136283bde4.zip
llvm-8ea9576d94ec6b15a2a3ba181af15d136283bde4.tar.gz
llvm-8ea9576d94ec6b15a2a3ba181af15d136283bde4.tar.bz2
[SCEV] Add initial matchers for SCEV expressions. (NFC) (#119390)
This patch adds initial matchers for unary and binary SCEV expressions and specializes it for SExt, ZExt and binary add expressions. Also adds matchers for SCEVConstant and SCEVUnknown. This patch only converts a few instances to use the new matchers to make sure everything builds as expected for now. The goal of the matchers is to hopefully make it slightly easier to write code matching SCEV patterns. Depends on https://github.com/llvm/llvm-project/pull/119389 PR: https://github.com/llvm/llvm-project/pull/119390
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolution.cpp')
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp35
1 files changed, 14 insertions, 21 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index e2c2500..c820e8b 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12725,33 +12725,28 @@ bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred,
static bool isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred,
const SCEV *LHS, const SCEV *RHS) {
// zext x u<= sext x, sext x s<= zext x
+ const SCEV *Op;
switch (Pred) {
case ICmpInst::ICMP_SGE:
std::swap(LHS, RHS);
[[fallthrough]];
case ICmpInst::ICMP_SLE: {
- // If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt.
- const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(LHS);
- const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(RHS);
- if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand())
- return true;
- break;
+ // If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt.
+ return match(LHS, m_scev_SExt(m_SCEV(Op))) &&
+ match(RHS, m_scev_ZExt(m_Specific(Op)));
}
case ICmpInst::ICMP_UGE:
std::swap(LHS, RHS);
[[fallthrough]];
case ICmpInst::ICMP_ULE: {
- // If operand >=s 0 then ZExt == SExt. If operand <s 0 then ZExt <u SExt.
- const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS);
- const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(RHS);
- if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand())
- return true;
- break;
+ // If operand >=u 0 then ZExt == SExt. If operand <u 0 then ZExt <u SExt.
+ return match(LHS, m_scev_ZExt(m_SCEV(Op))) &&
+ match(RHS, m_scev_SExt(m_Specific(Op)));
}
default:
- break;
+ return false;
};
- return false;
+ llvm_unreachable("unhandled case");
}
bool
@@ -15417,14 +15412,12 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
// (X >=u C1).
auto MatchRangeCheckIdiom = [&SE, Predicate, LHS, RHS, &RewriteMap,
&ExprsToRewrite]() {
- auto *AddExpr = dyn_cast<SCEVAddExpr>(LHS);
- if (!AddExpr || AddExpr->getNumOperands() != 2)
- return false;
-
- auto *C1 = dyn_cast<SCEVConstant>(AddExpr->getOperand(0));
- auto *LHSUnknown = dyn_cast<SCEVUnknown>(AddExpr->getOperand(1));
+ const SCEVConstant *C1;
+ const SCEVUnknown *LHSUnknown;
auto *C2 = dyn_cast<SCEVConstant>(RHS);
- if (!C1 || !C2 || !LHSUnknown)
+ if (!match(LHS,
+ m_scev_Add(m_SCEVConstant(C1), m_SCEVUnknown(LHSUnknown))) ||
+ !C2)
return false;
auto ExactRegion =