Bug 37604 - Suboptimal code generation for volatile access
Summary: Suboptimal code generation for volatile access
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: ada (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-09-20 20:13 UTC by Samuel Tardieu
Modified: 2008-09-21 12:23 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Samuel Tardieu 2008-09-20 20:13:44 UTC
The following code generates suboptimal code on i686-pc-linux-gnu:

with Interfaces; use Interfaces;
with System.Storage_Elements;
package A is
   Var : unsigned_8;
   for Var'Address use System.Storage_Elements.To_Address (1000);
   pragma Volatile (Var);
end A;
with A;
with Interfaces; use Interfaces;
procedure U is
begin
   A.Var := A.Var or 1;
   A.Var := A.Var or 2;
end U;

With -O3 -fomit-frame-pointer, we get:

_ada_u:
        movl    a__var, %edx     <=== indirection instead of...
        movzbl  (%edx), %eax     <=== ...direct access here
        orl     $1, %eax
        movb    %al, (%edx)
        movl    a__var, %edx     <=== useless reloading of a__var
        movzbl  (%edx), %eax
        orl     $2, %eax
        movb    %al, (%edx)
        ret

If the variable is declared in the same compilation unit, we get:

_ada_u:
        movzbl  1000, %eax
        orl     $1, %eax
        movb    %al, 1000
        movzbl  1000, %eax
        orl     $2, %eax
        movb    %al, 1000
        ret

We should probably get the same code in both cases.
Comment 1 Richard Biener 2008-09-21 12:20:19 UTC
We don't try to optimize volatile accesses.
Comment 2 Samuel Tardieu 2008-09-21 12:23:43 UTC
Well, in the second example I give (variable declared in the same compilation unit), the access is optimized. What I don't get is the difference between both cases, while the compiler does have the same information at its disposal.