Skip to content

Commit cee1899

Browse files
committed
Explicitly match all the cases
1 parent 843b0a5 commit cee1899

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,28 +2111,6 @@ template <typename Op_t> struct PtrToIntSameSize_match {
21112111
}
21122112
};
21132113

2114-
template <typename Op_t> struct PtrToIntOrAddr_GEAddrSize_match {
2115-
const DataLayout &DL;
2116-
Op_t Op;
2117-
2118-
PtrToIntOrAddr_GEAddrSize_match(const DataLayout &DL, const Op_t &OpMatch)
2119-
: DL(DL), Op(OpMatch) {}
2120-
2121-
template <typename OpTy> bool match(OpTy *V) const {
2122-
if (auto *O = dyn_cast<Operator>(V)) {
2123-
unsigned Opcode = O->getOpcode();
2124-
// The ptrtoaddr result type always matches the address size.
2125-
// For ptrtoint we have to explicitly check it.
2126-
return (Opcode == Instruction::PtrToAddr ||
2127-
(Opcode == Instruction::PtrToInt &&
2128-
O->getType()->getScalarSizeInBits() >=
2129-
DL.getAddressSizeInBits(O->getOperand(0)->getType()))) &&
2130-
Op.match(O->getOperand(0));
2131-
}
2132-
return false;
2133-
}
2134-
};
2135-
21362114
template <typename Op_t> struct NNegZExt_match {
21372115
Op_t Op;
21382116

@@ -2218,14 +2196,6 @@ template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
22182196
return m_CombineOr(m_PtrToInt(Op), m_PtrToAddr(Op));
22192197
}
22202198

2221-
/// Matches PtrToInt or PtrToAddr where the result is greater than or equal
2222-
/// to the pointer address size.
2223-
template <typename OpTy>
2224-
inline PtrToIntOrAddr_GEAddrSize_match<OpTy>
2225-
m_PtrToIntOrAddr_GEAddrSize(const DataLayout &DL, const OpTy &Op) {
2226-
return PtrToIntOrAddr_GEAddrSize_match<OpTy>(DL, Op);
2227-
}
2228-
22292199
/// Matches IntToPtr.
22302200
template <typename OpTy>
22312201
inline CastOperator_match<OpTy, Instruction::IntToPtr>

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,9 +2773,21 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
27732773
/* IsNUW */ false))
27742774
return replaceInstUsesWith(I, Res);
27752775

2776-
if (match(Op0, m_ZExt(m_PtrToIntOrAddr_GEAddrSize(DL, m_Value(LHSOp)))) &&
2777-
match(Op1,
2778-
m_ZExtOrSelf(m_PtrToIntOrAddr_GEAddrSize(DL, m_Value(RHSOp))))) {
2776+
auto MatchSubOfZExtOfPtrToIntOrAddr = [&]() {
2777+
if (match(Op0, m_ZExt(m_PtrToIntSameSize(DL, m_Value(LHSOp)))) &&
2778+
match(Op1, m_ZExt(m_PtrToIntSameSize(DL, m_Value(RHSOp)))))
2779+
return true;
2780+
if (match(Op0, m_ZExt(m_PtrToAddr(m_Value(LHSOp)))) &&
2781+
match(Op1, m_ZExt(m_PtrToAddr(m_Value(RHSOp)))))
2782+
return true;
2783+
// Special case for non-canonical ptrtoint in constant expression,
2784+
// where the zext has been folded into the ptrtoint.
2785+
if (match(Op0, m_ZExt(m_PtrToIntSameSize(DL, m_Value(LHSOp)))) &&
2786+
match(Op1, m_PtrToInt(m_Value(RHSOp))))
2787+
return true;
2788+
return false;
2789+
};
2790+
if (MatchSubOfZExtOfPtrToIntOrAddr()) {
27792791
if (auto *GEP = dyn_cast<GEPOperator>(LHSOp)) {
27802792
if (GEP->getPointerOperand() == RHSOp) {
27812793
if (GEP->hasNoUnsignedWrap() || GEP->hasNoUnsignedSignedWrap()) {

0 commit comments

Comments
 (0)