This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [tree-ssa] Remove useless null pointer checks
Frank Ch. Eigler writes:
>
> jsturm wrote:
>
> > [...] Java emits tons of useless null pointer checks.
> > Without this patch, the class:
> >
> > class A {
> > int i;
> > final int j() { return i; }
> > static int sum(A a) { return a.i + a.j(); }
> > }
>
> > would optimize to (in method sum):
> >
> > A.sum(A) (a)
> > {
> > [...]
> > T.3 = a->i;
> > if (a == 0B)
> > {
> > _Jv_ThrowNullPointerException ()
> > }; [...]
>
> Doesn't this look a little late? If the a reference could be NULL,
> then it should have been checked before a->i was fetched.
No, this is correct. Fetching a->i will catch a SEGV and then throw
the exception.
> Also, can you spell out why exactly you consider this check (assuming
> its location was corrected) useless? Can some object not call
> "A.sum(null);" ?
It's useless because if A were null than accessing a->i would already
have thrown the exception. There's no need to check again.
Andrew.