diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoB.td b/llvm/lib/Target/RISCV/RISCVInstrInfoB.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfoB.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoB.td @@ -186,6 +186,32 @@ return C > 9 && ((C % 9) == 0) && isPowerOf2_64(C / 9); }]>; +def CSImm12MulBy4 : PatLeaf<(imm), [{ + if (!N->hasOneUse()) + return false; + int64_t C = N->getSExtValue(); + // Skip if C is simm12 or can be optimized by the PatLeaf AddiPair. + return !isInt<13>(C) && isInt<14>(C) && (C & 3) == 0; +}]>; + +def CSImm12MulBy8 : PatLeaf<(imm), [{ + if (!N->hasOneUse()) + return false; + int64_t C = N->getSExtValue(); + // Skip if C is simm12 or can be optimized by the PatLeaf AddiPair. + return !isInt<13>(C) && isInt<15>(C) && (C & 7) == 0; +}]>; + +def SimmShiftRightBy2XForm : SDNodeXFormgetTargetConstant(N->getSExtValue() >> 2, SDLoc(N), + N->getValueType(0)); +}]>; + +def SimmShiftRightBy3XForm : SDNodeXFormgetTargetConstant(N->getSExtValue() >> 3, SDLoc(N), + N->getValueType(0)); +}]>; + //===----------------------------------------------------------------------===// // Instruction class templates //===----------------------------------------------------------------------===// @@ -1011,6 +1037,13 @@ def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 72)), GPR:$rs2), (SH3ADD (SH3ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>; +def : Pat<(add GPR:$r, CSImm12MulBy4:$i), + (SH2ADD (ADDI X0, (SimmShiftRightBy2XForm CSImm12MulBy4:$i)), + GPR:$r)>; +def : Pat<(add GPR:$r, CSImm12MulBy8:$i), + (SH3ADD (ADDI X0, (SimmShiftRightBy3XForm CSImm12MulBy8:$i)), + GPR:$r)>; + def : Pat<(mul GPR:$r, C3LeftShift:$i), (SLLI (SH1ADD GPR:$r, GPR:$r), (TrailingZerosXForm C3LeftShift:$i))>; diff --git a/llvm/test/CodeGen/RISCV/rv32zba.ll b/llvm/test/CodeGen/RISCV/rv32zba.ll --- a/llvm/test/CodeGen/RISCV/rv32zba.ll +++ b/llvm/test/CodeGen/RISCV/rv32zba.ll @@ -750,16 +750,14 @@ ; ; RV32B-LABEL: add4104: ; RV32B: # %bb.0: -; RV32B-NEXT: lui a1, 1 -; RV32B-NEXT: addi a1, a1, 8 -; RV32B-NEXT: add a0, a0, a1 +; RV32B-NEXT: addi a1, zero, 1026 +; RV32B-NEXT: sh2add a0, a1, a0 ; RV32B-NEXT: ret ; ; RV32ZBA-LABEL: add4104: ; RV32ZBA: # %bb.0: -; RV32ZBA-NEXT: lui a1, 1 -; RV32ZBA-NEXT: addi a1, a1, 8 -; RV32ZBA-NEXT: add a0, a0, a1 +; RV32ZBA-NEXT: addi a1, zero, 1026 +; RV32ZBA-NEXT: sh2add a0, a1, a0 ; RV32ZBA-NEXT: ret %c = add i32 %a, 4104 ret i32 %c @@ -775,16 +773,14 @@ ; ; RV32B-LABEL: add8208: ; RV32B: # %bb.0: -; RV32B-NEXT: lui a1, 2 -; RV32B-NEXT: addi a1, a1, 16 -; RV32B-NEXT: add a0, a0, a1 +; RV32B-NEXT: addi a1, zero, 1026 +; RV32B-NEXT: sh3add a0, a1, a0 ; RV32B-NEXT: ret ; ; RV32ZBA-LABEL: add8208: ; RV32ZBA: # %bb.0: -; RV32ZBA-NEXT: lui a1, 2 -; RV32ZBA-NEXT: addi a1, a1, 16 -; RV32ZBA-NEXT: add a0, a0, a1 +; RV32ZBA-NEXT: addi a1, zero, 1026 +; RV32ZBA-NEXT: sh3add a0, a1, a0 ; RV32ZBA-NEXT: ret %c = add i32 %a, 8208 ret i32 %c diff --git a/llvm/test/CodeGen/RISCV/rv64zba.ll b/llvm/test/CodeGen/RISCV/rv64zba.ll --- a/llvm/test/CodeGen/RISCV/rv64zba.ll +++ b/llvm/test/CodeGen/RISCV/rv64zba.ll @@ -1321,16 +1321,14 @@ ; ; RV64B-LABEL: add4104: ; RV64B: # %bb.0: -; RV64B-NEXT: lui a1, 1 -; RV64B-NEXT: addiw a1, a1, 8 -; RV64B-NEXT: add a0, a0, a1 +; RV64B-NEXT: addi a1, zero, 1026 +; RV64B-NEXT: sh2add a0, a1, a0 ; RV64B-NEXT: ret ; ; RV64ZBA-LABEL: add4104: ; RV64ZBA: # %bb.0: -; RV64ZBA-NEXT: lui a1, 1 -; RV64ZBA-NEXT: addiw a1, a1, 8 -; RV64ZBA-NEXT: add a0, a0, a1 +; RV64ZBA-NEXT: addi a1, zero, 1026 +; RV64ZBA-NEXT: sh2add a0, a1, a0 ; RV64ZBA-NEXT: ret %c = add i64 %a, 4104 ret i64 %c @@ -1346,16 +1344,14 @@ ; ; RV64B-LABEL: add8208: ; RV64B: # %bb.0: -; RV64B-NEXT: lui a1, 2 -; RV64B-NEXT: addiw a1, a1, 16 -; RV64B-NEXT: add a0, a0, a1 +; RV64B-NEXT: addi a1, zero, 1026 +; RV64B-NEXT: sh3add a0, a1, a0 ; RV64B-NEXT: ret ; ; RV64ZBA-LABEL: add8208: ; RV64ZBA: # %bb.0: -; RV64ZBA-NEXT: lui a1, 2 -; RV64ZBA-NEXT: addiw a1, a1, 16 -; RV64ZBA-NEXT: add a0, a0, a1 +; RV64ZBA-NEXT: addi a1, zero, 1026 +; RV64ZBA-NEXT: sh3add a0, a1, a0 ; RV64ZBA-NEXT: ret %c = add i64 %a, 8208 ret i64 %c