1 /* $Id: il.c,v 1.11 1994/06/24 10:25:12 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
6 /* I N L I N E S U B S T I T U T I O N */
11 #include "../share/types.h"
13 #include "../share/debug.h"
14 #include "../share/alloc.h"
15 #include "../share/global.h"
16 #include "../share/lset.h"
17 #include "../share/files.h"
18 #include "../share/map.h"
22 #include "il3_subst.h"
23 #include "../share/get.h"
24 #include "../share/put.h"
25 #include "../share/go.h"
29 calcnt_p cchead; /* call-count info of current proc */
30 STATIC long space = 0;
31 STATIC long total_size = 0;
33 STATIC char cname[128] = TMP_DIR;
34 STATIC char ccname[128] = TMP_DIR;
36 /* For debugging only */
37 STATIC char sname[128] = TMP_DIR;
38 STATIC int kp_temps = 0;
42 int Senv,Srecursive,Slocals,Sinstrlab,Sparsefails,Spremoved,Scals;
43 int Sbig_caller,Sdispensable,Schangedcallee,Sbigcallee,Sspace,Szeroratio;
48 * Pass 1 reads and analyses the EM text and the CFG.
49 * It determines for every procedure if it may be expanded
50 * in line and how it uses its formal parameters.
51 * It also collects all calls appearing in the program and
52 * recognizes the actual parameters of every call.
53 * The call descriptors are put in a file (calfile).
57 char *lnam, *bnam, *cnam;
59 FILE *f, *gf, *cf, *ccf; /* The EM input, the basic block graph,
60 * the call-list file and the calcnt file.
67 f = openfile(lnam,"r");
68 gf = openfile(bnam,"r");
69 cf = openfile(cnam,"w");
70 ccf = openfile(ccname,"w");
71 mesregs = Lempty_set();
73 /* use information from the procedure table to
74 * see which calls certainly cannot be expanded.
78 if (!getunit(gf,f,&kind,&g,&l,&curproc,TRUE)) break;
79 /* Read the control flow graph and EM text of
80 * one procedure and analyze it.
83 remunit(LDATA,(proc_p) 0,l);
86 OUTTRACE("flow graph of proc %d read",curproc->p_id);
87 assert(INSTR(g->b_start) == ps_pro);
89 curproc->P_LADDR = laddr;
90 /* address of em text in em-file */
91 /* address of graph in basic block file */
92 curproc->P_SIZE = proclength(curproc); /* #instructions */
93 total_size += curproc->P_SIZE;
94 if (BIG_PROC(curproc)) {
95 /* curproc is too large to be expanded in line */
99 anal_proc(curproc,cf,ccf);
100 OUTTRACE("proc %d processed",curproc->p_id);
101 remunit(LTEXT,curproc,(line_p) 0);
102 /* remove control flow graph + text */
103 OUTTRACE("graph of proc %d removed",curproc->p_id);
105 mesregs = Lempty_set();
117 * Pass 2 reads the calfile and determines which calls should
118 * be expanded in line. It does not use the EM text.
123 STATIC char cname2[128] = TMP_DIR;
129 FILE *cf, *cf2, *ccf;
132 cf = openfile(cnam,"r");
133 cf2 = openfile(cname2,"w");
134 ccf = openfile(ccname,"r");
135 while ((c = getcall(cf)) != (call_p) 0) {
136 /* process all calls */
137 if (SUITABLE(c->cl_proc) && anal_params(c)) {
138 /* called proc. may be put in line */
139 /* see which parameters may be put in line */
140 assign_ratio(c); /* assign a rank */
141 a = abstract(c); /* abstract essential info */
142 append_abstract(a,a->cl_caller);
143 /* put it in call-list of calling proc. */
144 putcall(c,cf2,(short) 0);
149 select_calls(fproc,ccf,space);
150 fclose(cf); if (! kp_temps) unlink(cnam);
152 fclose(ccf); if (! kp_temps) unlink(ccname);
153 cf2 = openfile(cname2,"r");
154 add_actuals(fproc,cf2);
155 cleancals(fproc); /* remove calls that were not selected */
156 /* add actual parameters to each selected call */
157 fclose(cf2); if (! kp_temps) unlink(cname2);
164 * pass 3 reads the substitution file and performs all
165 * substitutions described in that file. It reads the
166 * original EM text and produced a new (optimized)
175 FILE *lfile, *lfilerand, *lfile2, *sfile;
177 line_p l,startscan,cal;
178 short lastcid; /* last call-id seen */
180 lfile = openfile(lnam, "r");
181 lfilerand = openfile(lnam, "r");
182 lfile2 = openfile(lnam2,"w");
184 sfile = openfile(sname,"w");
186 mesregs = Lempty_set();
187 while ((l = get_text(lfile,&curproc)) != (line_p) 0) {
188 if (curproc == (proc_p) 0) {
189 /* Just a data-unit; no real instructions */
190 putlines(l->l_next,lfile2);
194 if (IS_DISPENSABLE(curproc)) {
195 liquidate(curproc,l->l_next);
197 startscan = l->l_next;
199 for (c = curproc->P_CALS; c != (call_p) 0; c = next) {
201 cal = scan_to_cal(startscan,c->cl_id - lastcid);
202 assert (cal != (line_p) 0);
203 startscan = scan_to_cal(cal->l_next,1);
206 /* next CAL after current one */
207 substitute(lfilerand,c,cal,l->l_next);
215 putlines(l->l_next,lfile2);
217 mesregs = Lempty_set();
224 if (! kp_temps) unlink(sname);
229 STATIC il_extptab(ptab)
232 /* Allocate space for extension of proctable entries.
233 * Also, initialise some of the fields just allocated.
238 for (p = ptab; p != (proc_p) 0; p = p->p_next) {
239 p->p_extend = newilpx();
240 p->P_ORGLABELS = p->p_nrlabels;
241 p->P_ORGLOCALS = p->p_localbytes;
245 STATIC il_cleanptab(ptab)
248 /* De-allocate space for extensions */
252 for (p = ptab; p != (proc_p) 0; p = p->p_next) {
253 oldilpx(p->p_extend);
260 /* print statictical information */
262 fprintf(stderr,"STATISTICS:\n");
263 fprintf(stderr,"Info about procedures:\n");
264 fprintf(stderr,"environment accessed: %d\n",Senv);
265 fprintf(stderr,"recursive: %d\n",Srecursive);
266 fprintf(stderr,"too many locals: %d\n",Slocals);
267 fprintf(stderr,"instr. lab in data block: %d\n",Sinstrlab);
268 fprintf(stderr,"procedures removed: %d\n",Spremoved);
269 fprintf(stderr,"\nInfo about calls:\n");
270 fprintf(stderr,"total number of calls: %d\n",Scals);
271 fprintf(stderr,"total number of calls substituted: %d\n",Ssubst);
272 fprintf(stderr,"parser failed: %d\n",Sparsefails);
273 fprintf(stderr,"caller too big: %d\n",Sbig_caller);
274 fprintf(stderr,"caller dispensable: %d\n",Sdispensable);
275 fprintf(stderr,"callee is changed: %d\n",Schangedcallee);
276 fprintf(stderr,"callee too big: %d\n",Sbigcallee);
277 fprintf(stderr,"no space available: %d\n",Sspace);
278 fprintf(stderr,"zero ratio: %d\n",Szeroratio);
288 space = 10*space +*p++ -'0';
292 complete_program = 1;
310 go(argc,argv,no_action,no_action,no_action,il_flags);
311 il_extptab(fproc); /* add extended data structures */
312 strcat(cname, "/ego.i1.XXXXXX");
313 strcat(ccname, "/ego.i2.XXXXXX");
314 strcat(sname, "/ego.i3.XXXXXX");
315 strcat(cname2, "/ego.i4.XXXXXX");
320 pass1(lname,bname,cname); /* grep calls, analyse procedures */
321 space = total_size * space / 100 ;
322 pass2(cname,space); /* select calls to be expanded */
323 pass3(lname,lname2); /* do substitutions */
324 f = openfile(dname2,"w");
325 il_cleanptab(fproc); /* remove extended data structures */
326 putdtable(fdblock,f);
327 f = openfile(pname2,"w");
328 putptable(fproc,f,FALSE);
329 report("inline substitutions",Ssubst);