Bug 12086

Summary: memcmp(i,j,4) should use word (SI) subtraction
Product: gcc Reporter: Andrew Pinski <pinskia>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: enhancement CC: dimhen, gabravier, gcc-bugzilla, msebor, ville.voutilainen
Priority: P2 Keywords: missed-optimization
Version: 3.4.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2007-07-01 01:22:19
Bug Depends on: 52171    
Bug Blocks:    

Description Andrew Pinski 2003-08-28 03:00:40 UTC
It would be nice if memcmp is inlined for small n's
It would be nice if these two functions are the same:
int g(int *j,int *l)  {  return memcmp(j,l,4);  }
int h(int *j, int *l) {  return *j - *l;                   }
This save space (not in this function on PPC because of sibcall but it could because gcc 
does not have to spill to much more to go over the function call) and time (because no 
function overhead on targets where memcmp is not inlined like PPC).
Comment 1 Andrew Pinski 2003-08-28 03:05:51 UTC
*** Bug 12087 has been marked as a duplicate of this bug. ***
Comment 2 Andrew Pinski 2003-08-28 03:21:09 UTC
I have got my own bug.
Add the attribute may_alias to the (const unsigned int *) when creating the type will make sure 
there is no aliasing problems.
The 2 (short case) instead of 4 should be able to be done the same way and also the 8 case (long 
long).  I have to check on the size of the types but that is the only thing different than the 1 size 
case that is already there besides the extra attribute, I should be able to do this tonight.
Comment 3 Andrew Pinski 2003-08-28 17:02:58 UTC
Posted patch <http://gcc.gnu.org/ml/gcc-patches/2003-08/msg01826.html>.
Comment 4 Andrew Pinski 2003-09-18 23:50:38 UTC
Cannot be done as there are cases where it would return the wrong result.
Comment 5 Falk Hueffner 2003-09-23 08:46:01 UTC
Why do you think it would give wrong results? On big endian, it should work IMHO,
on little endian, you might also be able to do something on some platforms (see
http://gcc.gnu.org/ml/gcc-patches/2003-04/msg01723.html)
Comment 6 Andrew Pinski 2003-09-24 04:01:30 UTC
Reopening based on new thoughts but this will not go in for 3.4 at least by me because I am too 
busy.
Comment 7 Andrew Pinski 2003-12-26 02:24:32 UTC
*** Bug 3508 has been marked as a duplicate of this bug. ***
Comment 8 Andrew Pinski 2005-10-22 23:59:51 UTC
I don't have time to work on this any more.
Comment 9 Andrew Pinski 2006-08-21 06:12:17 UTC
I am going to fix up my patch for 4.3.0.
Comment 10 Andrew Pinski 2007-05-28 21:07:07 UTC
I am no longer working on this.
Comment 11 Eelis 2014-11-16 18:53:48 UTC
This bug means that for

  struct X { char a[8]; };
  bool f(X x, X y) { return __builtin_memcmp(x.a, y.a, 8); }

gcc emits:

  subq    $40, %rsp
  .cfi_def_cfa_offset 48
  movl    $8, %edx
  movq    %rdi, 16(%rsp)
  leaq    16(%rsp), %rdi
  movq    %rsi, (%rsp)
  movq    %rsp, %rsi
  call    memcmp
  testl   %eax, %eax
  setne   %al
  addq    $40, %rsp
  .cfi_def_cfa_offset 8
  ret

while clang emits:

  movq	%rdi, -8(%rsp)
  movq	%rsi, -16(%rsp)
  cmpq	%rsi, -8(%rsp)
  setne	%al
  retq

(which is still suboptimal due to llvm bug #20673).
Comment 12 owner 2014-11-16 19:03:54 UTC
Thank you for the additional information you have supplied regarding
this Bug report.

This is an automatically generated reply to let you know your message
has been received.

Your message has not been forwarded to the package maintainers or
other interested parties; you should ensure that the developers are
aware of the problem you have entered into the system - preferably
quoting the Bug reference number, #85535.

If you wish to submit further information on this problem, please
send it to 85535-quiet@bugs.debian.org.

Please do not send mail to owner@bugs.debian.org unless you wish
to report a problem with the Bug-tracking system.
Comment 13 Eelis 2014-12-24 14:18:48 UTC
It's also worth noting that this affects operator== for std::array. :(
Comment 14 owner 2014-12-24 14:27:53 UTC
Thank you for the additional information you have supplied regarding
this Bug report.

This is an automatically generated reply to let you know your message
has been received.

Your message has not been forwarded to the package maintainers or
other interested parties; you should ensure that the developers are
aware of the problem you have entered into the system - preferably
quoting the Bug reference number, #85535.

If you wish to submit further information on this problem, please
send it to 85535-quiet@bugs.debian.org.

Please do not send mail to owner@bugs.debian.org unless you wish
to report a problem with the Bug-tracking system.
Comment 15 owner 2016-06-04 06:15:39 UTC
Thank you for the additional information you have supplied regarding
this Bug report.

This is an automatically generated reply to let you know your message
has been received.

Your message has not been forwarded to the package maintainers or
other interested parties; you should ensure that the developers are
aware of the problem you have entered into the system - preferably
quoting the Bug reference number, #85535.

If you wish to submit further information on this problem, please
send it to 85535-quiet@bugs.debian.org.

Please do not send mail to owner@bugs.debian.org unless you wish
to report a problem with the Bug-tracking system.
Comment 16 Martin Sebor 2017-01-11 20:29:29 UTC
See also bug 78257 for a related enhancement.
Comment 17 Andrew Pinski 2020-08-13 19:36:53 UTC
*** Bug 96603 has been marked as a duplicate of this bug. ***