Null pointer check elimination

David Daney ddaney@avtrex.com
Sun Nov 13 05:27:00 GMT 2005


Tom Tromey wrote:
>>>>>>"Chris" == Chris Gray <chris.gray@kiffer.be> writes:
> 
> 
> Chris> Another way to look at functions which return 'this' (and there
> Chris> are plenty of those in the core Java libraries) is that
> Chris> [...]
> 
> (Another not-uncommon case is methods that always 'return new ...'.)

Both the 'return this', and 'return new' cases imply not null.  But if 
you can distinguish the 'return this' case you might be able to generate 
even better code as you can completely ignore the return value if you 
already have a copy of it saved somewhere.  Or conversely you might not 
have to store it if you knew you were going to get it back.

> 
> We talked a bit on irc the other day about having a way to
> automatically recognize situations like this and mark up the class
> files (eg) so that we wouldn't have to maintain a separate list of
> known-non-null-returning functions inside gcj.

I was thinking this exact thought.

Furthermore, I want an additional attribute that says if any pointers 
escape from a method.  If we knew this we could allocate the storage for 
the class on the stack instead of the heap.

IMO the classfile is the best place to store this information.

> 
> It isn't unreasonable to try to detect known good cases during
> bytecode generation and then write a special method attribute.
> However, there is a security issue here; I would say we would probably
> have to ignore this attribute at least in BC mode.

I don't see what the security issue is.  Don't you trust the compiled 
version of the class without being able to verify it?  How is trusting 
the classfile so that you can optimize any different?

> 
> I wouldn't want this idea to hold up Anthony's patch though.

Anthony's patch is a nice proof of concept, but I don't think anyone is 
expecting it to be committed.

> I think
> they are separable and, as this one is harder to implement, it is
> reasonable to go with something easy first.

I disagree, I think the best way to identify all of these optimization 
opportunities is to add method attributes to the classfiles.

David Daney.



More information about the Java mailing list