How to express the fact that arguments don't escape?

Thomas Koenig tkoenig@gcc.gnu.org
Sat Aug 15 09:49:00 GMT 2009


On Sat, 2009-08-15 at 01:59 +0200, Tobias Schlüter wrote:
> Hi,
> 
> I reviewed some old discussions on argument passing, but I found nothing 
> which addressed a seemingly simple optimization for Fortran: what I 
> would like to see is a way to tell the compiler that a function argument 
> (which will be a pointer) will not have escaped after the function has 
> returned.  I.e.
>    call f(i)
>    i = 1
>    call g(j)  ! won't know about i
>    if (i != 1) then
>       can't happen
>    end if
> 
> Is there a way to do this?  Unless I'm mistaken, this assumption holds 
> for all non-POINTER arguments.

To stay within the current, C-centric framework of gcc, we'll need to do
even more copying.

Compare

$ cat g1.f90                                     
subroutine foo                                   
  call f(i)                                      
  i = 1                                          
  call g(j)                                      
  if (i /= 1) then                               
    call abort                                   
  end if                                         
end subroutine foo                               
$ gfortran -O2 -S -fdump-tree-optimized g1.f90   
$ cat g1.f90.133t.optimized                      

;; Function foo (foo_)

foo ()
{
  integer(kind=4) j;
  integer(kind=4) i;
  integer(kind=4) i.0;

<bb 2>:
  f (&i);
  i = 1;
  g (&j);
  i.0_1 = i;
  if (i.0_1 != 1)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  _gfortran_abort ();

<bb 4>:
  return;

}

with

$ cat g2.f90
subroutine foo
  call f(i_aux)
  i = i_aux
  i = 1
  call g(j)
  if (i /= 1) then
    call abort
  end if
end subroutine foo
$ gfortran -O2 -S -fdump-tree-optimized g2.f90
$ cat g2.f90.133t.optimized

;; Function foo (foo_)

foo ()
{
  integer(kind=4) j;
  integer(kind=4) i_aux;

<bb 2>:
  f (&i_aux);
  g (&j);
  return;

}




More information about the Fortran mailing list