diff options
author | Craig Topper <craig.topper@sifive.com> | 2022-08-05 12:41:28 -0700 |
---|---|---|
committer | Craig Topper <craig.topper@sifive.com> | 2022-08-05 12:47:03 -0700 |
commit | 75c64c7c4e40ec9bc16d90fee46353628d08d62d (patch) | |
tree | 6d722c894f12104934898aae0f6933166ed21dea | |
parent | 4b8db17c32e04a1fe17440a6fb80aa96f31ff068 (diff) |
[RISCV] Don't use li+sh3add for constants that can use lui+add.
If we're adding a constant that can't use addi we try a few tricks,
one of which is using li+sh3add. We should not do this if lui+add
would work. For example adding 8192. Using sh3add prevents folding
a sext.w to form addw, thus increasing instruction count.
-rw-r--r-- | llvm/lib/Target/RISCV/RISCVInstrInfoZb.td | 8 | ||||
-rw-r--r-- | llvm/test/CodeGen/RISCV/rv32zba.ll | 10 | ||||
-rw-r--r-- | llvm/test/CodeGen/RISCV/rv64zba.ll | 22 |
3 files changed, 36 insertions, 4 deletions
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td index 72122d6e8830..1a7539f40d47 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td @@ -237,17 +237,17 @@ 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) && isShiftedInt<12, 2>(C); + // Skip if C is simm12, an lui, or can be optimized by the PatLeaf AddiPair. + return !isInt<13>(C) && !isShiftedInt<20, 12>(C) && isShiftedInt<12, 2>(C); }]>; 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 or + // Skip if C is simm12, an lui or can be optimized by the PatLeaf AddiPair or // CSImm12MulBy4. - return !isInt<14>(C) && isShiftedInt<12, 3>(C); + return !isInt<14>(C) && !isShiftedInt<20, 12>(C) && isShiftedInt<12, 3>(C); }]>; def SimmShiftRightBy2XForm : SDNodeXForm<imm, [{ diff --git a/llvm/test/CodeGen/RISCV/rv32zba.ll b/llvm/test/CodeGen/RISCV/rv32zba.ll index 35b0920552a5..f39fa6a625b8 100644 --- a/llvm/test/CodeGen/RISCV/rv32zba.ll +++ b/llvm/test/CodeGen/RISCV/rv32zba.ll @@ -561,6 +561,16 @@ define i32 @add8208(i32 %a) { ret i32 %c } +define i32 @add8192(i32 %a) { +; CHECK-LABEL: add8192: +; CHECK: # %bb.0: +; CHECK-NEXT: lui a1, 2 +; CHECK-NEXT: add a0, a0, a1 +; CHECK-NEXT: ret + %c = add i32 %a, 8192 + ret i32 %c +} + define i32 @addshl_5_6(i32 %a, i32 %b) { ; RV32I-LABEL: addshl_5_6: ; RV32I: # %bb.0: diff --git a/llvm/test/CodeGen/RISCV/rv64zba.ll b/llvm/test/CodeGen/RISCV/rv64zba.ll index 0ba17412ce8c..cd03b23af9fc 100644 --- a/llvm/test/CodeGen/RISCV/rv64zba.ll +++ b/llvm/test/CodeGen/RISCV/rv64zba.ll @@ -1116,6 +1116,28 @@ define i64 @add8208(i64 %a) { ret i64 %c } +; Make sure we prefer LUI for the 8192 instead of using sh3add. +define signext i32 @add8192_i32(i32 signext %a) { +; CHECK-LABEL: add8192_i32: +; CHECK: # %bb.0: +; CHECK-NEXT: lui a1, 2 +; CHECK-NEXT: addw a0, a0, a1 +; CHECK-NEXT: ret + %c = add i32 %a, 8192 + ret i32 %c +} + +; Make sure we prefer LUI for the 8192 instead of using sh3add. +define i64 @add8192(i64 %a) { +; CHECK-LABEL: add8192: +; CHECK: # %bb.0: +; CHECK-NEXT: lui a1, 2 +; CHECK-NEXT: add a0, a0, a1 +; CHECK-NEXT: ret + %c = add i64 %a, 8192 + ret i64 %c +} + define signext i32 @addshl32_5_6(i32 signext %a, i32 signext %b) { ; RV64I-LABEL: addshl32_5_6: ; RV64I: # %bb.0: |