Bug 33555

Summary: x86 missed opportunity for sbb
Product: gcc Reporter: astrange+gcc <astrange+gcc>
Component: targetAssignee: Uroš Bizjak <ubizjak>
Status: RESOLVED FIXED    
Severity: enhancement CC: aldot, gcc-bugs, hjl.tools
Priority: P3 Keywords: missed-optimization, patch
Version: 4.3.0   
Target Milestone: 4.4.0   
URL: http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00705.html
Host: i386-apple-darwin8.10.1 Target: i386-apple-darwin8.10.1
Build: i386-apple-darwin8.10.1 Known to work:
Known to fail: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 33702    
Attachments: Patch to implement missed optimization.

Description astrange+gcc@gmail.com 2007-09-25 18:17:44 UTC
> /usr/local/gcc43/bin/gcc -v
Using built-in specs.
Target: i386-apple-darwin8.10.1
Configured with: ../gcc/configure --prefix=/usr/local/gcc43 --with-arch=nocona --with-tune=nocona --with-gmp=/sw --with-system-zlib --enable-languages=c,c++,objc,obj-c++
Thread model: posix
gcc version 4.3.0 20070925 (experimental) (GCC) 

> /usr/local/gcc43/bin/gcc -Os -fno-pic -S sbb.c -fomit-frame-pointer
        .text
.globl _cmpb_sbb
_cmpb_sbb:
        subl    $12, %esp
        movl    16(%esp), %eax
        movl    20(%esp), %ecx
        xorl    %edx, %edx
        cmpl    24(%esp), %ecx
        setb   %dl
        negl    %edx
        andl    %ecx, %edx
        subl    %edx, %eax
        addl    $12, %esp
        ret
        .subsections_via_symbols

Source:
unsigned cmpb_sbb(unsigned a, unsigned b, unsigned c)
{
	unsigned mask = -(b < c);
	
	a -= b & mask;
	return a;
}

"setb, negl" is the same as 0 - (0 + eflags.CF), so it can be replaced with "sbb %edx, %edx".
This is useful for if-conversion, since it's the same as "if (b < c) a -= b;"
Comment 1 Uroš Bizjak 2008-02-18 09:58:08 UTC
Created attachment 15183 [details]
Patch to implement missed optimization.

2008-02-18  Uros Bizjak  <ubizjak@gmail.com>

	PR target/33555
	* config/i386/i386.md (*x86_movsicc_0_m1_se): New insn pattern.
	(*x86_movdicc_0_m1_se): Ditto.

testsuite/ChangeLog:

2008-02-18  Uros Bizjak  <ubizjak@gmail.com>

	PR target/33555
	* gcc.target/i386/pr33555.c: New test.
Comment 2 uros 2008-02-19 07:24:11 UTC
Subject: Bug 33555

Author: uros
Date: Tue Feb 19 07:23:30 2008
New Revision: 132414

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132414
Log:
        PR target/33555
        * config/i386/i386.md (*x86_movsicc_0_m1_se): New insn pattern.
        (*x86_movdicc_0_m1_se): Ditto.

testsuite/ChangeLog:

        PR target/33555
        * gcc.target/i386/pr33555.c: New test.


Added:
    trunk/gcc/testsuite/gcc.target/i386/pr33555.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/i386.md
    trunk/gcc/testsuite/ChangeLog

Comment 3 Uroš Bizjak 2008-02-19 07:41:33 UTC
Fixed.
Comment 4 Uroš Bizjak 2009-10-11 20:18:14 UTC
*** Bug 41677 has been marked as a duplicate of this bug. ***