[llvm] [InstCombine] Extend #125676 to handle variable power of 2 (PR #125855)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 5 17:54:08 PST 2025
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/125855
>From c2ac1f0fa62acb52c972c559f55eef00bdcc9fb6 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 5 Feb 2025 21:40:43 +0800
Subject: [PATCH 1/3] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/xor-icmps.ll | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/xor-icmps.ll b/llvm/test/Transforms/InstCombine/xor-icmps.ll
index 382355cae7694d3..5d516a364876bb8 100644
--- a/llvm/test/Transforms/InstCombine/xor-icmps.ll
+++ b/llvm/test/Transforms/InstCombine/xor-icmps.ll
@@ -335,6 +335,46 @@ define i1 @test_xor_of_bittest_ne_ne(i8 %x, i8 %y) {
ret i1 %xor
}
+define i1 @test_xor_of_bittest_ne_ne_var_pow2(i8 %x, i8 %y, i8 %shamt) {
+; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2(
+; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[SHAMT:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1:%.*]], [[POW2]]
+; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP2]], 0
+; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
+; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[XOR1]]
+;
+ %pow2 = shl nuw i8 1, %shamt
+ %mask1 = and i8 %x, %pow2
+ %cmp1 = icmp ne i8 %mask1, 0
+ %mask2 = and i8 %y, %pow2
+ %cmp2 = icmp ne i8 %mask2, 0
+ %xor = xor i1 %cmp1, %cmp2
+ ret i1 %xor
+}
+
+define i1 @test_xor_of_bittest_ne_ne_var_pow2_or_zero(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2_or_zero(
+; CHECK-NEXT: [[NZ:%.*]] = sub i8 0, [[Z:%.*]]
+; CHECK-NEXT: [[POW2:%.*]] = and i8 [[Z]], [[NZ]]
+; CHECK-NEXT: [[TMP3:%.*]] = and i8 [[X:%.*]], [[POW2]]
+; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP3]], 0
+; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
+; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[XOR1]]
+;
+ %nz = sub i8 0, %z
+ %pow2 = and i8 %z, %nz
+ %mask1 = and i8 %x, %pow2
+ %cmp1 = icmp ne i8 %mask1, 0
+ %mask2 = and i8 %y, %pow2
+ %cmp2 = icmp ne i8 %mask2, 0
+ %xor = xor i1 %cmp1, %cmp2
+ ret i1 %xor
+}
+
define i1 @test_xor_of_bittest_eq_eq(i8 %x, i8 %y) {
; CHECK-LABEL: @test_xor_of_bittest_eq_eq(
; CHECK-NEXT: [[Y:%.*]] = xor i8 [[X:%.*]], [[Y1:%.*]]
>From 0867d3db27a82ce86a5b4a9575a4695d4c157982 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 5 Feb 2025 21:50:02 +0800
Subject: [PATCH 2/3] [InstCombine] Extend #125676 to handle variable power of
2
---
.../InstCombine/InstCombineAndOrXor.cpp | 10 +++++-----
llvm/test/Transforms/InstCombine/xor-icmps.ll | 18 +++++++-----------
2 files changed, 12 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 8701f7c28a39fc4..ca4d34d09c3c738 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4200,14 +4200,14 @@ Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
// Fold (icmp eq/ne (X & Pow2), 0) ^ (icmp eq/ne (Y & Pow2), 0) into
// (icmp eq/ne ((X ^ Y) & Pow2), 0)
- Value *X, *Y;
- const APInt *Mask;
+ Value *X, *Y, *Mask;
if (ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
LC->isZero() && RC->isZero() && LHS->hasOneUse() && RHS->hasOneUse() &&
- match(LHS0, m_And(m_Value(X), m_Power2(Mask))) &&
- match(RHS0, m_And(m_Value(Y), m_SpecificInt(*Mask)))) {
+ match(LHS0, m_And(m_Value(X), m_Value(Mask))) &&
+ match(RHS0, m_And(m_Value(Y), m_Specific(Mask))) &&
+ isKnownToBeAPowerOfTwo(Mask, /*OrZero=*/true, /*Depth=*/0, &I)) {
Value *Xor = Builder.CreateXor(X, Y);
- Value *And = Builder.CreateAnd(Xor, *Mask);
+ Value *And = Builder.CreateAnd(Xor, Mask);
return Builder.CreateICmp(PredL == PredR ? ICmpInst::ICMP_NE
: ICmpInst::ICMP_EQ,
And, ConstantInt::getNullValue(Xor->getType()));
diff --git a/llvm/test/Transforms/InstCombine/xor-icmps.ll b/llvm/test/Transforms/InstCombine/xor-icmps.ll
index 5d516a364876bb8..55af2116aacd18e 100644
--- a/llvm/test/Transforms/InstCombine/xor-icmps.ll
+++ b/llvm/test/Transforms/InstCombine/xor-icmps.ll
@@ -338,12 +338,10 @@ define i1 @test_xor_of_bittest_ne_ne(i8 %x, i8 %y) {
define i1 @test_xor_of_bittest_ne_ne_var_pow2(i8 %x, i8 %y, i8 %shamt) {
; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2(
; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[SHAMT:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1:%.*]], [[POW2]]
-; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP2]], 0
-; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
+; CHECK-NEXT: [[Y:%.*]] = xor i8 [[X:%.*]], [[Y1:%.*]]
+; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y]], [[POW2]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
-; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
-; CHECK-NEXT: ret i1 [[XOR1]]
+; CHECK-NEXT: ret i1 [[CMP2]]
;
%pow2 = shl nuw i8 1, %shamt
%mask1 = and i8 %x, %pow2
@@ -357,13 +355,11 @@ define i1 @test_xor_of_bittest_ne_ne_var_pow2(i8 %x, i8 %y, i8 %shamt) {
define i1 @test_xor_of_bittest_ne_ne_var_pow2_or_zero(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2_or_zero(
; CHECK-NEXT: [[NZ:%.*]] = sub i8 0, [[Z:%.*]]
-; CHECK-NEXT: [[POW2:%.*]] = and i8 [[Z]], [[NZ]]
-; CHECK-NEXT: [[TMP3:%.*]] = and i8 [[X:%.*]], [[POW2]]
-; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP3]], 0
-; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[NZ]]
+; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[TMP2]], [[Z]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
-; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
-; CHECK-NEXT: ret i1 [[XOR1]]
+; CHECK-NEXT: ret i1 [[CMP2]]
;
%nz = sub i8 0, %z
%pow2 = and i8 %z, %nz
>From e4310008fe67da1b125833daf638ba9f16b6b621 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 6 Feb 2025 09:53:47 +0800
Subject: [PATCH 3/3] [InstCombine] Rename `Mask` to `Pow2`. NFC.
---
.../lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index ca4d34d09c3c738..81c88673d48dcbe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4200,14 +4200,14 @@ Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
// Fold (icmp eq/ne (X & Pow2), 0) ^ (icmp eq/ne (Y & Pow2), 0) into
// (icmp eq/ne ((X ^ Y) & Pow2), 0)
- Value *X, *Y, *Mask;
+ Value *X, *Y, *Pow2;
if (ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
LC->isZero() && RC->isZero() && LHS->hasOneUse() && RHS->hasOneUse() &&
- match(LHS0, m_And(m_Value(X), m_Value(Mask))) &&
- match(RHS0, m_And(m_Value(Y), m_Specific(Mask))) &&
- isKnownToBeAPowerOfTwo(Mask, /*OrZero=*/true, /*Depth=*/0, &I)) {
+ match(LHS0, m_And(m_Value(X), m_Value(Pow2))) &&
+ match(RHS0, m_And(m_Value(Y), m_Specific(Pow2))) &&
+ isKnownToBeAPowerOfTwo(Pow2, /*OrZero=*/true, /*Depth=*/0, &I)) {
Value *Xor = Builder.CreateXor(X, Y);
- Value *And = Builder.CreateAnd(Xor, Mask);
+ Value *And = Builder.CreateAnd(Xor, Pow2);
return Builder.CreateICmp(PredL == PredR ? ICmpInst::ICMP_NE
: ICmpInst::ICMP_EQ,
And, ConstantInt::getNullValue(Xor->getType()));
More information about the llvm-commits
mailing list