This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gcc-3.0: Obvious infinite recursion not detected


On Thu, Jul 05, 2001 at 09:55:29AM -0300, Alexandre Oliva wrote:
> On Jul  5, 2001, Ryszard Kabatek <Ryszard.Kabatek@softax.pl> wrote:
> 
> > but the compiler does not warn.
> 
> And why should it?  What do you suggest the warning to be?  Under what
> conditions would it trigger?

Yet, common sense tells me that the optimization code of g++ should
be able to detect this.  Otherwise it would also not be able to optimize
it.

I won't try to describe an algorithm to detect this, but I am sure
it exists.  The problems seems to be related to iterative calls versus
recursive calls however.

Hmm, lets say the compiler is able to detect whether a function is a
`const' function (doesn't change anything but local variables, doesn't
use any static or global variables): the return value only depends on
the parameters passed.  Then a call like:

int f(int a1, int a2)
{
   // ...
   f(expression1(a1, a2), expression2(a1, a2));
   // ...
}

can be optimized (or at least, rewritten) in an iterative manner:

int f(int a1, int a2)
{
start:
   // ...
   new_a1 = expression1(a1, a2);
   new_a2 = expression2(a1, a2);
   a1 = new_a1;
   a2 = new_a2;
   goto start;
   // ...
}

which gets rid of the call overhead thus.

When we apply that to the example:

bool operator==(const X* lhs, const X* rhs)
{
  return lhs == rhs;
}

first becomes

bool operator==(const X* lhs, const X* rhs)
{
start:
  new_lhs = lhs;
  new_rhs = rhs;
  lhs = new_lhs;
  rhs = new_rhs;
  goto start;
}

then becomes:

bool operator==(const X* lhs, const X* rhs)
{
start:
  goto start;
}

and then could give the warning:
Warning, function with return value never returns.

While if we had started with:

bool operator==(const X* lhs, const X* rhs)
{
  if (lhs == NULL)
    return false;
  return lhs == rhs;
}

and after optimization we'd end up with:

bool operator==(const X* lhs, const X* rhs)
{
  if (lhs == NULL)
    return false;
start:
  goto start;
}

and no warning would be generated (but it wouldn't be hard to come up with one).

-- 
Carlo Wood <carlo@alinoe.com>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]