CTSRD-CHERI/llvm

Incrementing pointer by register and small offset is inefficient

brooksdavis opened this issue · 1 comments

This code:

void *
inc_ptr_weirdly(void *ptr, int inc)
{
        return (ptr + inc - 64);
}

increments the pointer with

        cincoffset      $c1, $c3, $4
        daddiu          $1, $zero, -64
        cincoffset      $c3, $c1, $1

It should probably be:

        daddiu          $1, $4, -64
        cincoffset      $c3, $c3, $1

For this test case in the newencodings branch, I get:

	cincoffset	$c1, $c3, $4
	cincoffset	$c3, $c1, -64

I'm not that the proposed assembly is more efficient, but it looks as if we're already folding this into something sensible when we have the cincoffset that uses an immediate. We're also folding it into a load or store, if one is available. See #242.