This is the mail archive of the gcc-patches@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]
Other format: [Raw text]

Re: [PATCH] Make attribute functions pure


On Friday 23 July 2004 13:13, Richard Kenner wrote:
>     GCC assumes that pure functions don't modify memory.  And even
>     though recog_memoized() conceptually depends only on PATTERN (insn),
>     the implementation really does modify insn.  Saying that it's pure
>     could lead to some subtle miscompilation later.
>
> I don't see how.  It's usually accepted that pure functions can be
> memoized. Can you give an example of the sort of miscompilation you're
> worried about?
$ cat impure.c
int i = 0;

int __attribute__((pure))
foo (int n)
{
  i = n + 1; /* Illegal - Pure functions shouldn't have side effects.  */
  return n;
}

int main()
{
  int a, b;

  a = i;
  b = foo(a);
  if (a + i + b != 1) abort();
  return 0;
}
$ gcc -O2 test.c
$ ./a.out
Aborted

Paul


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