http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21855

--- Comment #18 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-01-10 
16:35:46 UTC ---
(In reply to comment #17)
> (In reply to comment #16)
> > (In reply to comment #15)
> > > (In reply to comment #14)
> > > > (In reply to comment #13)
> > > > > We can't optimize this because System.out.println can change args[].
> > > > 
> > > > That's the whole point: System.out.println cannot change args[], which 
> > > > is a
> > > > java array, and the length of a Java array is constant.  It is not an 
> > > > invalid
> > > > test case.
> > > 
> > > I suppose
> > > 
> > >   public static void main(String[] args)
> > > 
> > > is passing args by value (but the implementation detail uses reference
> > > passing for efficiency?).
> > 
> > args is indeed a reference to a Java array.  The length field of a Java
> > array is immutable.  The elements of an array are not immutable.
> 
> You mean that System.out.println could change the elements of the array
> (well, it doesn't, but theoretically it could)?
> 
> > > In this case the Java frontend should do
> > > like the C++ frontend and tell this to the middle-end by properly
> > > marking args as 1) DECL_BY_REFERENCE, 2) use a TYPE_RESTRICT qualified
> > > pointer for the reference.  Then we would optimize this case.
> > 
> > If we could mark the length field as immutable that would fix it.  Is there 
> > any
> > way to do that?
> 
> No.  What you can do is, via the method I outlined, tell GCC that
> args is to be treated similar to a local automatic variable - thus
> it cannot be refered to from other functions (unless you pass them
> its address of course).

Thus, similar to the C++ case with

struct Array { int length; void data[]; }

void foo (Array args)
{
 ...
}

foo cannot change the callers args.length (only its own copy) but it
can change the callers args.data[] contents.  If the C++ frontend
decides to pass args by reference then it sets DECL_BY_REFERENCE
and uses a TYPE_RESTRICT qualified pointer.  This way the optimization
will be the same as if it was passed "really" by value.

Not sure if the Java situation is similar enough.

Reply via email to