This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
[ Reported to the Debian BTS as report #85535. Please CC 85535-quiet@bugs.debian.org on replies. Log of report can be found at http://bugs.debian.org/85535 ] int main() { return !memcmp("abcd", "efgh", 4); } produces the assembly (-S -g -O2), movl $.LC1, %edi movl $.LC0, %esi movl $4, %ecx cld repz cmpsb seta %cl setb %dl popl %esi xorl %eax, %eax popl %edi cmpb %dl, %cl sete %al which is a waste because it could've simply loaded the two objects into registers and compared them. The same is true on other architectures as well. Release: 3.0 (Debian GNU/Linux) and HEAD 20010701 Environment: System: Debian GNU/Linux (testing/unstable) Architecture: i686 host: i386-linux build: i386-linux target: i386-linux configured with: ../src/configure -v --enable-languages=c,c++,java,f77,proto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man --enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-checking --enable-threads=posix --enable-java-gc=boehm --with-cpp-install-dir=bin --enable-objc-gc i386-linux
Fix: http://gcc.gnu.org/ml/gcc-patches/2001-12/msg02443.html
State-Changed-From-To: open->closed State-Changed-Why: Fixed http://gcc.gnu.org/ml/gcc-patches/2001-12/msg02443.html
Unfortunately this bug has only been special cased rather than fixed in its full generality. If we change one of the arguments to a variable, the compiler still fails to produce the dword comparison that I would expect: -- a.c -- int foo(const char *s) { return !memcmp("abcd", s, 4); } -- gcc -S -O2 a.c -- .LC0: .string "abcd" .text .p2align 2,,3 .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp pushl %edi pushl %esi movl 8(%ebp), %edi movl $.LC0, %esi movl $4, %ecx cld repz cmpsb sete %al popl %esi movzbl %al, %eax popl %edi leave ret .size foo, .-foo .ident "GCC: (GNU) 3.3 20030509 (Debian prerelease)" --
Reopening because the code is not optimal in the case where only argument is constant to memcmp.
Related to bug 12086.
This is a dup of bug 12086, which should be fixed for 3.5 if I get my act together. *** This bug has been marked as a duplicate of 12086 ***