Skip to content

Commit f688ae7

Browse files
committed
[InstCombine] allow vector splats for add+xor with low-mask
This can be allowed with undef elements too, but that can be another step: https://alive2.llvm.org/ce/z/hnC4Z-
1 parent 69efcd0 commit f688ae7

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,19 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
924924
C2->isMinSignedValue() && C2->sext(Ty->getScalarSizeInBits()) == *C)
925925
return CastInst::Create(Instruction::SExt, X, Ty);
926926

927-
// (X ^ signmask) + C --> (X + (signmask ^ C))
928-
if (match(Op0, m_Xor(m_Value(X), m_APInt(C2))) && C2->isSignMask())
929-
return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C2 ^ *C));
927+
if (match(Op0, m_Xor(m_Value(X), m_APInt(C2)))) {
928+
// (X ^ signmask) + C --> (X + (signmask ^ C))
929+
if (C2->isSignMask())
930+
return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C2 ^ *C));
931+
932+
// If X has no high-bits set above an xor mask:
933+
// add (xor X, LowMaskC), C --> sub (LowMaskC + C), X
934+
if (C2->isMask()) {
935+
KnownBits LHSKnown = computeKnownBits(X, 0, &Add);
936+
if ((*C2 | LHSKnown.Zero).isAllOnesValue())
937+
return BinaryOperator::CreateSub(ConstantInt::get(Ty, *C2 + *C), X);
938+
}
939+
}
930940

931941
if (C->isOneValue() && Op0->hasOneUse()) {
932942
// add (sext i1 X), 1 --> zext (not X)
@@ -1295,15 +1305,6 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
12951305
Value *NewShl = Builder.CreateShl(XorLHS, ShAmt, "sext");
12961306
return BinaryOperator::CreateAShr(NewShl, ShAmt);
12971307
}
1298-
1299-
// If X has no high-bits above the xor mask set:
1300-
// add (xor X, LowMaskC), C --> sub (LowMaskC + C), X
1301-
if ((XorRHS->getValue() + 1).isPowerOf2()) {
1302-
KnownBits LHSKnown = computeKnownBits(XorLHS, 0, &I);
1303-
if ((XorRHS->getValue() | LHSKnown.Zero).isAllOnesValue())
1304-
return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
1305-
XorLHS);
1306-
}
13071308
}
13081309
}
13091310

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ define i32 @xor_add_extra_use(i32 %x) {
6868
define <2 x i8> @xor_add_splat(<2 x i8> %x) {
6969
; CHECK-LABEL: @xor_add_splat(
7070
; CHECK-NEXT: [[AND:%.*]] = and <2 x i8> [[X:%.*]], <i8 24, i8 24>
71-
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i8> [[AND]], <i8 63, i8 63>
72-
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i8> [[XOR]], <i8 42, i8 42>
71+
; CHECK-NEXT: [[ADD:%.*]] = sub nuw nsw <2 x i8> <i8 105, i8 105>, [[AND]]
7372
; CHECK-NEXT: ret <2 x i8> [[ADD]]
7473
;
7574
%and = and <2 x i8> %x, <i8 24, i8 24>

0 commit comments

Comments
 (0)