[llvm] [InstCombine] Remove one-use requirement for add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN (PR #90509)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 29 22:01:47 PDT 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/90509
>From 831777ed34c3fbb82703409a5bc61507a3bd7d89 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 29 Apr 2024 14:17:39 -0400
Subject: [PATCH] [InstCombine] Remove one-use requirement for add iN (sext i1
X), (sext i1 Y) --> sext (X | Y) to iN
Since these remove instructions, we can forgo the one-use check
---
.../InstCombineSimplifyDemanded.cpp | 8 +++---
llvm/test/Transforms/InstCombine/add.ll | 28 ++++++++-----------
2 files changed, 15 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 6739b8745d74e4..cb543ed1da0c22 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -510,10 +510,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
}
// add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
- // TODO: Relax the one-use checks because we are removing an instruction?
- if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
- m_OneUse(m_SExt(m_Value(Y))))) &&
- X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
+ if (match(I, m_Add(m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
+ X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
+ (I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {
+
// Truth table for inputs and output signbits:
// X:0 | X:1
// -----------
diff --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index 56ee54d351e762..5c7826f33074c1 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -1435,15 +1435,12 @@ define i32 @and31_add_sexts(i1 %x, i1 %y) {
ret i32 %r
}
-; Negative test - extra use
-
define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
; CHECK-LABEL: @lshr_add_use_sexts(
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
-; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
-; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
-; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
+; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%xs = sext i1 %x to i32
@@ -1454,15 +1451,12 @@ define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
ret i32 %r
}
-; Negative test - extra use
-
define i32 @lshr_add_use2_sexts(i1 %x, i1 %y, ptr %p) {
; CHECK-LABEL: @lshr_add_use2_sexts(
-; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
-; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
-; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
+; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y]]
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%xs = sext i1 %x to i32
@@ -4018,8 +4012,8 @@ define i32 @add_reduce_sqr_sum_varC_invalid2(i32 %a, i32 %b) {
define i32 @fold_sext_addition_or_disjoint(i8 %x) {
; CHECK-LABEL: @fold_sext_addition_or_disjoint(
-; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX:%.*]] to i32
-; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1246
+; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 1246
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or disjoint i8 %x, 12
@@ -4043,8 +4037,8 @@ define i32 @fold_sext_addition_fail(i8 %x) {
define i32 @fold_zext_addition_or_disjoint(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_or_disjoint(
-; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX:%.*]] to i32
-; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1246
+; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[TMP1]], 1246
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or disjoint i8 %x, 12
@@ -4055,9 +4049,9 @@ define i32 @fold_zext_addition_or_disjoint(i8 %x) {
define i32 @fold_zext_addition_or_disjoint2(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_or_disjoint2(
-; CHECK-NEXT: [[XX:%.*]] = add nuw i8 [[X:%.*]], 4
-; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
-; CHECK-NEXT: ret i32 [[SE]]
+; CHECK-NEXT: [[TMP1:%.*]] = add nuw i8 [[X:%.*]], 4
+; CHECK-NEXT: [[R:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[R]]
;
%xx = or disjoint i8 %x, 18
%se = zext i8 %xx to i32
More information about the llvm-commits
mailing list