This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Stack use optimizations.
- To: egcs at cygnus dot com
- Subject: Stack use optimizations.
- From: Pal Engstad <engstad at hunt dot inmet dot com>
- Date: Mon, 24 Nov 1997 22:28:35 -0800
Hi.
I've noticed some obvious optimizations not taking place in the later
egcs versions. The most obvious one is this:
struct bigstruct {
double x;
double y;
double z;
};
double stackuse_2()
{
struct bigstruct x = { 1.0, 1.0, 0.0 };
return 1.0;
}
First of all, the compiler does warn about a variable not being used,
but the compiler still tries to write variables onto the stack.
This is hardly ever a big problem for C programs, but in C++ there are
quite a few cases where one would use a structure as a function-object.
For your reference, I made a small test-program. The compiler optimized
away most of the constants, but it still pushed temporary values onto
the stack.
There has to be a way to tell if a value that is pushed onto the stack
is never referred to. If it is not then it can safely be ignored. As a
matter of fact, if a function is not using the stack at all, then the
function can also ignore the use of stack-pointers and frame-pointers.
(PS: Note that only local variables pushed on the stack can be taken
away. Return-values can not.)
In the example below the functions mult_[0-3] should all produce
the same code and ditto for cm[0-3].
struct multiplier {
double operator() (double a, double b) { return a * b; }
};
struct multiplier2 {
double a, b;
multiplier2(double x, double y) : a(x), b(y) {}
operator double () { return a * b; }
};
struct multiplier3
{
double result;
multiplier3(double x, double y) : result(x * y) {}
operator double () { return result; }
};
double mult_0(double a, double b)
{
return a * b;
}
double mult_1(double a, double b)
{
return multiplier()(a, b);
}
double mult_2(double a, double b)
{
return multiplier2(a, b);
}
double mult_3(double a, double b)
{
return multiplier3(a, b);
}
double cm0()
{
return mult_0(1.0, 1.0);
}
double cm1()
{
return mult_1(1.0, 1.0);
}
double cm2()
{
return mult_2(1.0, 1.0);
}
double cm3()
{
return mult_3(1.0, 1.0);
}
PKE.