This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Okay, who made scalar replacement of aggregates work?
- To: gcc at gcc dot gnu dot org
- Subject: Okay, who made scalar replacement of aggregates work?
- From: Daniel Berlin <dan at cgsoftware dot com>
- Date: 07 Jun 2001 12:25:30 -0400
I was told we only do it if the entire class fits in a register.
This is no longer true.
We'll do it to single members of classes/structs now to.
In other words, we do full scalar replacement of aggregates.
Try the following (for C):
typedef enum { APPLE, BANANA, ORANGE } VARIETY;
typedef enum { LONG, ROUND } SHAPE;
typedef struct fruit {
VARIETY variety;
SHAPE shape;
int fred;
int george;
int a;
int b;
int c;
int d;
}
FRUIT;
char * Red="red";
char * Yellow = "yellow";
char *Orange = "orange";
int george (CurrentFruit)
FRUIT *CurrentFruit;
{
return CurrentFruit->george;
}
char *color (CurrentFruit)
FRUIT *CurrentFruit;
{
switch (CurrentFruit->variety) {
case APPLE: return Red;
break;
case BANANA: return Yellow;
break;
case ORANGE: return Orange;
}
}
main( )
{
FRUIT snack;
snack.variety = APPLE;
snack.shape = ROUND;
snack.fred = 30;
snack.george = 3;
printf("%s\n", color (&snack));
printf("%d\n", george (&snack));
}
Even though snack can't fit in a register, some of it's components
will, and we sure as hell put them there.
Make snack as big as you like. We'll still do the repacements we can.
Same with the C++ version.
extern "C" int printf(const char *, ...);
enum VARIETY
{ APPLE, BANANA, ORANGE };
enum SHAPE { LONG, ROUND };
class FRUIT
{
public:
enum VARIETY variety;
enum SHAPE shape;
int george;
int fred;
int a;
int b;
int c;
int d;
};
char * Red="red";
char * Yellow = "yellow";
char *Orange = "orange";
char *color (FRUIT *CurrentFruit)
{
switch (CurrentFruit->variety) {
case APPLE: return Red;
break;
case BANANA: return Yellow;
break;
case ORANGE: return Orange;
}
}
int main( )
{
FRUIT snack;
snack.variety = APPLE;
snack.shape = ROUND;
snack.george = 5;
snack.fred = 30;
printf("%s\n", color (&snack));
printf("%d\n", snack.fred);
}
--Dan
--
"It's a good thing we have gravity, or else when birds died
they'd just stay right up there. Hunters would be all confused.
"-Steven Wright