Skip to content

Commit b8df88b

Browse files
committed
[InstCombine] Support zext nneg in gep of sext add fold
Add m_NNegZext() and m_SExtLike() matchers to make doing these kinds of changes simpler in the future.
1 parent 4d71124 commit b8df88b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,19 @@ template <typename Op_t> struct PtrToIntSameSize_match {
16561656
}
16571657
};
16581658

1659+
template <typename Op_t> struct NNegZExt_match {
1660+
Op_t Op;
1661+
1662+
NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
1663+
1664+
template <typename OpTy> bool match(OpTy *V) {
1665+
if (auto *I = dyn_cast<Instruction>(V))
1666+
return I->getOpcode() == Instruction::ZExt && I->hasNonNeg() &&
1667+
Op.match(I->getOperand(0));
1668+
return false;
1669+
}
1670+
};
1671+
16591672
/// Matches BitCast.
16601673
template <typename OpTy>
16611674
inline CastOperator_match<OpTy, Instruction::BitCast>
@@ -1707,6 +1720,11 @@ inline CastInst_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
17071720
return CastInst_match<OpTy, Instruction::ZExt>(Op);
17081721
}
17091722

1723+
template <typename OpTy>
1724+
inline NNegZExt_match<OpTy> m_NNegZExt(const OpTy &Op) {
1725+
return NNegZExt_match<OpTy>(Op);
1726+
}
1727+
17101728
template <typename OpTy>
17111729
inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, OpTy>
17121730
m_ZExtOrSelf(const OpTy &Op) {
@@ -1719,6 +1737,14 @@ m_SExtOrSelf(const OpTy &Op) {
17191737
return m_CombineOr(m_SExt(Op), Op);
17201738
}
17211739

1740+
/// Match either "sext" or "zext nneg".
1741+
template <typename OpTy>
1742+
inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>,
1743+
NNegZExt_match<OpTy>>
1744+
m_SExtLike(const OpTy &Op) {
1745+
return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
1746+
}
1747+
17221748
template <typename OpTy>
17231749
inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>,
17241750
CastInst_match<OpTy, Instruction::SExt>>

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
25162516
Idx2);
25172517
}
25182518
ConstantInt *C;
2519-
if (match(GEP.getOperand(1), m_OneUse(m_SExt(m_OneUse(m_NSWAdd(
2519+
if (match(GEP.getOperand(1), m_OneUse(m_SExtLike(m_OneUse(m_NSWAdd(
25202520
m_Value(Idx1), m_ConstantInt(C))))))) {
25212521
// %add = add nsw i32 %idx1, idx2
25222522
// %sidx = sext i32 %add to i64

llvm/test/Transforms/InstCombine/array.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ define void @test_zext_nneg(ptr %ptr, i32 %a, i32 %b) {
7777
; CHECK-LABEL: define void @test_zext_nneg(
7878
; CHECK-SAME: ptr [[PTR:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) {
7979
; CHECK-NEXT: entry:
80-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 10
81-
; CHECK-NEXT: [[IDX:%.*]] = zext nneg i32 [[ADD]] to i64
82-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[PTR]], i64 [[IDX]]
80+
; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[A]] to i64
81+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[PTR]], i64 [[TMP0]]
82+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[TMP1]], i64 10
8383
; CHECK-NEXT: store i32 [[B]], ptr [[GEP]], align 4
8484
; CHECK-NEXT: ret void
8585
;

0 commit comments

Comments
 (0)