Bug 31557 - return 0x80000000UL code gen can be improved
Summary: return 0x80000000UL code gen can be improved
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: 13.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2007-04-12 22:10 UTC by Andrew Pinski
Modified: 2024-03-05 00:20 UTC (History)
5 users (show)

See Also:
Host:
Target: powerpc64-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-07-02 21:35:45


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2007-04-12 22:10:29 UTC
Testcase:
unsigned int f(void)
{
  return 0x80000001UL;
}

This should be able to done in three instructions:
.f:
        li 3,1
        oris 3,3,0x8000
        blr

Right now it is done as:
.L.f:
        li 3,0
        ori 3,3,32768
        sldi 3,3,16
        ori 3,3,1
        blr
Which is (0x8000 << 16) | 1 but (0x8000 << 16) is what is done for oris.
Comment 1 Andrew Pinski 2007-07-02 21:35:45 UTC
Confirmed.
Comment 2 Ben Elliston 2009-05-14 05:19:18 UTC
Still present in GCC 4.5.0 20090513.
Comment 3 Paul H. Hargrove 2012-08-13 23:03:06 UTC
FWIW, 4.8.0 20120809 w/ -O1 or higher is now using just 4 instructions instead of 5.  So, "half way there".


.L.f:
        lis 3,0x8000
        ori 3,3,1
        rldicl 3,3,0,32
        blr
Comment 4 Paul H. Hargrove 2012-08-14 00:31:14 UTC
(In reply to comment #3)
> FWIW, 4.8.0 20120809 w/ -O1 or higher is now using just 4 instructions instead
> of 5.  So, "half way there".
> 
> 
> .L.f:
>         lis 3,0x8000
>         ori 3,3,1
>         rldicl 3,3,0,32
>         blr

That was for a 64-bit target, where the need to zero the upper half of r3 (which is 0xffffffff due to sign extension of 0x8000 by 'lis') accounts for the 4th instruction.  So, there is still room for improvement using the originally proposed 3-instruction sequence (since 'oris' won't sign-extend as 'lis' does).


For a 32-bit target, it appears that GCC 4.8.0 20120809 has reached the desired three instructions:

f:
        lis 3,0x8000
        ori 3,3,1
        blr
Comment 5 Segher Boessenkool 2017-03-29 15:47:52 UTC
Fixed on trunk, no backports planned, closing.
Comment 6 Segher Boessenkool 2017-03-29 15:51:15 UTC
Actually, huh, *not* fixed on trunk yet.
Comment 7 Peter Bergner 2024-03-05 00:16:25 UTC
(In reply to Segher Boessenkool from comment #6)
> Actually, huh, *not* fixed on trunk yet.

This was fixed in GCC 13.  Marking it as FIXED.