llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Use of slti to reduce code size with compressed branch

Open preames opened this issue 4 years ago • 2 comments

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
}

preames avatar Jul 05 '22 18:07 preames

@llvm/issue-subscribers-backend-risc-v

llvmbot avatar Jul 05 '22 18:07 llvmbot

Posted patch on review: https://reviews.llvm.org/D132358

dybv-sc avatar Aug 22 '22 10:08 dybv-sc