[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pcc-list
Subject:    Register allocator behavior.
From:       Anders Magnusson <ragge () ludd ! ltu ! se>
Date:       2008-03-28 8:03:46
Message-ID: 47ECA662.3030305 () ludd ! ltu ! se
[Download RAW message or body]

I noticed this interesting "optimization" that the register allocator did:

This code:

x(i){ int a[i]; a[0] = 3; }

creates (on i386) this assembler:

...
        movl 8(%ebp),%eax
        sall $2,%eax
        subl %eax,%esp
        movl $3,(%esp)  <- note!
...

The register allocator realizes that the value it needs is in esp and 
uses it for indexing.  So far so good.
But; because esp is not a register available to the register allocator 
strange things may happen:

x(i){ int a[i]; y(); a[0] = 3; }
...
        movl 8(%ebp),%eax
        sall $2,%eax
        subl %eax,%esp
        call y
        movl $3,(%esp)

esp may have been altered here (think libc alloca) which would cause the 
move to be to the wrong address. 
And even more:

x(i){ int a[i]; y(a,1); }

...
        movl 8(%ebp),%eax
        sall $2,%eax
        subl %eax,%esp
        pushl $1
        pushl %esp
        call y
        addl $8, %esp

Here it will totally fail because esp in incremented in the first push 
and the register allocator is not told that esp will be trashed.

Anyway, I will do a slight change to the register allocator and only 
allow registers to be used for storage
if they are owned by the register allocator (that RSTATUS has a register 
class for the register).
So that noone get surprised :-)

-- Ragge
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic