Question on aliasing

Stacey Campbell stacey at hcr.UUCP
Fri Aug 5 01:57:33 AEST 1988


>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



More information about the Comp.lang.c mailing list