Skip to content

Commit 4e0008d

Browse files
committed
Revert "[InstCombine] try to narrow shifted bswap-of-zext"
This reverts commit 9e9bda2. This causes a backend error when building the Linux kernel for arm64. See https://reviews.llvm.org/D122166 for a simplified reproducer.
1 parent 27bd8f9 commit 4e0008d

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,21 +1173,6 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
11731173
MulC->logBase2() == ShAmtC)
11741174
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
11751175

1176-
// Try to narrow a bswap:
1177-
// (bswap (zext X)) >> C --> zext (bswap X >> C')
1178-
// In the case where the shift amount equals the bitwidth difference, the
1179-
// shift is eliminated.
1180-
if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::bswap>(
1181-
m_OneUse(m_ZExt(m_Value(X))))))) {
1182-
// TODO: If the shift amount is less than the zext, we could shift left.
1183-
unsigned WidthDiff = BitWidth - X->getType()->getScalarSizeInBits();
1184-
if (ShAmtC >= WidthDiff) {
1185-
Value *NarrowSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
1186-
Value *NewShift = Builder.CreateLShr(NarrowSwap, ShAmtC - WidthDiff);
1187-
return new ZExtInst(NewShift, Ty);
1188-
}
1189-
}
1190-
11911176
// If the shifted-out value is known-zero, then this is an exact shift.
11921177
if (!I.isExact() &&
11931178
MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmtC), 0, &I)) {

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,9 @@ define i1 @icmp_sge(i32 %x, i32 %y) {
831831

832832
define i32 @narrow_bswap(i16 %x) {
833833
; CHECK-LABEL: @narrow_bswap(
834-
; CHECK-NEXT: [[TMP1:%.*]] = call i16 @llvm.bswap.i16(i16 [[X:%.*]])
835-
; CHECK-NEXT: [[S:%.*]] = zext i16 [[TMP1]] to i32
834+
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
835+
; CHECK-NEXT: [[B:%.*]] = call i32 @llvm.bswap.i32(i32 [[Z]])
836+
; CHECK-NEXT: [[S:%.*]] = lshr exact i32 [[B]], 16
836837
; CHECK-NEXT: ret i32 [[S]]
837838
;
838839
%z = zext i16 %x to i32
@@ -843,8 +844,9 @@ define i32 @narrow_bswap(i16 %x) {
843844

844845
define i128 @narrow_bswap_extra_wide(i16 %x) {
845846
; CHECK-LABEL: @narrow_bswap_extra_wide(
846-
; CHECK-NEXT: [[TMP1:%.*]] = call i16 @llvm.bswap.i16(i16 [[X:%.*]])
847-
; CHECK-NEXT: [[S:%.*]] = zext i16 [[TMP1]] to i128
847+
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i128
848+
; CHECK-NEXT: [[B:%.*]] = call i128 @llvm.bswap.i128(i128 [[Z]])
849+
; CHECK-NEXT: [[S:%.*]] = lshr exact i128 [[B]], 112
848850
; CHECK-NEXT: ret i128 [[S]]
849851
;
850852
%z = zext i16 %x to i128
@@ -853,8 +855,6 @@ define i128 @narrow_bswap_extra_wide(i16 %x) {
853855
ret i128 %s
854856
}
855857

856-
; TODO: The bswap can be narrowed followed by shl.
857-
858858
define i32 @narrow_bswap_undershift(i16 %x) {
859859
; CHECK-LABEL: @narrow_bswap_undershift(
860860
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
@@ -870,8 +870,9 @@ define i32 @narrow_bswap_undershift(i16 %x) {
870870

871871
define <2 x i64> @narrow_bswap_splat(<2 x i16> %x) {
872872
; CHECK-LABEL: @narrow_bswap_splat(
873-
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[X:%.*]])
874-
; CHECK-NEXT: [[S:%.*]] = zext <2 x i16> [[TMP1]] to <2 x i64>
873+
; CHECK-NEXT: [[Z:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i64>
874+
; CHECK-NEXT: [[B:%.*]] = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> [[Z]])
875+
; CHECK-NEXT: [[S:%.*]] = lshr exact <2 x i64> [[B]], <i64 48, i64 48>
875876
; CHECK-NEXT: ret <2 x i64> [[S]]
876877
;
877878
%z = zext <2 x i16> %x to <2 x i64>
@@ -880,8 +881,6 @@ define <2 x i64> @narrow_bswap_splat(<2 x i16> %x) {
880881
ret <2 x i64> %s
881882
}
882883

883-
; TODO: poison/undef in the shift amount is ok to propagate.
884-
885884
define <2 x i64> @narrow_bswap_splat_poison_elt(<2 x i16> %x) {
886885
; CHECK-LABEL: @narrow_bswap_splat_poison_elt(
887886
; CHECK-NEXT: [[Z:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i64>
@@ -897,9 +896,9 @@ define <2 x i64> @narrow_bswap_splat_poison_elt(<2 x i16> %x) {
897896

898897
define <2 x i64> @narrow_bswap_overshift(<2 x i32> %x) {
899898
; CHECK-LABEL: @narrow_bswap_overshift(
900-
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.bswap.v2i32(<2 x i32> [[X:%.*]])
901-
; CHECK-NEXT: [[TMP2:%.*]] = lshr <2 x i32> [[TMP1]], <i32 16, i32 16>
902-
; CHECK-NEXT: [[S:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64>
899+
; CHECK-NEXT: [[Z:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
900+
; CHECK-NEXT: [[B:%.*]] = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> [[Z]])
901+
; CHECK-NEXT: [[S:%.*]] = lshr <2 x i64> [[B]], <i64 48, i64 48>
903902
; CHECK-NEXT: ret <2 x i64> [[S]]
904903
;
905904
%z = zext <2 x i32> %x to <2 x i64>
@@ -910,9 +909,9 @@ define <2 x i64> @narrow_bswap_overshift(<2 x i32> %x) {
910909

911910
define i128 @narrow_bswap_overshift2(i96 %x) {
912911
; CHECK-LABEL: @narrow_bswap_overshift2(
913-
; CHECK-NEXT: [[TMP1:%.*]] = call i96 @llvm.bswap.i96(i96 [[X:%.*]])
914-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i96 [[TMP1]], 29
915-
; CHECK-NEXT: [[S:%.*]] = zext i96 [[TMP2]] to i128
912+
; CHECK-NEXT: [[Z:%.*]] = zext i96 [[X:%.*]] to i128
913+
; CHECK-NEXT: [[B:%.*]] = call i128 @llvm.bswap.i128(i128 [[Z]])
914+
; CHECK-NEXT: [[S:%.*]] = lshr i128 [[B]], 61
916915
; CHECK-NEXT: ret i128 [[S]]
917916
;
918917
%z = zext i96 %x to i128

0 commit comments

Comments
 (0)