bagel99 / llvm-my66000

This is a fork of the LLVM project. The code in branch my66000 supports Mitch Alsup's MY66000. The code in branch mcore supports the Motorola MCore.
http://llvm.org
Other
2 stars 2 forks source link

Combining b + c - 1 #38

Closed tkoenig1 closed 1 year ago

tkoenig1 commented 1 year ago

I am having to reach a bit to find missed optimizations in embench-iot, but here's an interesting one, reduced from libpicojpg. Other architectures do not do this.

long foo (long b, long c)
{
  return b + c - 1;
}
asdf@flaemmli:~/ADD$ PS1="$ "
$ cat c.c 
long foo (long b, long c)
{
  return b + c - 1;
}
$ 66 c.c
$ cat c_opt.s 
        .text
        .file   "c.c"
        .globl  foo                             ; -- Begin function foo
        .type   foo,@function
foo:                                    ; @foo
; %bb.0:                                ; %entry
        add     r1,r1,r2
        add     r1,r1,#-1
        ret
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo
                                        ; -- End function
        .ident  "clang version 15.0.0 (https://github.com/bagel99/llvm-my66000.git af7ee91ac91db0ac489c686eb35daf15ceb1f32f)"
        .section        ".note.GNU-stack","",@progbits
$ 

This could be just

      add r1,r2,#-1
tkoenig1 commented 1 year ago

Truth be told, this looks like a missed optimization of clang generally. Godbolt gives the code for ARM64 as

foo(int, int):                               # @foo(int, int)
        add     a0, a0, a1
        addiw   a0, a0, -1
        ret

gcc does not catch this either.

tkoenig1 commented 1 year ago

Argh, too late in the evening. The code I posted is wrong, of course :-)