Skip to content

Commit 734c213

Browse files
committed
[LoongArch] Optimize multiplication with immediates
Optimize multiplication with some specific immediates to a pair of `alsl`. Reviewed By: xen0n, SixWeining Differential Revision: https://reviews.llvm.org/D147305
1 parent 918209b commit 734c213

File tree

2 files changed

+124
-105
lines changed

2 files changed

+124
-105
lines changed

llvm/lib/Target/LoongArch/LoongArchInstrInfo.td

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,13 @@ class shiftopw<SDPatternOperator operator>
859859
: PatFrag<(ops node:$val, node:$count),
860860
(operator node:$val, (i64 (shiftMask32 node:$count)))>;
861861

862+
def mul_const_oneuse : PatFrag<(ops node:$A, node:$B),
863+
(mul node:$A, node:$B), [{
864+
if (auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
865+
return N1C->hasOneUse();
866+
return false;
867+
}]>;
868+
862869
let Predicates = [IsLA32] in {
863870
def : PatGprGpr<add, ADD_W>;
864871
def : PatGprImm<add, ADDI_W, simm12>;
@@ -939,6 +946,31 @@ def : Pat<(sext_inreg (add GPR:$rj, (AddiPair:$im)), i32),
939946
(AddiPairImmSmall AddiPair:$im))>;
940947
} // Predicates = [IsLA64]
941948

949+
let Predicates = [IsLA32] in {
950+
foreach Idx0 = 1...4 in {
951+
foreach Idx1 = 1...4 in {
952+
defvar CImm = !add(1, !shl(!add(1, !shl(1, Idx0)), Idx1));
953+
def : Pat<(mul_const_oneuse GPR:$r, (i32 CImm)),
954+
(ALSL_W (ALSL_W GPR:$r, GPR:$r, (i32 Idx0)),
955+
GPR:$r, (i32 Idx1))>;
956+
}
957+
}
958+
} // Predicates = [IsLA32]
959+
960+
let Predicates = [IsLA64] in {
961+
foreach Idx0 = 1...4 in {
962+
foreach Idx1 = 1...4 in {
963+
defvar CImm = !add(1, !shl(!add(1, !shl(1, Idx0)), Idx1));
964+
def : Pat<(sext_inreg (mul_const_oneuse GPR:$r, (i64 CImm)), i32),
965+
(ALSL_W (ALSL_W GPR:$r, GPR:$r, (i64 Idx0)),
966+
GPR:$r, (i64 Idx1))>;
967+
def : Pat<(mul_const_oneuse GPR:$r, (i64 CImm)),
968+
(ALSL_D (ALSL_D GPR:$r, GPR:$r, (i64 Idx0)),
969+
GPR:$r, (i64 Idx1))>;
970+
}
971+
}
972+
} // Predicates = [IsLA64]
973+
942974
foreach Idx = 1...7 in {
943975
defvar ShamtA = !mul(8, Idx);
944976
defvar ShamtB = !mul(8, !sub(8, Idx));

0 commit comments

Comments
 (0)