Skip to content

Commit 642e493

Browse files
authored
[InstCombine] Convert fshl(x, 0, y) to shl(x, and(y, BitWidth - 1)) when BitWidth is pow2 (#122362)
Convert `fshl(x, 0, y)` to `shl(x, and(y, BitWidth - 1))` when BitWidth is pow2 Alive2 proof: https://alive2.llvm.org/ce/z/3oTEop Fixes: #122235
1 parent 4435b7d commit 642e493

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,15 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
22292229
return BitOp;
22302230
}
22312231

2232+
// fshl(X, 0, Y) --> shl(X, and(Y, BitWidth - 1)) if bitwidth is a
2233+
// power-of-2
2234+
if (IID == Intrinsic::fshl && isPowerOf2_32(BitWidth) &&
2235+
match(Op1, m_ZeroInt())) {
2236+
Value *Op2 = II->getArgOperand(2);
2237+
Value *And = Builder.CreateAnd(Op2, ConstantInt::get(Ty, BitWidth - 1));
2238+
return BinaryOperator::CreateShl(Op0, And);
2239+
}
2240+
22322241
// Left or right might be masked.
22332242
if (SimplifyDemandedInstructionBits(*II))
22342243
return &CI;

llvm/test/Transforms/InstCombine/fsh.ll

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,3 +1010,61 @@ define <2 x i32> @fshr_vec_zero_elem(<2 x i32> %x, <2 x i32> %y) {
10101010
%fsh = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 2, i32 0>)
10111011
ret <2 x i32> %fsh
10121012
}
1013+
1014+
define i16 @fshl_i16_shl(i16 %x, i16 %y) {
1015+
; CHECK-LABEL: @fshl_i16_shl(
1016+
; CHECK-NEXT: entry:
1017+
; CHECK-NEXT: [[TMP0:%.*]] = and i16 [[Y:%.*]], 15
1018+
; CHECK-NEXT: [[RES:%.*]] = shl i16 [[X:%.*]], [[TMP0]]
1019+
; CHECK-NEXT: ret i16 [[RES]]
1020+
;
1021+
entry:
1022+
%res = call i16 @llvm.fshl.i16(i16 %x, i16 0, i16 %y)
1023+
ret i16 %res
1024+
}
1025+
1026+
define i32 @fshl_i32_shl(i32 %x, i32 %y) {
1027+
; CHECK-LABEL: @fshl_i32_shl(
1028+
; CHECK-NEXT: entry:
1029+
; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y:%.*]], 31
1030+
; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X:%.*]], [[TMP0]]
1031+
; CHECK-NEXT: ret i32 [[RES]]
1032+
;
1033+
entry:
1034+
%res = call i32 @llvm.fshl.i32(i32 %x, i32 0, i32 %y)
1035+
ret i32 %res
1036+
}
1037+
1038+
define <2 x i16> @fshl_vi16_shl(<2 x i16> %x, <2 x i16> %y) {
1039+
; CHECK-LABEL: @fshl_vi16_shl(
1040+
; CHECK-NEXT: entry:
1041+
; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i16> [[Y:%.*]], splat (i16 15)
1042+
; CHECK-NEXT: [[RES:%.*]] = shl <2 x i16> [[X:%.*]], [[TMP0]]
1043+
; CHECK-NEXT: ret <2 x i16> [[RES]]
1044+
;
1045+
entry:
1046+
%res = call <2 x i16> @llvm.fshl.v2i16(<2 x i16> %x, <2 x i16> zeroinitializer, <2 x i16> %y)
1047+
ret <2 x i16> %res
1048+
}
1049+
1050+
define i32 @fshr_i32_shl_negative_test(i32 %x, i32 %y) {
1051+
; CHECK-LABEL: @fshr_i32_shl_negative_test(
1052+
; CHECK-NEXT: entry:
1053+
; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 0, i32 [[Y:%.*]])
1054+
; CHECK-NEXT: ret i32 [[RES]]
1055+
;
1056+
entry:
1057+
%res = call i32 @llvm.fshr.i32(i32 %x, i32 0, i32 %y)
1058+
ret i32 %res
1059+
}
1060+
1061+
define <2 x i31> @fshl_vi31_shl_negative_test(<2 x i31> %x, <2 x i31> %y) {
1062+
; CHECK-LABEL: @fshl_vi31_shl_negative_test(
1063+
; CHECK-NEXT: entry:
1064+
; CHECK-NEXT: [[RES:%.*]] = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> [[X:%.*]], <2 x i31> zeroinitializer, <2 x i31> [[Y:%.*]])
1065+
; CHECK-NEXT: ret <2 x i31> [[RES]]
1066+
;
1067+
entry:
1068+
%res = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> %x, <2 x i31> zeroinitializer, <2 x i31> %y)
1069+
ret <2 x i31> %res
1070+
}

0 commit comments

Comments
 (0)