Skip to content

Commit bc4e14b

Browse files
authored
[SCEV] Add m_scev_Trunc pattern matcher. (#163169)
This patch adds a new m_scev_Trunc pattern matcher for SCEVTruncateExpr and uses it in a few places to slightly simplify the code. PR: #163169
1 parent ad69fef commit bc4e14b

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ m_scev_PtrToInt(const Op0_t &Op0) {
182182
return SCEVUnaryExpr_match<SCEVPtrToIntExpr, Op0_t>(Op0);
183183
}
184184

185+
template <typename Op0_t>
186+
inline SCEVUnaryExpr_match<SCEVTruncateExpr, Op0_t>
187+
m_scev_Trunc(const Op0_t &Op0) {
188+
return m_scev_Unary<SCEVTruncateExpr>(Op0);
189+
}
190+
185191
/// Match a binary SCEV.
186192
template <typename SCEVTy, typename Op0_t, typename Op1_t,
187193
SCEV::NoWrapFlags WrapFlags = SCEV::FlagAnyWrap,

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,20 +5419,15 @@ static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
54195419
if (SourceBits != NewBits)
54205420
return nullptr;
54215421

5422-
const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
5423-
const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
5424-
if (!SExt && !ZExt)
5425-
return nullptr;
5426-
const SCEVTruncateExpr *Trunc =
5427-
SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
5428-
: dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
5429-
if (!Trunc)
5430-
return nullptr;
5431-
const SCEV *X = Trunc->getOperand();
5432-
if (X != SymbolicPHI)
5433-
return nullptr;
5434-
Signed = SExt != nullptr;
5435-
return Trunc->getType();
5422+
if (match(Op, m_scev_SExt(m_scev_Trunc(m_scev_Specific(SymbolicPHI))))) {
5423+
Signed = true;
5424+
return cast<SCEVCastExpr>(Op)->getOperand()->getType();
5425+
}
5426+
if (match(Op, m_scev_ZExt(m_scev_Trunc(m_scev_Specific(SymbolicPHI))))) {
5427+
Signed = false;
5428+
return cast<SCEVCastExpr>(Op)->getOperand()->getType();
5429+
}
5430+
return nullptr;
54365431
}
54375432

54385433
static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
@@ -15428,20 +15423,18 @@ bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS,
1542815423
// Try to match 'zext (trunc A to iB) to iY', which is used
1542915424
// for URem with constant power-of-2 second operands. Make sure the size of
1543015425
// the operand A matches the size of the whole expressions.
15431-
if (const auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Expr))
15432-
if (const auto *Trunc = dyn_cast<SCEVTruncateExpr>(ZExt->getOperand(0))) {
15433-
LHS = Trunc->getOperand();
15434-
// Bail out if the type of the LHS is larger than the type of the
15435-
// expression for now.
15436-
if (getTypeSizeInBits(LHS->getType()) >
15437-
getTypeSizeInBits(Expr->getType()))
15438-
return false;
15439-
if (LHS->getType() != Expr->getType())
15440-
LHS = getZeroExtendExpr(LHS, Expr->getType());
15441-
RHS = getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1)
15442-
<< getTypeSizeInBits(Trunc->getType()));
15443-
return true;
15444-
}
15426+
if (match(Expr, m_scev_ZExt(m_scev_Trunc(m_SCEV(LHS))))) {
15427+
Type *TruncTy = cast<SCEVZeroExtendExpr>(Expr)->getOperand()->getType();
15428+
// Bail out if the type of the LHS is larger than the type of the
15429+
// expression for now.
15430+
if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(Expr->getType()))
15431+
return false;
15432+
if (LHS->getType() != Expr->getType())
15433+
LHS = getZeroExtendExpr(LHS, Expr->getType());
15434+
RHS = getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1)
15435+
<< getTypeSizeInBits(TruncTy));
15436+
return true;
15437+
}
1544515438
const auto *Add = dyn_cast<SCEVAddExpr>(Expr);
1544615439
if (Add == nullptr || Add->getNumOperands() != 2)
1544715440
return false;

0 commit comments

Comments
 (0)