[llvm] 60ed86d - [RISCV] Optimize addition with immediate
Ben Shi via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 25 22:26:25 PDT 2021
Author: Ben Shi
Date: 2021-04-26T13:26:17+08:00
New Revision: 60ed86d3507bf908e0079f78b246ae096321ae03
URL: https://github.com/llvm/llvm-project/commit/60ed86d3507bf908e0079f78b246ae096321ae03
DIFF: https://github.com/llvm/llvm-project/commit/60ed86d3507bf908e0079f78b246ae096321ae03.diff
LOG: [RISCV] Optimize addition with immediate
Reviewed by: craig.topper
Differential Revision: https://reviews.llvm.org/D101244
Added:
Modified:
llvm/lib/Target/RISCV/RISCVInstrInfo.td
llvm/test/CodeGen/RISCV/add-imm.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.td b/llvm/lib/Target/RISCV/RISCVInstrInfo.td
index ed31e1dee0d3..d1fcf4714281 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.td
@@ -885,6 +885,10 @@ def SLLIUWPat : PatFrag<(ops node:$A, node:$B),
return MatchSLLIUW(N);
}]>;
+def add_oneuse : PatFrag<(ops node:$A, node:$B), (add node:$A, node:$B), [{
+ return N->hasOneUse();
+}]>;
+
/// Simple arithmetic operations
def : PatGprGpr<add, ADD>;
@@ -1299,6 +1303,12 @@ def : Pat<(add GPR:$rs1, (AddiPair GPR:$rs2)),
(ADDI (ADDI GPR:$rs1, (AddiPairImmB GPR:$rs2)),
(AddiPairImmA GPR:$rs2))>;
+let Predicates = [IsRV64] in {
+def : Pat<(sext_inreg (add_oneuse GPR:$rs1, (AddiPair GPR:$rs2)), i32),
+ (ADDIW (ADDIW GPR:$rs1, (AddiPairImmB GPR:$rs2)),
+ (AddiPairImmA GPR:$rs2))>;
+}
+
//===----------------------------------------------------------------------===//
// Standard extensions
//===----------------------------------------------------------------------===//
diff --git a/llvm/test/CodeGen/RISCV/add-imm.ll b/llvm/test/CodeGen/RISCV/add-imm.ll
index 548a2e2579b8..37231aae4454 100644
--- a/llvm/test/CodeGen/RISCV/add-imm.ll
+++ b/llvm/test/CodeGen/RISCV/add-imm.ll
@@ -159,14 +159,37 @@ define signext i32 @add32_sext_accept(i32 signext %a) nounwind {
;
; RV64I-LABEL: add32_sext_accept:
; RV64I: # %bb.0:
-; RV64I-NEXT: lui a1, 1
-; RV64I-NEXT: addiw a1, a1, -1097
-; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: addiw a0, a0, 1500
+; RV64I-NEXT: addiw a0, a0, 1499
; RV64I-NEXT: ret
%1 = add i32 %a, 2999
ret i32 %1
}
+ at gv0 = global i32 0, align 4
+define signext i32 @add32_sext_reject_on_rv64(i32 signext %a) nounwind {
+; RV32I-LABEL: add32_sext_reject_on_rv64:
+; RV32I: # %bb.0:
+; RV32I-NEXT: addi a0, a0, 1500
+; RV32I-NEXT: addi a0, a0, 1500
+; RV32I-NEXT: lui a1, %hi(gv0)
+; RV32I-NEXT: sw a0, %lo(gv0)(a1)
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: add32_sext_reject_on_rv64:
+; RV64I: # %bb.0:
+; RV64I-NEXT: lui a1, 1
+; RV64I-NEXT: addiw a1, a1, -1096
+; RV64I-NEXT: add a2, a0, a1
+; RV64I-NEXT: lui a3, %hi(gv0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: sw a2, %lo(gv0)(a3)
+; RV64I-NEXT: ret
+ %b = add nsw i32 %a, 3000
+ store i32 %b, i32* @gv0, align 4
+ ret i32 %b
+}
+
define i64 @add64_accept(i64 %a) nounwind {
; RV32I-LABEL: add64_accept:
; RV32I: # %bb.0:
More information about the llvm-commits
mailing list