llvm-project
llvm-project copied to clipboard
Use of slti to reduce code size with compressed branch
We currently emit the following for a less than comparison against a constant:
li a1, 254
blt a1, a0, .LBB0_2
An alternate sequence we could emit would be:
slti a1, a0, 254
bnez a1, .LBB0_2
bnez is a compressible instruction; none of the others are. By using the later form, we can save 2 bytes in the case where LBB0_2 is nearby. If a0 is not otherwise used, this also improves register pressure. This does lengthen the critical path. For this to be profitable (code size wise), the constant must fit in the slti encoding.
To reproduce:
$ ./llc -O3 -march=riscv64 -mattr=+c < compare.ll
declare void @foo()
define void @bar(i64 %a) {
%c = icmp slt i64 %a, 255
br i1 %c, label %taken, label %return
taken:
call void @foo()
br label %return
return:
ret void
}
@llvm/issue-subscribers-backend-risc-v
Posted patch on review: https://reviews.llvm.org/D132358