Skip to content

Commit f155039

Browse files
committed
[InstCombine] Convert fshl(x, 0, y) to shl(x, and(y, BitWidth - 1)) when BitWidth is pow2
1 parent b38d52b commit f155039

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,14 @@ 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 (isPowerOf2_32(BitWidth) && match(Op1, m_ZeroInt())) {
2235+
Value *Op2 = II->getArgOperand(2);
2236+
Value *And = Builder.CreateAnd(Op2, ConstantInt::get(Ty, BitWidth - 1));
2237+
return BinaryOperator::CreateShl(Op0, And);
2238+
}
2239+
22322240
// Left or right might be masked.
22332241
if (SimplifyDemandedInstructionBits(*II))
22342242
return &CI;

llvm/test/Transforms/InstCombine/fsh.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ define <2 x i32> @fshr_vec_zero_elem(<2 x i32> %x, <2 x i32> %y) {
10141014
define i16 @fshl_i16_shl(i16 %x, i16 %y) {
10151015
; CHECK-LABEL: @fshl_i16_shl(
10161016
; CHECK-NEXT: entry:
1017-
; CHECK-NEXT: [[RES:%.*]] = call i16 @llvm.fshl.i16(i16 [[X:%.*]], i16 0, i16 [[Y:%.*]])
1017+
; CHECK-NEXT: [[TMP0:%.*]] = and i16 [[Y:%.*]], 15
1018+
; CHECK-NEXT: [[RES:%.*]] = shl i16 [[X:%.*]], [[TMP0]]
10181019
; CHECK-NEXT: ret i16 [[RES]]
10191020
;
10201021
entry:
@@ -1025,7 +1026,8 @@ entry:
10251026
define i32 @fshl_i32_shl(i32 %x, i32 %y) {
10261027
; CHECK-LABEL: @fshl_i32_shl(
10271028
; CHECK-NEXT: entry:
1028-
; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 0, i32 [[Y:%.*]])
1029+
; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y:%.*]], 31
1030+
; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X:%.*]], [[TMP0]]
10291031
; CHECK-NEXT: ret i32 [[RES]]
10301032
;
10311033
entry:
@@ -1036,7 +1038,8 @@ entry:
10361038
define <2 x i16> @fshl_vi16_shl(<2 x i16> %x, <2 x i16> %y) {
10371039
; CHECK-LABEL: @fshl_vi16_shl(
10381040
; CHECK-NEXT: entry:
1039-
; CHECK-NEXT: [[RES:%.*]] = call <2 x i16> @llvm.fshl.v2i16(<2 x i16> [[X:%.*]], <2 x i16> zeroinitializer, <2 x i16> [[Y:%.*]])
1041+
; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i16> [[Y:%.*]], splat (i16 15)
1042+
; CHECK-NEXT: [[RES:%.*]] = shl <2 x i16> [[X:%.*]], [[TMP0]]
10401043
; CHECK-NEXT: ret <2 x i16> [[RES]]
10411044
;
10421045
entry:

0 commit comments

Comments
 (0)