
The logic in RISCVMatInt would previously produce lui+addiw on RV64 whenever a 32-bit integer must be materialised and the Hi20 and Lo12 parts are non-zero. However, sometimes addi can be used equivalently (whenever the sign extension behaviour of addiw would be a no-op). This patch moves to using addiw only when necessary. Although there is absolutely no advantage in terms of compressibility or performance, this has the following advantages: * It's more consistent with logic used elsewhere in the backend. For instance, RISCVOptWInstrs will try to convert addiw to addi on the basis it reduces test diffs vs RV32. * This matches the lowering GCC does in its codegen path. Unlike LLVM, GCC seems to have different expansion logic for the assembler vs codegen. For codegen it will use lui+addi if possible, but expanding `li` in the assembler will always produces lui+addiw as LLVM did prior to this commit. As someone who has been looking at a lot of gcc vs clang diffs lately, reducing unnecessary divergence is of at least some value. * As the diff for fold-mem-offset.ll shows, we can fold memory offsets in more cases when addi is used. Memory offset folding could be taught to recognise when the addiw could be replaced with an addi, but that seems unnecessary when we can simply change the logic in RISCVMatInt. As pointed out by @topperc during review, making this change without modifying RISCVOptWInstrs risks introducing some cases where we fail to remove a sext.w that we removed before. I've incorporated a patch based on a suggestion from Craig that avoids it, and also adds appropriate RISCVOptWInstrs test cases. The initial patch description noted that the main motivation was to avoid unnecessary differences both for RV32/RV64 and when comparing GCC, but noted that very occasionally we see a benefit from memory offset folding kicking in when it didn't before. Looking at the dynamic instruction count difference for SPEC benchmarks targeting rva22u64 and it shows we actually get a meaningful ~4.3% reduction in dynamic icount for 519.lbm_r. Looking at the data more closely, the codegen difference is in `LBM_performStreamCollideTRT` which as a function accounts for ~98% for dynamically executed instructions and the codegen diffs appear to be a knock-on effect of the address merging reducing register pressure right from function entry (for instance, we get a big reduction in dynamically executed loads in that function). Below is the icount data (rva22u64 -O3, no LTO): ``` Benchmark Baseline This PR Diff (%) ============================================================ 500.perlbench_r 174116601991 174115795810 -0.00% 502.gcc_r 218903280858 218903215788 -0.00% 505.mcf_r 131208029185 131207692803 -0.00% 508.namd_r 217497594322 217497594297 -0.00% 510.parest_r 289314486153 289313577652 -0.00% 511.povray_r 30640531048 30640765701 0.00% 519.lbm_r 95897914862 91712688050 -4.36% 520.omnetpp_r 134641549722 134867015683 0.17% 523.xalancbmk_r 281462762992 281432092673 -0.01% 525.x264_r 379776121941 379535558210 -0.06% 526.blender_r 659736022025 659738387343 0.00% 531.deepsjeng_r 349122867552 349122867481 -0.00% 538.imagick_r 238558760552 238558753269 -0.00% 541.leela_r 406578560612 406385135260 -0.05% 544.nab_r 400997131674 400996765827 -0.00% 557.xz_r 130079522194 129945515709 -0.10% ``` The instcounting setup I use doesn't have good support for drilling down into functions from outside the linked executable (e.g. libc). The difference in omnetpp all seems to come from there, and does not reflect any degradation in codegen quality. I can confirm with the current version of the PR there is no change in the number of static sext.w across all the SPEC 2017 benchmarks (rva22u64 O3) Co-authored-by: Craig Topper <craig.topper@sifive.com>
162 lines
5.2 KiB
LLVM
162 lines
5.2 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
|
|
; RUN: llc -mtriple=riscv32 -mattr=+zfh -verify-machineinstrs \
|
|
; RUN: -target-abi ilp32f < %s | FileCheck %s
|
|
; RUN: llc -mtriple=riscv64 -mattr=+zfh -verify-machineinstrs \
|
|
; RUN: -target-abi lp64f < %s | FileCheck %s
|
|
; RUN: llc -mtriple=riscv32 -mattr=+zhinx -verify-machineinstrs \
|
|
; RUN: -target-abi ilp32 < %s \
|
|
; RUN: | FileCheck -check-prefix=RV32IZHINX %s
|
|
; RUN: llc -mtriple=riscv64 -mattr=+zhinx -verify-machineinstrs \
|
|
; RUN: -target-abi lp64 < %s \
|
|
; RUN: | FileCheck -check-prefix=RV64IZHINX %s
|
|
; RUN: llc -mtriple=riscv32 -mattr=+zfhmin -verify-machineinstrs \
|
|
; RUN: -target-abi ilp32f < %s | FileCheck -check-prefixes=CHECKIZFHMIN %s
|
|
; RUN: llc -mtriple=riscv64 -mattr=+zfhmin -verify-machineinstrs \
|
|
; RUN: -target-abi lp64f < %s | FileCheck -check-prefixes=CHECKIZFHMIN %s
|
|
; RUN: llc -mtriple=riscv32 -mattr=+zhinxmin -verify-machineinstrs \
|
|
; RUN: -target-abi ilp32 < %s \
|
|
; RUN: | FileCheck -check-prefixes=CHECKIZHINXMIN,RV32IZHINXMIN %s
|
|
; RUN: llc -mtriple=riscv64 -mattr=+zhinxmin -verify-machineinstrs \
|
|
; RUN: -target-abi lp64 < %s \
|
|
; RUN: | FileCheck -check-prefixes=CHECKIZHINXMIN,RV64IZHINXMIN %s
|
|
|
|
; TODO: constant pool shouldn't be necessary for RV32IZfh and RV64IZfh
|
|
define half @half_imm() nounwind {
|
|
; CHECK-LABEL: half_imm:
|
|
; CHECK: # %bb.0:
|
|
; CHECK-NEXT: lui a0, %hi(.LCPI0_0)
|
|
; CHECK-NEXT: flh fa0, %lo(.LCPI0_0)(a0)
|
|
; CHECK-NEXT: ret
|
|
;
|
|
; RV32IZHINX-LABEL: half_imm:
|
|
; RV32IZHINX: # %bb.0:
|
|
; RV32IZHINX-NEXT: lui a0, 4
|
|
; RV32IZHINX-NEXT: addi a0, a0, 512
|
|
; RV32IZHINX-NEXT: # kill: def $x10_h killed $x10_h killed $x10
|
|
; RV32IZHINX-NEXT: ret
|
|
;
|
|
; RV64IZHINX-LABEL: half_imm:
|
|
; RV64IZHINX: # %bb.0:
|
|
; RV64IZHINX-NEXT: lui a0, 4
|
|
; RV64IZHINX-NEXT: addi a0, a0, 512
|
|
; RV64IZHINX-NEXT: # kill: def $x10_h killed $x10_h killed $x10
|
|
; RV64IZHINX-NEXT: ret
|
|
;
|
|
; CHECKIZFHMIN-LABEL: half_imm:
|
|
; CHECKIZFHMIN: # %bb.0:
|
|
; CHECKIZFHMIN-NEXT: lui a0, %hi(.LCPI0_0)
|
|
; CHECKIZFHMIN-NEXT: flh fa0, %lo(.LCPI0_0)(a0)
|
|
; CHECKIZFHMIN-NEXT: ret
|
|
;
|
|
; CHECKIZHINXMIN-LABEL: half_imm:
|
|
; CHECKIZHINXMIN: # %bb.0:
|
|
; CHECKIZHINXMIN-NEXT: lui a0, 4
|
|
; CHECKIZHINXMIN-NEXT: addi a0, a0, 512
|
|
; CHECKIZHINXMIN-NEXT: # kill: def $x10_h killed $x10_h killed $x10
|
|
; CHECKIZHINXMIN-NEXT: ret
|
|
ret half 3.0
|
|
}
|
|
|
|
define half @half_imm_op(half %a) nounwind {
|
|
; CHECK-LABEL: half_imm_op:
|
|
; CHECK: # %bb.0:
|
|
; CHECK-NEXT: lui a0, %hi(.LCPI1_0)
|
|
; CHECK-NEXT: flh fa5, %lo(.LCPI1_0)(a0)
|
|
; CHECK-NEXT: fadd.h fa0, fa0, fa5
|
|
; CHECK-NEXT: ret
|
|
;
|
|
; RV32IZHINX-LABEL: half_imm_op:
|
|
; RV32IZHINX: # %bb.0:
|
|
; RV32IZHINX-NEXT: li a1, 15
|
|
; RV32IZHINX-NEXT: slli a1, a1, 10
|
|
; RV32IZHINX-NEXT: fadd.h a0, a0, a1
|
|
; RV32IZHINX-NEXT: ret
|
|
;
|
|
; RV64IZHINX-LABEL: half_imm_op:
|
|
; RV64IZHINX: # %bb.0:
|
|
; RV64IZHINX-NEXT: li a1, 15
|
|
; RV64IZHINX-NEXT: slli a1, a1, 10
|
|
; RV64IZHINX-NEXT: fadd.h a0, a0, a1
|
|
; RV64IZHINX-NEXT: ret
|
|
;
|
|
; CHECKIZFHMIN-LABEL: half_imm_op:
|
|
; CHECKIZFHMIN: # %bb.0:
|
|
; CHECKIZFHMIN-NEXT: fcvt.s.h fa5, fa0
|
|
; CHECKIZFHMIN-NEXT: lui a0, 260096
|
|
; CHECKIZFHMIN-NEXT: fmv.w.x fa4, a0
|
|
; CHECKIZFHMIN-NEXT: fadd.s fa5, fa5, fa4
|
|
; CHECKIZFHMIN-NEXT: fcvt.h.s fa0, fa5
|
|
; CHECKIZFHMIN-NEXT: ret
|
|
;
|
|
; CHECKIZHINXMIN-LABEL: half_imm_op:
|
|
; CHECKIZHINXMIN: # %bb.0:
|
|
; CHECKIZHINXMIN-NEXT: fcvt.s.h a0, a0
|
|
; CHECKIZHINXMIN-NEXT: lui a1, 260096
|
|
; CHECKIZHINXMIN-NEXT: fadd.s a0, a0, a1
|
|
; CHECKIZHINXMIN-NEXT: fcvt.h.s a0, a0
|
|
; CHECKIZHINXMIN-NEXT: ret
|
|
%1 = fadd half %a, 1.0
|
|
ret half %1
|
|
}
|
|
|
|
define half @half_positive_zero(ptr %pf) nounwind {
|
|
; CHECK-LABEL: half_positive_zero:
|
|
; CHECK: # %bb.0:
|
|
; CHECK-NEXT: fmv.h.x fa0, zero
|
|
; CHECK-NEXT: ret
|
|
;
|
|
; RV32IZHINX-LABEL: half_positive_zero:
|
|
; RV32IZHINX: # %bb.0:
|
|
; RV32IZHINX-NEXT: li a0, 0
|
|
; RV32IZHINX-NEXT: ret
|
|
;
|
|
; RV64IZHINX-LABEL: half_positive_zero:
|
|
; RV64IZHINX: # %bb.0:
|
|
; RV64IZHINX-NEXT: li a0, 0
|
|
; RV64IZHINX-NEXT: ret
|
|
;
|
|
; CHECKIZFHMIN-LABEL: half_positive_zero:
|
|
; CHECKIZFHMIN: # %bb.0:
|
|
; CHECKIZFHMIN-NEXT: fmv.h.x fa0, zero
|
|
; CHECKIZFHMIN-NEXT: ret
|
|
;
|
|
; CHECKIZHINXMIN-LABEL: half_positive_zero:
|
|
; CHECKIZHINXMIN: # %bb.0:
|
|
; CHECKIZHINXMIN-NEXT: li a0, 0
|
|
; CHECKIZHINXMIN-NEXT: ret
|
|
ret half 0.0
|
|
}
|
|
|
|
define half @half_negative_zero(ptr %pf) nounwind {
|
|
; CHECK-LABEL: half_negative_zero:
|
|
; CHECK: # %bb.0:
|
|
; CHECK-NEXT: lui a0, 1048568
|
|
; CHECK-NEXT: fmv.h.x fa0, a0
|
|
; CHECK-NEXT: ret
|
|
;
|
|
; RV32IZHINX-LABEL: half_negative_zero:
|
|
; RV32IZHINX: # %bb.0:
|
|
; RV32IZHINX-NEXT: lui a0, 1048568
|
|
; RV32IZHINX-NEXT: ret
|
|
;
|
|
; RV64IZHINX-LABEL: half_negative_zero:
|
|
; RV64IZHINX: # %bb.0:
|
|
; RV64IZHINX-NEXT: lui a0, 1048568
|
|
; RV64IZHINX-NEXT: ret
|
|
;
|
|
; CHECKIZFHMIN-LABEL: half_negative_zero:
|
|
; CHECKIZFHMIN: # %bb.0:
|
|
; CHECKIZFHMIN-NEXT: lui a0, 1048568
|
|
; CHECKIZFHMIN-NEXT: fmv.h.x fa0, a0
|
|
; CHECKIZFHMIN-NEXT: ret
|
|
;
|
|
; CHECKIZHINXMIN-LABEL: half_negative_zero:
|
|
; CHECKIZHINXMIN: # %bb.0:
|
|
; CHECKIZHINXMIN-NEXT: lui a0, 1048568
|
|
; CHECKIZHINXMIN-NEXT: ret
|
|
ret half -0.0
|
|
}
|
|
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
|
|
; RV32IZHINXMIN: {{.*}}
|
|
; RV64IZHINXMIN: {{.*}}
|