diff --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td --- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td +++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td @@ -816,6 +816,13 @@ def : PatGprGpr; def : PatGprGpr; def : PatGprImm; + +foreach Idx = 1...3 in { + defvar ShamtA = !mul(8, Idx); + defvar ShamtB = !mul(8, !sub(4, Idx)); + def : Pat<(or (shl GPR:$rk, (i32 ShamtA)), (srl GPR:$rj, (i32 ShamtB))), + (BYTEPICK_W GPR:$rj, GPR:$rk, Idx)>; +} } // Predicates = [IsLA32] let Predicates = [IsLA64] in { @@ -851,6 +858,16 @@ def : Pat<(i64 (mul (loongarch_bstrpick GPR:$rj, (i64 31), (i64 0)), (loongarch_bstrpick GPR:$rk, (i64 31), (i64 0)))), (MULW_D_WU GPR:$rj, GPR:$rk)>; + +foreach Idx = 1...7 in { + defvar ShamtA = !mul(8, Idx); + defvar ShamtB = !mul(8, !sub(8, Idx)); + def : Pat<(or (shl GPR:$rk, (i64 ShamtA)), (srl GPR:$rj, (i64 ShamtB))), + (BYTEPICK_D GPR:$rj, GPR:$rk, Idx)>; + def : Pat<(sext_inreg (or (shl GPR:$rj, (i64 ShamtA)), + (srl GPR:$rk, (i64 ShamtB))), i32), + (BYTEPICK_W GPR:$rj, GPR:$rk, Idx)>; +} } // Predicates = [IsLA64] def : PatGprGpr; diff --git a/llvm/test/CodeGen/LoongArch/bytepick.ll b/llvm/test/CodeGen/LoongArch/bytepick.ll --- a/llvm/test/CodeGen/LoongArch/bytepick.ll +++ b/llvm/test/CodeGen/LoongArch/bytepick.ll @@ -9,9 +9,7 @@ define i32 @pick_i32_1(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_1: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 8 -; LA32-NEXT: srli.w $a1, $a1, 24 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 1 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_1: @@ -31,9 +29,7 @@ define signext i32 @pick_i32_1_sext(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_1_sext: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 8 -; LA32-NEXT: srli.w $a1, $a1, 24 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 1 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_1_sext: @@ -54,9 +50,7 @@ define i32 @pick_i32_2(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_2: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 16 -; LA32-NEXT: srli.w $a1, $a1, 16 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_2: @@ -76,9 +70,7 @@ define signext i32 @pick_i32_2_sext(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_2_sext: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 16 -; LA32-NEXT: srli.w $a1, $a1, 16 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_2_sext: @@ -99,9 +91,7 @@ define i32 @pick_i32_3(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_3: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 24 -; LA32-NEXT: srli.w $a1, $a1, 8 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 3 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_3: @@ -121,9 +111,7 @@ define signext i32 @pick_i32_3_sext(i32 %a, i32 %b) { ; LA32-LABEL: pick_i32_3_sext: ; LA32: # %bb.0: -; LA32-NEXT: slli.w $a0, $a0, 24 -; LA32-NEXT: srli.w $a1, $a1, 8 -; LA32-NEXT: or $a0, $a1, $a0 +; LA32-NEXT: bytepick.w $a0, $a1, $a0, 3 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i32_3_sext: @@ -144,20 +132,14 @@ define i64 @pick_i64_1(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_1: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a2, $a3, 24 -; LA32-NEXT: slli.w $a3, $a0, 8 -; LA32-NEXT: or $a2, $a2, $a3 -; LA32-NEXT: srli.w $a0, $a0, 24 -; LA32-NEXT: slli.w $a1, $a1, 8 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a3, $a0, 1 +; LA32-NEXT: bytepick.w $a1, $a0, $a1, 1 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_1: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 8 -; LA64-NEXT: srli.d $a1, $a1, 56 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 1 ; LA64-NEXT: ret %1 = lshr i64 %b, 56 %2 = shl i64 %a, 8 @@ -170,20 +152,14 @@ define i64 @pick_i64_2(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_2: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a2, $a3, 16 -; LA32-NEXT: slli.w $a3, $a0, 16 -; LA32-NEXT: or $a2, $a2, $a3 -; LA32-NEXT: srli.w $a0, $a0, 16 -; LA32-NEXT: slli.w $a1, $a1, 16 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a3, $a0, 2 +; LA32-NEXT: bytepick.w $a1, $a0, $a1, 2 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_2: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 16 -; LA64-NEXT: srli.d $a1, $a1, 48 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 2 ; LA64-NEXT: ret %1 = lshr i64 %b, 48 %2 = shl i64 %a, 16 @@ -196,20 +172,14 @@ define i64 @pick_i64_3(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_3: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a2, $a3, 8 -; LA32-NEXT: slli.w $a3, $a0, 24 -; LA32-NEXT: or $a2, $a2, $a3 -; LA32-NEXT: srli.w $a0, $a0, 8 -; LA32-NEXT: slli.w $a1, $a1, 24 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a3, $a0, 3 +; LA32-NEXT: bytepick.w $a1, $a0, $a1, 3 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_3: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 24 -; LA64-NEXT: srli.d $a1, $a1, 40 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 3 ; LA64-NEXT: ret %1 = lshr i64 %b, 40 %2 = shl i64 %a, 24 @@ -228,9 +198,7 @@ ; ; LA64-LABEL: pick_i64_4: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 32 -; LA64-NEXT: srli.d $a1, $a1, 32 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 4 ; LA64-NEXT: ret %1 = lshr i64 %b, 32 %2 = shl i64 %a, 32 @@ -243,20 +211,14 @@ define i64 @pick_i64_5(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_5: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a1, $a2, 24 -; LA32-NEXT: slli.w $a2, $a3, 8 -; LA32-NEXT: or $a2, $a1, $a2 -; LA32-NEXT: slli.w $a0, $a0, 8 -; LA32-NEXT: srli.w $a1, $a3, 24 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a2, $a3, 1 +; LA32-NEXT: bytepick.w $a1, $a3, $a0, 1 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_5: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 40 -; LA64-NEXT: srli.d $a1, $a1, 24 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 5 ; LA64-NEXT: ret %1 = lshr i64 %b, 24 %2 = shl i64 %a,40 @@ -269,20 +231,14 @@ define i64 @pick_i64_6(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_6: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a1, $a2, 16 -; LA32-NEXT: slli.w $a2, $a3, 16 -; LA32-NEXT: or $a2, $a1, $a2 -; LA32-NEXT: slli.w $a0, $a0, 16 -; LA32-NEXT: srli.w $a1, $a3, 16 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a2, $a3, 2 +; LA32-NEXT: bytepick.w $a1, $a3, $a0, 2 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_6: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 48 -; LA64-NEXT: srli.d $a1, $a1, 16 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 6 ; LA64-NEXT: ret %1 = lshr i64 %b, 16 %2 = shl i64 %a, 48 @@ -295,20 +251,14 @@ define i64 @pick_i64_7(i64 %a, i64 %b) { ; LA32-LABEL: pick_i64_7: ; LA32: # %bb.0: -; LA32-NEXT: srli.w $a1, $a2, 8 -; LA32-NEXT: slli.w $a2, $a3, 24 -; LA32-NEXT: or $a2, $a1, $a2 -; LA32-NEXT: slli.w $a0, $a0, 24 -; LA32-NEXT: srli.w $a1, $a3, 8 -; LA32-NEXT: or $a1, $a1, $a0 +; LA32-NEXT: bytepick.w $a2, $a2, $a3, 3 +; LA32-NEXT: bytepick.w $a1, $a3, $a0, 3 ; LA32-NEXT: move $a0, $a2 ; LA32-NEXT: ret ; ; LA64-LABEL: pick_i64_7: ; LA64: # %bb.0: -; LA64-NEXT: slli.d $a0, $a0, 56 -; LA64-NEXT: srli.d $a1, $a1, 8 -; LA64-NEXT: or $a0, $a1, $a0 +; LA64-NEXT: bytepick.d $a0, $a1, $a0, 7 ; LA64-NEXT: ret %1 = lshr i64 %b, 8 %2 = shl i64 %a, 56