forked from OSchip/llvm-project
PatternMatch: Introduce a matcher for instructions with the "exact" bit. Use it to simplify a few matchers.
llvm-svn: 147403
This commit is contained in:
parent
c5bc4cccd5
commit
9442cd01f6
|
@ -441,6 +441,26 @@ m_IDiv(const LHS &L, const RHS &R) {
|
||||||
return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
|
return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
// Class that matches exact binary ops.
|
||||||
|
//
|
||||||
|
template<typename SubPattern_t>
|
||||||
|
struct Exact_match {
|
||||||
|
SubPattern_t SubPattern;
|
||||||
|
|
||||||
|
Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
|
||||||
|
|
||||||
|
template<typename OpTy>
|
||||||
|
bool match(OpTy *V) {
|
||||||
|
if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
|
||||||
|
return PEO->isExact() && SubPattern.match(V);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Matchers for CmpInst classes
|
// Matchers for CmpInst classes
|
||||||
//
|
//
|
||||||
|
|
|
@ -812,14 +812,10 @@ static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
|
||||||
return Op0;
|
return Op0;
|
||||||
|
|
||||||
// (X / Y) * Y -> X if the division is exact.
|
// (X / Y) * Y -> X if the division is exact.
|
||||||
Value *X = 0, *Y = 0;
|
Value *X = 0;
|
||||||
if ((match(Op0, m_IDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
|
if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
|
||||||
(match(Op1, m_IDiv(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y)
|
match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y)
|
||||||
PossiblyExactOperator *Div =
|
return X;
|
||||||
cast<PossiblyExactOperator>(Y == Op1 ? Op0 : Op1);
|
|
||||||
if (Div->isExact())
|
|
||||||
return X;
|
|
||||||
}
|
|
||||||
|
|
||||||
// i1 mul -> and.
|
// i1 mul -> and.
|
||||||
if (MaxRecurse && Op0->getType()->isIntegerTy(1))
|
if (MaxRecurse && Op0->getType()->isIntegerTy(1))
|
||||||
|
@ -1162,8 +1158,7 @@ static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
|
||||||
|
|
||||||
// (X >> A) << A -> X
|
// (X >> A) << A -> X
|
||||||
Value *X;
|
Value *X;
|
||||||
if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1))) &&
|
if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
|
||||||
cast<PossiblyExactOperator>(Op0)->isExact())
|
|
||||||
return X;
|
return X;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -811,11 +811,9 @@ bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, bool OrZero,
|
||||||
// An exact divide or right shift can only shift off zero bits, so the result
|
// An exact divide or right shift can only shift off zero bits, so the result
|
||||||
// is a power of two only if the first operand is a power of two and not
|
// is a power of two only if the first operand is a power of two and not
|
||||||
// copying a sign bit (sdiv int_min, 2).
|
// copying a sign bit (sdiv int_min, 2).
|
||||||
if (match(V, m_LShr(m_Value(), m_Value())) ||
|
if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
|
||||||
match(V, m_UDiv(m_Value(), m_Value()))) {
|
match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
|
||||||
PossiblyExactOperator *PEO = cast<PossiblyExactOperator>(V);
|
return isPowerOfTwo(cast<Operator>(V)->getOperand(0), TD, OrZero, Depth);
|
||||||
if (PEO->isExact())
|
|
||||||
return isPowerOfTwo(PEO->getOperand(0), TD, OrZero, Depth);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -879,10 +877,8 @@ bool llvm::isKnownNonZero(Value *V, const TargetData *TD, unsigned Depth) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// div exact can only produce a zero if the dividend is zero.
|
// div exact can only produce a zero if the dividend is zero.
|
||||||
else if (match(V, m_IDiv(m_Value(X), m_Value()))) {
|
else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
|
||||||
PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
|
return isKnownNonZero(X, TD, Depth);
|
||||||
if (BO->isExact())
|
|
||||||
return isKnownNonZero(X, TD, Depth);
|
|
||||||
}
|
}
|
||||||
// X + Y.
|
// X + Y.
|
||||||
else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
|
else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
|
||||||
|
|
Loading…
Reference in New Issue