[comp.lang.c] Question on aliasing

stacey@hcr.UUCP (Stacey Campbell) (08/04/88)

>Exactly what is 'aliasing', and why does it make such a difference?
>They mention that [noalias] will speed up the program. How does this work?

Here is an example of aliasing and what it means to an optimiser...

int *trouble;

main()
{
	int i, n = 10;
	void setup_alias();

	setup_alias(&n);

/* within the loop (n / 5) looks like good optimiser fodder,
 * but it can't be touched because '*trouble' is aliased to 'n'
 * therefore 'n' is not invariant */

	for (i = 0; i < 10; ++i) {
		if (n / 5 + i > 10)
			printf("higher %d\n", n);
		*trouble += 2;
	}
}

/* in another module far away from the current hcr invocation
 * the following happens */

void setup_alias(int_ptr)
int *int_ptr;
{
	trouble = int_ptr;
}

Now consider a different setup_alias() routine...

void setup_alias(int_ptr)
int *int_ptr;
{
	printf("Henry, make sure you correct this if it is a bad example.\n");
}

If setup_alias() didn't alias 'n' the optimiser still couldn't
simplify (n / 5) because it has no knowlege of what is happening to the
&n parameter in the call; your program would be calculating the same
division for every iteration of the loop.

To convince the hcr that it can optimise (n / 5) you would declare 'n'
as...

noalias int n;

The optimiser would then calculate (n / 5) once outside the loop, store
the result in a temporary variable, and use the temporary in the loop
(but if you declare it noalias, then go ahead and alias it and write to the
alias, all hell will break loose).
-- 
{lsuc,utzoo,utcsri}!hcr!hcr!stacey Stacey Campbell, HCR Corporation,
130 Bloor St W, Toronto, Ontario, Canada. +1 416 922 1937 X48

henry@utzoo.uucp (Henry Spencer) (08/06/88)

In article <3709@hcr.UUCP> stacey@hcr.UUCP (Stacey Campbell) writes:
>>They mention that [noalias] will speed up the program. How does this work?
> ...
>	printf("Henry, make sure you correct this if it is a bad example.\n");

What a sneaky way to get me to comment... :-)

>noalias int n;
>
>The optimiser would then calculate (n / 5) once outside the loop, store
>the result in a temporary variable, and use the temporary in the loop
>(but if you declare it noalias, then go ahead and alias it and write to the
>alias, all hell will break loose).

Pretty much so.  The idea behind noalias was, roughly, that if you access a
value via a path including "noalias", the compiler is entitled to cache that
value, possibly even altering the cached copy and writing it back later,
without worrying that the value might have been accessed by another path
which would result in another possibly-altered-and-written-back cached copy.
If such an alternate path exists, the results are anyone's guess.  Getting
all this *precisely* pinned down is harder than it looks, especially in
the presence of arrays (if you pass a pointer to an array element, is this
a path to the whole array?  what if it's a character array that contains
more than one string, and you pass a pointer to one of the strings? etc.).
-- 
MSDOS is not dead, it just     |     Henry Spencer at U of Toronto Zoology
smells that way.               | uunet!attcan!utzoo!henry henry@zoo.toronto.edu