Bug 21508 - non-optimal code with __builtin_signbit
Summary: non-optimal code with __builtin_signbit
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2005-05-11 09:15 UTC by Uroš Bizjak
Modified: 2006-10-22 21:01 UTC (History)
1 user (show)

See Also:
Host:
Target: i?86-*-*
Build:
Known to work:
Known to fail: 4.0.0 4.1.0
Last reconfirmed: 2006-01-15 20:21:56


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Uroš Bizjak 2005-05-11 09:15:41 UTC
This testcase:

int test (double a) {
        return __builtin_signbit (a + 1.0);
}

produces quite non-optimal code when compiled with '-O2' (-fomit-frame-pointer):

test:
        subl    $12, %esp
!       xorl    %eax, %eax
!       movl    $1072693248, %edx
!       movl    %eax, (%esp)
!       movl    %edx, 4(%esp)
!       fldl    (%esp)
        faddl   16(%esp)
        fstpl   (%esp)
?       movl    4(%esp), %edx
        addl    $12, %esp
?       movl    %edx, %eax
        andl    $-2147483648, %eax
        ret

Instructions marked with (!) could be replaced with a fld1.
Instructions marked with (?) could be replaced with a "movl 4(%esp), %eax".

Perhaps fxam could be used in this case? Then the code would look something like:

fldl   4(%esp)
fxam
fnstsw %ax
fstp   %st(0)
andl   0x02, %eax
ret
Comment 1 Andrew Pinski 2005-05-11 11:25:47 UTC
Confirmed.
Comment 2 Uroš Bizjak 2005-06-03 08:13:03 UTC
It looks that REG_EQUAL notes gets lost between regmove and lreg pass. A 
relevant part from compiling an example in description with '-O2' shows:

pr21508.c.19.regmove:

(note:HI 7 6 12 0 NOTE_INSN_FUNCTION_BEG)

(insn:HI 12 7 13 0 (set (reg:DF 62)
        (mem/u/i:DF (symbol_ref/u:SI ("*.LC1") [flags 0x2]) [2 S8 A64])) 65 
{*movdf_nointeger} (nil)
    (expr_list:REG_EQUAL (const_double:DF -2147483648 [0x80000000] 1.0e+0 
[0x0.8p+1])
        (nil)))

in pr21508.c.22.lreg:
(note:HI 7 6 12 0 NOTE_INSN_FUNCTION_BEG)

(insn:HI 12 7 13 0 (set (reg:DF 62)
        (mem/u/i:DF (symbol_ref/u:SI ("*.LC1") [flags 0x2]) [2 S8 A64])) 65 
{*movdf_nointeger} (nil)
    (nil))


However, compiling a simple program, such as:

double test (double a) {
   return a + 1.0;
}

results in fld1 insn being used. REG_EQUAL notes are still present in lreg dump 
file:

(note:HI 7 6 11 0 NOTE_INSN_FUNCTION_BEG)

(insn:HI 11 7 12 0 (set (reg:DF 61)
        (mem/u/i:DF (symbol_ref/u:SI ("*.LC1") [flags 0x2]) [2 S8 A64])) 65 
{*movdf_nointeger} (nil)
    (expr_list:REG_EQUAL (const_double:DF -2147483648 [0x80000000] 1.0e+0 
[0x0.8p+1])
        (nil)))
Comment 3 Andrew Pinski 2006-10-22 21:01:10 UTC
On the mainline we get:
        subl    $12, %esp
        fld1
        faddl   16(%esp)
        fstpl   (%esp)
        movl    4(%esp), %eax
        addl    $12, %esp
        andl    $-2147483648, %eax
        ret

Which is optimial as there is no extra movl and there is fld1.