Re: c = foo(--a) + a; ?
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Fri, 02 Oct 2009 13:26:26 -0400
Keith Thompson wrote:
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:In <ha2o9i$a90$1@xxxxxxxxxxxxxxxxxxxxxxxxxx>, James Kuyper wrote:[...]In general, yes - but it took me only two seconds to think of a counter-example:
int x;
int foo(void)
{
printf("foo\n");
return 6;
}
int bar(void)
{
printf("bar\n");
return 42;
}
int baz(void)
{
x = foo() + bar();
printf("%d\n", x);
}
A compiler is allowed to notice that foo() and bar() return constants, and thus could emit code such as:
MOV x, 48
LEA R1, "foo\n" ;; or "bar\n", of course
CALL _printf
LEA R1, "bar\n" ;; or "foo\n", of course
CALL _printf
LEA R1, "%d\n"
MOV R2, x
CALL _printf
That is, it is allowed to assign to x before evaluating foo() and bar() (for their side effects).
I'm not convinced that that's correct. Function calls do involve
sequence points, and I suspect that modifying x before making the
calls would involve moving a side effect across a sequence point.
I haven't worked out the details. I've been saving your article for
the last day or two in anticipation of finding the time to do so.
Maybe somebody else can do the analysis?
If there were code in foo() or bar() or in their calls to
printf() that accessed x, the assignment would have to take place
after both foo() and bar() had returned. But if the compiler can
somehow know that no such accesses occur, it can reposition
the assignment as described.
It's probably pretty hard for the compiler to know that much
about a complicated function like printf(), but in principle the
jiggery-pokery appears possible. For a less intricate side-effect
than a printf() call -- say, assignments to globals foo_var and
bar_var -- the optimization seems more plausible.
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: c = foo(--a) + a; ?
- From: Keith Thompson
- Re: c = foo(--a) + a; ?
- References:
- Re: c = foo(--a) + a; ?
- From: Antoninus Twink
- Re: c = foo(--a) + a; ?
- From: James Kuyper
- Re: c = foo(--a) + a; ?
- From: Richard Heathfield
- Re: c = foo(--a) + a; ?
- From: Keith Thompson
- Re: c = foo(--a) + a; ?
- Prev by Date: Re: can a character be negative?
- Next by Date: Re: can a character be negative?
- Previous by thread: Re: c = foo(--a) + a; ?
- Next by thread: Re: c = foo(--a) + a; ?
- Index(es):
Relevant Pages
|