Skip to content

Commit 0867d3d

Browse files
committed
[InstCombine] Extend llvm#125676 to handle variable power of 2
1 parent c2ac1f0 commit 0867d3d

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4200,14 +4200,14 @@ Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
42004200

42014201
// Fold (icmp eq/ne (X & Pow2), 0) ^ (icmp eq/ne (Y & Pow2), 0) into
42024202
// (icmp eq/ne ((X ^ Y) & Pow2), 0)
4203-
Value *X, *Y;
4204-
const APInt *Mask;
4203+
Value *X, *Y, *Mask;
42054204
if (ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
42064205
LC->isZero() && RC->isZero() && LHS->hasOneUse() && RHS->hasOneUse() &&
4207-
match(LHS0, m_And(m_Value(X), m_Power2(Mask))) &&
4208-
match(RHS0, m_And(m_Value(Y), m_SpecificInt(*Mask)))) {
4206+
match(LHS0, m_And(m_Value(X), m_Value(Mask))) &&
4207+
match(RHS0, m_And(m_Value(Y), m_Specific(Mask))) &&
4208+
isKnownToBeAPowerOfTwo(Mask, /*OrZero=*/true, /*Depth=*/0, &I)) {
42094209
Value *Xor = Builder.CreateXor(X, Y);
4210-
Value *And = Builder.CreateAnd(Xor, *Mask);
4210+
Value *And = Builder.CreateAnd(Xor, Mask);
42114211
return Builder.CreateICmp(PredL == PredR ? ICmpInst::ICMP_NE
42124212
: ICmpInst::ICMP_EQ,
42134213
And, ConstantInt::getNullValue(Xor->getType()));

llvm/test/Transforms/InstCombine/xor-icmps.ll

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,10 @@ define i1 @test_xor_of_bittest_ne_ne(i8 %x, i8 %y) {
338338
define i1 @test_xor_of_bittest_ne_ne_var_pow2(i8 %x, i8 %y, i8 %shamt) {
339339
; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2(
340340
; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[SHAMT:%.*]]
341-
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1:%.*]], [[POW2]]
342-
; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP2]], 0
343-
; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
341+
; CHECK-NEXT: [[Y:%.*]] = xor i8 [[X:%.*]], [[Y1:%.*]]
342+
; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y]], [[POW2]]
344343
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
345-
; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
346-
; CHECK-NEXT: ret i1 [[XOR1]]
344+
; CHECK-NEXT: ret i1 [[CMP2]]
347345
;
348346
%pow2 = shl nuw i8 1, %shamt
349347
%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) {
357355
define i1 @test_xor_of_bittest_ne_ne_var_pow2_or_zero(i8 %x, i8 %y, i8 %z) {
358356
; CHECK-LABEL: @test_xor_of_bittest_ne_ne_var_pow2_or_zero(
359357
; CHECK-NEXT: [[NZ:%.*]] = sub i8 0, [[Z:%.*]]
360-
; CHECK-NEXT: [[POW2:%.*]] = and i8 [[Z]], [[NZ]]
361-
; CHECK-NEXT: [[TMP3:%.*]] = and i8 [[X:%.*]], [[POW2]]
362-
; CHECK-NEXT: [[XOR:%.*]] = icmp ne i8 [[TMP3]], 0
363-
; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[Y:%.*]], [[POW2]]
358+
; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X:%.*]], [[Y:%.*]]
359+
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[NZ]]
360+
; CHECK-NEXT: [[MASK2:%.*]] = and i8 [[TMP2]], [[Z]]
364361
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[MASK2]], 0
365-
; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[XOR]], [[CMP2]]
366-
; CHECK-NEXT: ret i1 [[XOR1]]
362+
; CHECK-NEXT: ret i1 [[CMP2]]
367363
;
368364
%nz = sub i8 0, %z
369365
%pow2 = and i8 %z, %nz

0 commit comments

Comments
 (0)