Initial revision
This commit is contained in:
46
util/ego/sp/Makefile
Normal file
46
util/ego/sp/Makefile
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
EMH=../../../h
|
||||
EML=../../../lib
|
||||
SHARE=../share
|
||||
OBJECTS=sp.o
|
||||
MOBJECTS=sp.m
|
||||
SHOBJECTS=$(SHARE)/get.o $(SHARE)/put.o $(SHARE)/alloc.o $(SHARE)/global.o $(SHARE)/debug.o $(SHARE)/files.o $(SHARE)/map.o $(SHARE)/lset.o $(SHARE)/cset.o $(SHARE)/aux.o $(SHARE)/stack_chg.o $(SHARE)/go.o
|
||||
MSHOBJECTS=$(SHARE)/get.m $(SHARE)/put.m $(SHARE)/alloc.m $(SHARE)/global.m $(SHARE)/debug.m $(SHARE)/files.m $(SHARE)/map.m $(SHARE)/lset.m $(SHARE)/cset.m $(SHARE)/aux.m $(SHARE)/stack_chg.m
|
||||
SRC=sp.c
|
||||
.SUFFIXES: .m
|
||||
|
||||
.c.o:
|
||||
cc $(CFLAGS) -c $<
|
||||
.c.m:
|
||||
ack -O -L -c.m $(CFLAGS) $<
|
||||
all: $(OBJECTS)
|
||||
sp: \
|
||||
$(OBJECTS) $(SHOBJECTS)
|
||||
cc -o sp -i $(OBJECTS) $(SHOBJECTS) $(EML)/em_data.a
|
||||
optim: $(MOBJECTS) $(MSHOBJECTS)
|
||||
ego IC CF $(F) CA $(MOBJECTS) $(MSHOBJECTS)
|
||||
ack -O -o sp.ego -.c lfile.m $(EML)/em_data.a
|
||||
lpr:
|
||||
pr $(SRC) | lpr
|
||||
# the next lines are generated automatically
|
||||
# AUTOAUTOAUTOAUTOAUTOAUTO
|
||||
sp.o: ../share/alloc.h
|
||||
sp.o: ../share/aux.h
|
||||
sp.o: ../share/debug.h
|
||||
sp.o: ../share/files.h
|
||||
sp.o: ../share/get.h
|
||||
sp.o: ../share/global.h
|
||||
sp.o: ../share/go.h
|
||||
sp.o: ../share/lset.h
|
||||
sp.o: ../share/map.h
|
||||
sp.o: ../share/put.h
|
||||
sp.o: ../share/stack_chg.h
|
||||
sp.o: ../share/types.h
|
||||
sp.o: ../../../h/em_mnem.h
|
||||
sp.o: ../../../h/em_spec.h
|
||||
stack_chg.o: ../share/debug.h
|
||||
stack_chg.o: ../share/global.h
|
||||
stack_chg.o: ../share/types.h
|
||||
stack_chg.o: ../../../h/em_mnem.h
|
||||
stack_chg.o: ../../../h/em_spec.h
|
||||
stack_chg.o: pop_push.h
|
||||
240
util/ego/sp/sp.c
Normal file
240
util/ego/sp/sp.c
Normal file
@@ -0,0 +1,240 @@
|
||||
/* S T A C K P O L L U T I O N
|
||||
*
|
||||
* S P . C
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../share/types.h"
|
||||
#include "../share/debug.h"
|
||||
#include "../share/global.h"
|
||||
#include "../share/files.h"
|
||||
#include "../share/get.h"
|
||||
#include "../share/put.h"
|
||||
#include "../share/lset.h"
|
||||
#include "../share/map.h"
|
||||
#include "../share/alloc.h"
|
||||
#include "../share/aux.h"
|
||||
#include "../share/go.h"
|
||||
#include "../share/stack_chg.h"
|
||||
#include "../../../h/em_mnem.h"
|
||||
#include "../../../h/em_spec.h"
|
||||
|
||||
|
||||
/* Stack pollution throws away the ASP instructions after a procedure call.
|
||||
* This saves a lot of code, at the cost of some extra stack space.
|
||||
* ASPs that are part of a loop are not removed.
|
||||
*/
|
||||
|
||||
#define BF_MARK 04
|
||||
#define MARK(b) b->b_flags |= BF_MARK
|
||||
#define NOT_MARKED(b) (!(b->b_flags&BF_MARK))
|
||||
#define IN_LOOP(b) (Lnrelems(b->b_loops) > 0)
|
||||
|
||||
STATIC int Ssp; /* number of optimizations */
|
||||
|
||||
/* According to the EM definition, the stack must be cleaned up
|
||||
* before any return. However, for some backends it causes no harm
|
||||
* if the stack is not cleaned up. If so, we can do Stack Pollution
|
||||
* more globally.
|
||||
*/
|
||||
|
||||
STATIC int globl_sp_allowed;
|
||||
|
||||
|
||||
#define IS_ASP(l) (INSTR(l) == op_asp && TYPE(l) == OPSHORT && SHORT(l) > 0)
|
||||
|
||||
|
||||
STATIC sp_machinit(f)
|
||||
FILE *f;
|
||||
{
|
||||
/* Read target machine dependent information for this phase */
|
||||
char s[100];
|
||||
|
||||
for (;;) {
|
||||
while(getc(f) != '\n');
|
||||
fscanf(f,"%s",s);
|
||||
if (strcmp(s,"%%SP") == 0)break;
|
||||
}
|
||||
fscanf(f,"%d",&globl_sp_allowed);
|
||||
}
|
||||
comb_asps(l1,l2,b)
|
||||
line_p l1,l2;
|
||||
bblock_p b;
|
||||
{
|
||||
assert(INSTR(l1) == op_asp);
|
||||
assert(INSTR(l2) == op_asp);
|
||||
assert(TYPE(l1) == OPSHORT);
|
||||
assert(TYPE(l2) == OPSHORT);
|
||||
|
||||
SHORT(l2) += SHORT(l1);
|
||||
rm_line(l1,b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
stack_pollution(b)
|
||||
bblock_p b;
|
||||
{
|
||||
/* For every pair of successive ASP instructions in basic
|
||||
* block b, try to combine the two into one ASP.
|
||||
*/
|
||||
|
||||
register line_p l;
|
||||
line_p asp,next = b->b_start;
|
||||
bool asp_seen = FALSE;
|
||||
int stack_diff,pop,push;
|
||||
bool ok;
|
||||
|
||||
do {
|
||||
stack_diff = 0;
|
||||
for (l = next; l != (line_p) 0; l = next) {
|
||||
next = l->l_next;
|
||||
if (IS_ASP(l)) break;
|
||||
if (asp_seen) {
|
||||
if (INSTR(l) == op_ret) {
|
||||
stack_diff -= SHORT(l);
|
||||
} else {
|
||||
line_change(l,&ok,&pop,&push);
|
||||
if (!ok || (stack_diff -= pop) < 0) {
|
||||
/* can't eliminate last ASP */
|
||||
asp_seen = FALSE;
|
||||
} else {
|
||||
stack_diff += push;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (asp_seen) {
|
||||
if (l == (line_p) 0) {
|
||||
/* last asp of basic block */
|
||||
if (globl_sp_allowed &&
|
||||
NOT_MARKED(b) && !IN_LOOP(b)) {
|
||||
Ssp++;
|
||||
rm_line(asp,b);
|
||||
}
|
||||
} else {
|
||||
/* try to combine with previous asp */
|
||||
if (SHORT(l) == stack_diff) {
|
||||
Ssp++;
|
||||
comb_asps(asp,l,b);
|
||||
}
|
||||
}
|
||||
}
|
||||
asp = l;
|
||||
asp_seen = TRUE; /* use new ASP for next try! */
|
||||
} while (asp != (line_p) 0);
|
||||
}
|
||||
|
||||
STATIC bool block_save(b)
|
||||
bblock_p b;
|
||||
{
|
||||
|
||||
register line_p l;
|
||||
int stack_diff,pop,push;
|
||||
bool ok;
|
||||
|
||||
stack_diff = 0;
|
||||
for (l = b->b_start; l != (line_p) 0; l = l->l_next) {
|
||||
if (INSTR(l) == op_ret) {
|
||||
stack_diff -= SHORT(l);
|
||||
break;
|
||||
}
|
||||
line_change(l,&ok,&pop,&push);
|
||||
/* printf("instr %d, pop %d,push %d,ok %d\n",INSTR(l),pop,push,ok); */
|
||||
if (!ok || (stack_diff -= pop) < 0) {
|
||||
return FALSE;
|
||||
} else {
|
||||
stack_diff += push;
|
||||
}
|
||||
}
|
||||
return stack_diff >= 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
STATIC mark_pred(b)
|
||||
bblock_p b;
|
||||
{
|
||||
Lindex i;
|
||||
bblock_p x;
|
||||
|
||||
for (i = Lfirst(b->b_pred); i != (Lindex) 0; i = Lnext(i,b->b_pred)) {
|
||||
x = (bblock_p) Lelem(i);
|
||||
if (NOT_MARKED(x)) {
|
||||
MARK(x);
|
||||
mark_pred(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
STATIC mark_unsave_blocks(p)
|
||||
proc_p p;
|
||||
{
|
||||
register bblock_p b;
|
||||
|
||||
for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
|
||||
if (NOT_MARKED(b) && !block_save(b)) {
|
||||
MARK(b);
|
||||
mark_pred(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sp_optimize(p)
|
||||
proc_p p;
|
||||
{
|
||||
register bblock_p b;
|
||||
|
||||
mark_unsave_blocks(p);
|
||||
for (b = p->p_start; b != 0; b = b->b_next) {
|
||||
stack_pollution(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
main(argc,argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
go(argc,argv,no_action,sp_optimize,sp_machinit,no_action);
|
||||
report("stack adjustments deleted",Ssp);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***** DEBUGGING:
|
||||
|
||||
debug_stack_pollution(p)
|
||||
proc_p p;
|
||||
{
|
||||
register bblock_p b;
|
||||
register line_p l;
|
||||
int lcnt,aspcnt,instr;
|
||||
|
||||
for (b = p->p_start; b != 0; b = b->b_next) {
|
||||
lcnt = 0; aspcnt = 0;
|
||||
for (l = b->b_start; l != 0; l= l->l_next) {
|
||||
instr = INSTR(l);
|
||||
if (instr >= sp_fmnem && instr <= sp_lmnem) {
|
||||
lcnt++;
|
||||
if (instr == op_asp && off_set(l) > 0) {
|
||||
aspcnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\t%d\n",aspcnt,lcnt);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user