[InstCombine] Fold shift of a constant into a reverse shift (#192982)

C1 << (C2 - X) -> (C1 << C2) >> X
    C1 << (C2 ^ X) -> (C1 << C2) >> X (if equivalent to the above)
    C1 >> (C2 - X) -> (C1 >> C2) << X (right shift modes match)
    C1 >> (C2 ^ X) -> (C1 >> C2) << X (if equivalent to the above)

Proof: https://alive2.llvm.org/ce/z/q-4soi
This commit is contained in:
Piotr Fusik
2026-04-27 08:59:59 +02:00
committed by GitHub
parent 9b787812e1
commit 58f2c189c4
2 changed files with 579 additions and 31 deletions

View File

@@ -460,42 +460,77 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
unsigned BitWidth = Ty->getScalarSizeInBits();
const APInt *AC, *AddC;
// Try to pre-shift a constant shifted by a variable amount added with a
// negative number:
// C << (X - AddC) --> (C >> AddC) << X
// and
// C >> (X - AddC) --> (C << AddC) >> X
if (match(Op0, m_APInt(AC)) && match(Op1, m_Add(m_Value(A), m_APInt(AddC))) &&
AddC->isNegative() && (-*AddC).ult(BitWidth)) {
const APInt *AC;
if (match(Op0, m_APInt(AC))) {
assert(!AC->isZero() && "Expected simplify of shifted zero");
unsigned PosOffset = (-*AddC).getZExtValue();
auto isSuitableForPreShift = [PosOffset, &I, AC]() {
switch (I.getOpcode()) {
default:
return false;
case Instruction::Shl:
return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
AC->eq(AC->lshr(PosOffset).shl(PosOffset));
case Instruction::LShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset));
case Instruction::AShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset));
// Try to pre-shift a constant shifted by a variable amount added with a
// negative number:
// C << (X - AddC) --> (C >> AddC) << X
// and
// C >> (X - AddC) --> (C << AddC) >> X
const APInt *AddC;
if (match(Op1, m_Add(m_Value(A), m_APInt(AddC))) && AddC->isNegative() &&
(-*AddC).ult(BitWidth)) {
unsigned PosOffset = (-*AddC).getZExtValue();
auto isSuitableForPreShift = [PosOffset, &I, AC]() {
switch (I.getOpcode()) {
default:
return false;
case Instruction::Shl:
return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
AC->eq(AC->lshr(PosOffset).shl(PosOffset));
case Instruction::LShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset));
case Instruction::AShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset));
}
};
if (isSuitableForPreShift()) {
Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl
? AC->lshr(PosOffset)
: AC->shl(PosOffset));
BinaryOperator *NewShiftOp =
BinaryOperator::Create(I.getOpcode(), NewC, A);
if (I.getOpcode() == Instruction::Shl) {
NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
} else {
NewShiftOp->setIsExact();
}
return NewShiftOp;
}
};
if (isSuitableForPreShift()) {
Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl
? AC->lshr(PosOffset)
: AC->shl(PosOffset));
BinaryOperator *NewShiftOp =
BinaryOperator::Create(I.getOpcode(), NewC, A);
}
// C1 << (C2 - X) -> (C1 << C2) >> X
// C1 >> (C2 - X) -> (C1 >> C2) << X
// X must be u<= C2 (checked by NUWSub).
// Also match (X ^ C2) if equivalent to (C2 - X).
uint64_t C2;
Value *X;
if (match(Op1, m_NUWSub(m_ConstantInt(C2), m_Value(X))) ||
(match(Op1, m_Xor(m_Value(X), m_ConstantInt(C2))) &&
(C2 | computeKnownBits(X, &I).Zero).isAllOnes())) {
if (I.getOpcode() == Instruction::Shl) {
NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
} else {
NewShiftOp->setIsExact();
if (AC->countl_zero() >= C2)
return BinaryOperator::CreateExactLShr(
ConstantInt::get(Ty, AC->shl(C2)), X);
if (AC->countl_one() > C2)
return BinaryOperator::CreateExactAShr(
ConstantInt::get(Ty, AC->shl(C2)), X);
} else if (AC->countr_zero() >= C2) {
if (AC->isSignBitClear()) {
auto *Shl = BinaryOperator::CreateNUWShl(
ConstantInt::get(Ty, AC->lshr(C2)), X);
Shl->setHasNoSignedWrap();
return Shl;
}
if (I.getOpcode() == Instruction::LShr)
return BinaryOperator::CreateNUWShl(
ConstantInt::get(Ty, AC->lshr(C2)), X);
return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, AC->ashr(C2)),
X);
}
return NewShiftOp;
}
}

View File

@@ -0,0 +1,513 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
declare void @use(i8)
define i8 @shl_sub(i8 %x) {
; CHECK-LABEL: @shl_sub(
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 32, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 4, %x
%r = shl i8 2, %s
ret i8 %r
}
define i8 @shl_sub_maxlzero(i8 %x) {
; CHECK-LABEL: @shl_sub_maxlzero(
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 -64, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 6, %x
%r = shl i8 3, %s
ret i8 %r
}
define i8 @shl_sub_maxlone(i8 %x) {
; CHECK-LABEL: @shl_sub_maxlone(
; CHECK-NEXT: [[R:%.*]] = ashr exact i8 -128, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 6, %x
%r = shl i8 -2, %s
ret i8 %r
}
define i8 @shl_sub_multiuse(i8 %x) {
; CHECK-LABEL: @shl_sub_multiuse(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 3, [[X:%.*]]
; CHECK-NEXT: call void @use(i8 [[S]])
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 16, [[X]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
call void @use(i8 %s)
%r = shl i8 2, %s
ret i8 %r
}
define <2 x i8> @shl_sub_vec_splat(<2 x i8> %x) {
; CHECK-LABEL: @shl_sub_vec_splat(
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i8> splat (i8 48), [[X:%.*]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 4>, %x
%r = shl <2 x i8> <i8 3, i8 3>, %s
ret <2 x i8> %r
}
define <2 x i8> @shl_sub_vec_splat_poison(<2 x i8> %x) {
; CHECK-LABEL: @shl_sub_vec_splat_poison(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> <i8 4, i8 poison>, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = shl <2 x i8> <i8 3, i8 poison>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 poison>, %x
%r = shl <2 x i8> <i8 3, i8 poison>, %s
ret <2 x i8> %r
}
define <2 x i8> @shl_sub_vec_non_splat(<2 x i8> %x) {
; CHECK-LABEL: @shl_sub_vec_non_splat(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> <i8 4, i8 3>, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = shl <2 x i8> <i8 3, i8 2>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 3>, %x
%r = shl <2 x i8> <i8 3, i8 2>, %s
ret <2 x i8> %r
}
define i8 @shl_sub_negative_uw(i8 %x) {
; CHECK-LABEL: @shl_sub_negative_uw(
; CHECK-NEXT: [[S:%.*]] = sub i8 4, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = shl i8 2, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub i8 4, %x
%r = shl i8 2, %s
ret i8 %r
}
define i8 @shl_sub_negative_nolzero(i8 %x) {
; CHECK-LABEL: @shl_sub_negative_nolzero(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 7, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = shl i8 2, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 7, %x
%r = shl i8 2, %s
ret i8 %r
}
define i8 @shl_sub_negative_nolone(i8 %x) {
; CHECK-LABEL: @shl_sub_negative_nolone(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 7, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = shl i8 -2, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 7, %x
%r = shl i8 -2, %s
ret i8 %r
}
define i8 @shl_xor(i8 %x) {
; CHECK-LABEL: @shl_xor(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 16, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 3
%r = shl i8 2, %s
ret i8 %r
}
define i16 @shl_xor_i2(i2 %x) {
; CHECK-LABEL: @shl_xor_i2(
; CHECK-NEXT: [[M:%.*]] = zext i2 [[X:%.*]] to i16
; CHECK-NEXT: [[R:%.*]] = lshr exact i16 256, [[M]]
; CHECK-NEXT: ret i16 [[R]]
;
%m = zext i2 %x to i16
%s = xor i16 %m, 7
%r = shl i16 2, %s
ret i16 %r
}
define i8 @shl_xor_notmask(i8 %x) {
; CHECK-LABEL: @shl_xor_notmask(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 2
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 -128, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 2
%s = xor i8 %m, 6
%r = shl i8 2, %s
ret i8 %r
}
define i8 @shl_xor_multiuse(i8 %x) {
; CHECK-LABEL: @shl_xor_multiuse(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[S:%.*]] = xor i8 [[M]], 3
; CHECK-NEXT: call void @use(i8 [[S]])
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 16, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 3
call void @use(i8 %s)
%r = shl i8 2, %s
ret i8 %r
}
define <2 x i8> @shl_xor_vec_splat(<2 x i8> %x) {
; CHECK-LABEL: @shl_xor_vec_splat(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 3)
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i8> splat (i8 40), [[M]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 3, i8 3>, %x
%s = xor <2 x i8> <i8 3, i8 3>, %m
%r = shl <2 x i8> <i8 5, i8 5>, %s
ret <2 x i8> %r
}
define <2 x i8> @shl_xor_vec_splat_poison(<2 x i8> %x) {
; CHECK-LABEL: @shl_xor_vec_splat_poison(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 3)
; CHECK-NEXT: [[S:%.*]] = xor <2 x i8> [[M]], <i8 poison, i8 3>
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i8> splat (i8 5), [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 3, i8 3>, %x
%s = xor <2 x i8> <i8 poison, i8 3>, %m
%r = shl <2 x i8> <i8 5, i8 5>, %s
ret <2 x i8> %r
}
define <2 x i8> @shl_xor_vec_non_splat(<2 x i8> %x) {
; CHECK-LABEL: @shl_xor_vec_non_splat(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 3)
; CHECK-NEXT: [[S:%.*]] = xor <2 x i8> [[M]], splat (i8 3)
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i8> <i8 4, i8 5>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 3, i8 3>, %x
%s = xor <2 x i8> <i8 3, i8 3>, %m
%r = shl <2 x i8> <i8 4, i8 5>, %s
ret <2 x i8> %r
}
define i8 @shl_xor_negative_notsub(i8 %x) {
; CHECK-LABEL: @shl_xor_negative_notsub(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[S:%.*]] = xor i8 [[M]], 2
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 2, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 2
%r = shl i8 2, %s
ret i8 %r
}
define i8 @shl_xor_negative_abovemask(i8 %x) {
; CHECK-LABEL: @shl_xor_negative_abovemask(
; CHECK-NEXT: [[S:%.*]] = xor i8 [[X:%.*]], 7
; CHECK-NEXT: [[R:%.*]] = shl i8 2, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = xor i8 %x, 7
%r = shl i8 2, %s
ret i8 %r
}
define i8 @lshr_sub(i8 %x) {
; CHECK-LABEL: @lshr_sub(
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 6, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
%r = lshr i8 48, %s
ret i8 %r
}
define i8 @lshr_sub_neg(i8 %x) {
; CHECK-LABEL: @lshr_sub_neg(
; CHECK-NEXT: [[R:%.*]] = shl nuw i8 24, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
%r = lshr i8 -64, %s
ret i8 %r
}
define i8 @lshr_sub_maxtzero(i8 %x) {
; CHECK-LABEL: @lshr_sub_maxtzero(
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 1, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 5, %x
%r = lshr i8 32, %s
ret i8 %r
}
define i8 @lshr_sub_multiuse(i8 %x) {
; CHECK-LABEL: @lshr_sub_multiuse(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 3, [[X:%.*]]
; CHECK-NEXT: call void @use(i8 [[S]])
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 4, [[X]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
call void @use(i8 %s)
%r = lshr i8 32, %s
ret i8 %r
}
define <2 x i8> @lshr_sub_vec_splat(<2 x i8> %x) {
; CHECK-LABEL: @lshr_sub_vec_splat(
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i8> splat (i8 4), [[X:%.*]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 4>, %x
%r = lshr <2 x i8> <i8 64, i8 64>, %s
ret <2 x i8> %r
}
define <2 x i8> @lshr_sub_vec_splat_poison(<2 x i8> %x) {
; CHECK-LABEL: @lshr_sub_vec_splat_poison(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> <i8 4, i8 poison>, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> <i8 64, i8 poison>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 poison>, %x
%r = lshr <2 x i8> <i8 64, i8 poison>, %s
ret <2 x i8> %r
}
define <2 x i8> @lshr_sub_vec_non_splat(<2 x i8> %x) {
; CHECK-LABEL: @lshr_sub_vec_non_splat(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> <i8 4, i8 1>, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> splat (i8 64), [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 1>, %x
%r = lshr <2 x i8> <i8 64, i8 64>, %s
ret <2 x i8> %r
}
define i8 @lshr_sub_negative_uw(i8 %x) {
; CHECK-LABEL: @lshr_sub_negative_uw(
; CHECK-NEXT: [[S:%.*]] = sub i8 3, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = lshr i8 32, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub i8 3, %x
%r = lshr i8 32, %s
ret i8 %r
}
define i8 @lshr_sub_negative_notrzero(i8 %x) {
; CHECK-LABEL: @lshr_sub_negative_notrzero(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 5, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = lshr i8 16, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 5, %x
%r = lshr i8 16, %s
ret i8 %r
}
define i8 @lshr_xor(i8 %x) {
; CHECK-LABEL: @lshr_xor(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 6
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 1, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 6
%s = xor i8 %m, 7
%r = lshr i8 -128, %s
ret i8 %r
}
define i8 @lshr_xor_neg(i8 %x) {
; CHECK-LABEL: @lshr_xor_neg(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[R:%.*]] = shl nuw i8 28, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 3
%r = lshr i8 -32, %s
ret i8 %r
}
define i8 @lshr_xor_multiuse(i8 %x) {
; CHECK-LABEL: @lshr_xor_multiuse(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 6
; CHECK-NEXT: [[S:%.*]] = xor i8 [[M]], 7
; CHECK-NEXT: call void @use(i8 [[S]])
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 1, [[M]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 6
%s = xor i8 %m, 7
call void @use(i8 %s)
%r = lshr i8 -128, %s
ret i8 %r
}
define <2 x i8> @lshr_xor_vec_splat(<2 x i8> %x) {
; CHECK-LABEL: @lshr_xor_vec_splat(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], <i8 2, i8 3>
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i8> splat (i8 2), [[M]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 2, i8 3>, %x
%s = xor <2 x i8> %m, <i8 3, i8 3>
%r = lshr <2 x i8> <i8 16, i8 16>, %s
ret <2 x i8> %r
}
define <2 x i8> @lshr_xor_vec_splat_poison(<2 x i8> %x) {
; CHECK-LABEL: @lshr_xor_vec_splat_poison(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], <i8 2, i8 3>
; CHECK-NEXT: [[S:%.*]] = xor <2 x i8> [[M]], splat (i8 3)
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i8> <i8 poison, i8 16>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 2, i8 3>, %x
%s = xor <2 x i8> %m, <i8 3, i8 3>
%r = lshr <2 x i8> <i8 poison, i8 16>, %s
ret <2 x i8> %r
}
define <2 x i8> @lshr_xor_vec_non_splat(<2 x i8> %x) {
; CHECK-LABEL: @lshr_xor_vec_non_splat(
; CHECK-NEXT: [[M:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 3)
; CHECK-NEXT: [[S:%.*]] = xor <2 x i8> [[M]], splat (i8 3)
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i8> <i8 32, i8 16>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%m = and <2 x i8> <i8 3, i8 3>, %x
%s = xor <2 x i8> %m, <i8 3, i8 3>
%r = lshr <2 x i8> <i8 32, i8 16>, %s
ret <2 x i8> %r
}
define i8 @lshr_xor_negative_notsub(i8 %x) {
; CHECK-LABEL: @lshr_xor_negative_notsub(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[S:%.*]] = xor i8 [[M]], 2
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 96, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 2
%r = lshr i8 96, %s
ret i8 %r
}
define i8 @lshr_xor_negative_notrzero(i8 %x) {
; CHECK-LABEL: @lshr_xor_negative_notrzero(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 3
; CHECK-NEXT: [[S:%.*]] = xor i8 [[M]], 3
; CHECK-NEXT: [[R:%.*]] = lshr i8 68, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %m, 3
%r = lshr i8 68, %s
ret i8 %r
}
define i8 @ashr_sub(i8 %x) {
; CHECK-LABEL: @ashr_sub(
; CHECK-NEXT: [[R:%.*]] = shl nsw i8 -8, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
%r = ashr i8 -64, %s
ret i8 %r
}
define i8 @ashr_sub_maxtzero(i8 %x) {
; CHECK-LABEL: @ashr_sub_maxtzero(
; CHECK-NEXT: [[R:%.*]] = shl nsw i8 -1, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 6, %x
%r = ashr i8 -64, %s
ret i8 %r
}
define i8 @ashr_sub_multiuse(i8 %x) {
; CHECK-LABEL: @ashr_sub_multiuse(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 3, [[X:%.*]]
; CHECK-NEXT: call void @use(i8 [[S]])
; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i8 12, [[X]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 3, %x
call void @use(i8 %s)
%r = ashr i8 -160, %s
ret i8 %r
}
define <2 x i8> @ashr_sub_vec_splat(<2 x i8> %x) {
; CHECK-LABEL: @ashr_sub_vec_splat(
; CHECK-NEXT: [[R:%.*]] = shl nsw <2 x i8> splat (i8 -4), [[X:%.*]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 4>, %x
%r = ashr <2 x i8> <i8 -64, i8 -64>, %s
ret <2 x i8> %r
}
define <2 x i8> @ashr_sub_vec_splat_poison(<2 x i8> %x) {
; CHECK-LABEL: @ashr_sub_vec_splat_poison(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> <i8 4, i8 poison>, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = ashr <2 x i8> splat (i8 -64), [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 poison>, %x
%r = ashr <2 x i8> <i8 -64, i8 -64>, %s
ret <2 x i8> %r
}
define <2 x i8> @ashr_sub_vec_non_splat(<2 x i8> %x) {
; CHECK-LABEL: @ashr_sub_vec_non_splat(
; CHECK-NEXT: [[S:%.*]] = sub nuw <2 x i8> splat (i8 4), [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = ashr <2 x i8> <i8 64, i8 -64>, [[S]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%s = sub nuw <2 x i8> <i8 4, i8 4>, %x
%r = ashr <2 x i8> <i8 64, i8 -64>, %s
ret <2 x i8> %r
}
define i8 @ashr_sub_negative_notrzero(i8 %x) {
; CHECK-LABEL: @ashr_sub_negative_notrzero(
; CHECK-NEXT: [[S:%.*]] = sub nuw i8 7, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = ashr i8 -64, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = sub nuw i8 7, %x
%r = ashr i8 -64, %s
ret i8 %r
}
define i8 @ashr_xor(i8 %x) {
; CHECK-LABEL: @ashr_xor(
; CHECK-NEXT: [[S:%.*]] = xor i8 [[X:%.*]], 3
; CHECK-NEXT: [[R:%.*]] = ashr i8 -64, [[S]]
; CHECK-NEXT: ret i8 [[R]]
;
%m = and i8 %x, 3
%s = xor i8 %x, 3
%r = ashr i8 -64, %s
ret i8 %r
}