[llvm] [InstCombine] Convert fshl(x, 0, y) to shl(x, and(y, BitWidth - 1)) when BitWidth is pow2 (PR #122362)
Amr Hesham via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 10 09:56:42 PST 2025
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/122362
>From fc1a935f71a68bd512fe93176cd3681a1b2b20fd Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 10 Jan 2025 18:47:04 +0100
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests
---
llvm/test/Transforms/InstCombine/fsh.ll | 44 +++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/fsh.ll b/llvm/test/Transforms/InstCombine/fsh.ll
index 434cd810296d8c..2cbfa94a04f265 100644
--- a/llvm/test/Transforms/InstCombine/fsh.ll
+++ b/llvm/test/Transforms/InstCombine/fsh.ll
@@ -1010,3 +1010,47 @@ define <2 x i32> @fshr_vec_zero_elem(<2 x i32> %x, <2 x i32> %y) {
%fsh = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 2, i32 0>)
ret <2 x i32> %fsh
}
+
+define i16 @fshl_i16_shl(i16 %x, i16 %y) {
+; CHECK-LABEL: @fshl_i16_shl(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[RES:%.*]] = call i16 @llvm.fshl.i16(i16 [[X:%.*]], i16 0, i16 [[Y:%.*]])
+; CHECK-NEXT: ret i16 [[RES]]
+;
+entry:
+ %res = call i16 @llvm.fshl.i16(i16 %x, i16 0, i16 %y)
+ ret i16 %res
+}
+
+define i32 @fshl_i32_shl(i32 %x, i32 %y) {
+; CHECK-LABEL: @fshl_i32_shl(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 0, i32 [[Y:%.*]])
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ %res = call i32 @llvm.fshl.i32(i32 %x, i32 0, i32 %y)
+ ret i32 %res
+}
+
+define <2 x i16> @fshl_vi16_shl(<2 x i16> %x, <2 x i16> %y) {
+; CHECK-LABEL: @fshl_vi16_shl(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[RES:%.*]] = call <2 x i16> @llvm.fshl.v2i16(<2 x i16> [[X:%.*]], <2 x i16> zeroinitializer, <2 x i16> [[Y:%.*]])
+; CHECK-NEXT: ret <2 x i16> [[RES]]
+;
+entry:
+ %res = call <2 x i16> @llvm.fshl.v2i16(<2 x i16> %x, <2 x i16> <i16 0, i16 0>, <2 x i16> %y)
+ ret <2 x i16> %res
+}
+
+define <2 x i31> @fshl_vi31_shl(<2 x i31> %x, <2 x i31> %y) {
+; CHECK-LABEL: @fshl_vi31_shl(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[RES:%.*]] = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> [[X:%.*]], <2 x i31> zeroinitializer, <2 x i31> [[Y:%.*]])
+; CHECK-NEXT: ret <2 x i31> [[RES]]
+;
+entry:
+ %res = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> %x, <2 x i31> <i31 0, i31 0>, <2 x i31> %y)
+ ret <2 x i31> %res
+}
>From 345258dbbb148c9c19ff4ffbd90138b9a9ec872d Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 10 Jan 2025 18:55:40 +0100
Subject: [PATCH 2/2] [InstCombine] Convert fshl(x, 0, y) to shl(x, and(y,
BitWidth - 1)) when BitWidth is pow2
---
.../lib/Transforms/InstCombine/InstCombineCalls.cpp | 8 ++++++++
llvm/test/Transforms/InstCombine/fsh.ll | 13 ++++++++-----
2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index c55c40c88bc845..f0aeb03f83496e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2229,6 +2229,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
return BitOp;
}
+ // fshl(X, 0, Y) --> shl(X, and(Y, BitWidth - 1)) if bitwidth is a
+ // power-of-2
+ if (isPowerOf2_32(BitWidth) && match(Op1, m_ZeroInt())) {
+ Value *Op2 = II->getArgOperand(2);
+ Value *And = Builder.CreateAnd(Op2, ConstantInt::get(Ty, BitWidth - 1));
+ return BinaryOperator::CreateShl(Op0, And);
+ }
+
// Left or right might be masked.
if (SimplifyDemandedInstructionBits(*II))
return &CI;
diff --git a/llvm/test/Transforms/InstCombine/fsh.ll b/llvm/test/Transforms/InstCombine/fsh.ll
index 2cbfa94a04f265..00f507776ae937 100644
--- a/llvm/test/Transforms/InstCombine/fsh.ll
+++ b/llvm/test/Transforms/InstCombine/fsh.ll
@@ -1014,7 +1014,8 @@ define <2 x i32> @fshr_vec_zero_elem(<2 x i32> %x, <2 x i32> %y) {
define i16 @fshl_i16_shl(i16 %x, i16 %y) {
; CHECK-LABEL: @fshl_i16_shl(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call i16 @llvm.fshl.i16(i16 [[X:%.*]], i16 0, i16 [[Y:%.*]])
+; CHECK-NEXT: [[TMP0:%.*]] = and i16 [[Y:%.*]], 15
+; CHECK-NEXT: [[RES:%.*]] = shl i16 [[X:%.*]], [[TMP0]]
; CHECK-NEXT: ret i16 [[RES]]
;
entry:
@@ -1025,7 +1026,8 @@ entry:
define i32 @fshl_i32_shl(i32 %x, i32 %y) {
; CHECK-LABEL: @fshl_i32_shl(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 0, i32 [[Y:%.*]])
+; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y:%.*]], 31
+; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X:%.*]], [[TMP0]]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
@@ -1036,7 +1038,8 @@ entry:
define <2 x i16> @fshl_vi16_shl(<2 x i16> %x, <2 x i16> %y) {
; CHECK-LABEL: @fshl_vi16_shl(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call <2 x i16> @llvm.fshl.v2i16(<2 x i16> [[X:%.*]], <2 x i16> zeroinitializer, <2 x i16> [[Y:%.*]])
+; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i16> [[Y:%.*]], splat (i16 15)
+; CHECK-NEXT: [[RES:%.*]] = shl <2 x i16> [[X:%.*]], [[TMP0]]
; CHECK-NEXT: ret <2 x i16> [[RES]]
;
entry:
@@ -1044,8 +1047,8 @@ entry:
ret <2 x i16> %res
}
-define <2 x i31> @fshl_vi31_shl(<2 x i31> %x, <2 x i31> %y) {
-; CHECK-LABEL: @fshl_vi31_shl(
+define <2 x i31> @fshl_vi31_shl_negative_test(<2 x i31> %x, <2 x i31> %y) {
+; CHECK-LABEL: @fshl_vi31_shl_negative_test(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[RES:%.*]] = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> [[X:%.*]], <2 x i31> zeroinitializer, <2 x i31> [[Y:%.*]])
; CHECK-NEXT: ret <2 x i31> [[RES]]
More information about the llvm-commits
mailing list