il.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* $Id$ */
  2. /*
  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".
  5. */
  6. /* I N L I N E S U B S T I T U T I O N */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <em_path.h>
  11. #include <em_mnem.h>
  12. #include <em_pseu.h>
  13. #include "../share/types.h"
  14. #include "il.h"
  15. #include "../share/debug.h"
  16. #include "../share/alloc.h"
  17. #include "../share/global.h"
  18. #include "../share/lset.h"
  19. #include "../share/files.h"
  20. #include "../share/map.h"
  21. #include "il_aux.h"
  22. #include "il1_anal.h"
  23. #include "il2_aux.h"
  24. #include "il3_subst.h"
  25. #include "../share/get.h"
  26. #include "../share/put.h"
  27. #include "../share/go.h"
  28. int calnr;
  29. int complete_program;
  30. calcnt_p cchead; /* call-count info of current proc */
  31. STATIC long space = 0;
  32. STATIC long total_size = 0;
  33. STATIC char cname[128] = TMP_DIR;
  34. STATIC char ccname[128] = TMP_DIR;
  35. /* For debugging only */
  36. STATIC char sname[128] = TMP_DIR;
  37. STATIC int kp_temps = 0;
  38. int Ssubst;
  39. #ifdef VERBOSE
  40. int Senv,Srecursive,Slocals,Sinstrlab,Sparsefails,Spremoved,Scals;
  41. int Sbig_caller,Sdispensable,Schangedcallee,Sbigcallee,Sspace,Szeroratio;
  42. #endif
  43. /* P A S S 1
  44. *
  45. * Pass 1 reads and analyses the EM text and the CFG.
  46. * It determines for every procedure if it may be expanded
  47. * in line and how it uses its formal parameters.
  48. * It also collects all calls appearing in the program and
  49. * recognizes the actual parameters of every call.
  50. * The call descriptors are put in a file (calfile).
  51. */
  52. pass1(lnam,bnam,cnam)
  53. char *lnam, *bnam, *cnam;
  54. {
  55. FILE *f, *gf, *cf, *ccf; /* The EM input, the basic block graph,
  56. * the call-list file and the calcnt file.
  57. */
  58. long laddr;
  59. bblock_p g;
  60. short kind;
  61. line_p l;
  62. f = openfile(lnam,"r");
  63. gf = openfile(bnam,"r");
  64. cf = openfile(cnam,"w");
  65. ccf = openfile(ccname,"w");
  66. mesregs = Lempty_set();
  67. apriori(fproc);
  68. /* use information from the procedure table to
  69. * see which calls certainly cannot be expanded.
  70. */
  71. while(TRUE) {
  72. laddr = ftell(f);
  73. if (!getunit(gf,f,&kind,&g,&l,&curproc,TRUE)) break;
  74. /* Read the control flow graph and EM text of
  75. * one procedure and analyze it.
  76. */
  77. if (kind == LDATA) {
  78. remunit(LDATA,(proc_p) 0,l);
  79. continue;
  80. }
  81. OUTTRACE("flow graph of proc %d read",curproc->p_id);
  82. assert(INSTR(g->b_start) == ps_pro);
  83. curproc->p_start = g;
  84. curproc->P_LADDR = laddr;
  85. /* address of em text in em-file */
  86. /* address of graph in basic block file */
  87. curproc->P_SIZE = proclength(curproc); /* #instructions */
  88. total_size += curproc->P_SIZE;
  89. if (BIG_PROC(curproc)) {
  90. /* curproc is too large to be expanded in line */
  91. UNSUITABLE(curproc);
  92. }
  93. calnr = 0;
  94. anal_proc(curproc,cf,ccf);
  95. OUTTRACE("proc %d processed",curproc->p_id);
  96. remunit(LTEXT,curproc,(line_p) 0);
  97. /* remove control flow graph + text */
  98. OUTTRACE("graph of proc %d removed",curproc->p_id);
  99. Ldeleteset(mesregs);
  100. mesregs = Lempty_set();
  101. }
  102. fclose(f);
  103. fclose(gf);
  104. fclose(cf);
  105. fclose(ccf);
  106. }
  107. /* P A S S 2
  108. *
  109. * Pass 2 reads the calfile and determines which calls should
  110. * be expanded in line. It does not use the EM text.
  111. */
  112. STATIC char cname2[128] = TMP_DIR;
  113. pass2(cnam,space)
  114. char *cnam;
  115. long space;
  116. {
  117. FILE *cf, *cf2, *ccf;
  118. call_p c,a;
  119. cf = openfile(cnam,"r");
  120. cf2 = openfile(cname2,"w");
  121. ccf = openfile(ccname,"r");
  122. while ((c = getcall(cf)) != (call_p) 0) {
  123. /* process all calls */
  124. if (SUITABLE(c->cl_proc) && anal_params(c)) {
  125. /* called proc. may be put in line */
  126. /* see which parameters may be put in line */
  127. assign_ratio(c); /* assign a rank */
  128. a = abstract(c); /* abstract essential info */
  129. append_abstract(a,a->cl_caller);
  130. /* put it in call-list of calling proc. */
  131. putcall(c,cf2,(short) 0);
  132. } else {
  133. rem_call(c);
  134. }
  135. }
  136. select_calls(fproc,ccf,space);
  137. fclose(cf); if (! kp_temps) unlink(cnam);
  138. fclose(cf2);
  139. fclose(ccf); if (! kp_temps) unlink(ccname);
  140. cf2 = openfile(cname2,"r");
  141. add_actuals(fproc,cf2);
  142. cleancals(fproc); /* remove calls that were not selected */
  143. /* add actual parameters to each selected call */
  144. fclose(cf2); if (! kp_temps) unlink(cname2);
  145. }
  146. /* P A S S 3
  147. *
  148. * pass 3 reads the substitution file and performs all
  149. * substitutions described in that file. It reads the
  150. * original EM text and produced a new (optimized)
  151. * EM textfile.
  152. */
  153. pass3(lnam,lnam2)
  154. char *lnam,*lnam2;
  155. {
  156. bool verbose = TRUE;
  157. FILE *lfile, *lfilerand, *lfile2, *sfile;
  158. call_p c,next;
  159. line_p l,startscan,cal;
  160. short lastcid; /* last call-id seen */
  161. lfile = openfile(lnam, "r");
  162. lfilerand = openfile(lnam, "r");
  163. lfile2 = openfile(lnam2,"w");
  164. if (verbose) {
  165. sfile = openfile(sname,"w");
  166. }
  167. mesregs = Lempty_set();
  168. while ((l = get_text(lfile,&curproc)) != (line_p) 0) {
  169. if (curproc == (proc_p) 0) {
  170. /* Just a data-unit; no real instructions */
  171. putlines(l->l_next,lfile2);
  172. oldline(l);
  173. continue;
  174. }
  175. if (IS_DISPENSABLE(curproc)) {
  176. liquidate(curproc,l->l_next);
  177. } else {
  178. startscan = l->l_next;
  179. lastcid = 0;
  180. for (c = curproc->P_CALS; c != (call_p) 0; c = next) {
  181. next = c->cl_cdr;
  182. cal = scan_to_cal(startscan,c->cl_id - lastcid);
  183. assert (cal != (line_p) 0);
  184. startscan = scan_to_cal(cal->l_next,1);
  185. /* next CAL */
  186. lastcid = c->cl_id;
  187. /* next CAL after current one */
  188. substitute(lfilerand,c,cal,l->l_next);
  189. if (verbose) {
  190. putcall(c,sfile,0);
  191. } else {
  192. rem_call(c);
  193. }
  194. }
  195. }
  196. putlines(l->l_next,lfile2);
  197. Ldeleteset(mesregs);
  198. mesregs = Lempty_set();
  199. oldline(l);
  200. }
  201. fclose(lfile);
  202. fclose(lfile2);
  203. if (verbose) {
  204. fclose(sfile);
  205. if (! kp_temps) unlink(sname);
  206. }
  207. }
  208. STATIC il_extptab(ptab)
  209. proc_p ptab;
  210. {
  211. /* Allocate space for extension of proctable entries.
  212. * Also, initialise some of the fields just allocated.
  213. */
  214. register proc_p p;
  215. for (p = ptab; p != (proc_p) 0; p = p->p_next) {
  216. p->p_extend = newilpx();
  217. p->P_ORGLABELS = p->p_nrlabels;
  218. p->P_ORGLOCALS = p->p_localbytes;
  219. }
  220. }
  221. STATIC il_cleanptab(ptab)
  222. proc_p ptab;
  223. {
  224. /* De-allocate space for extensions */
  225. register proc_p p;
  226. for (p = ptab; p != (proc_p) 0; p = p->p_next) {
  227. oldilpx(p->p_extend);
  228. }
  229. }
  230. #ifdef VERBOSE
  231. Sdiagnostics()
  232. {
  233. /* print statictical information */
  234. fprintf(stderr,"STATISTICS:\n");
  235. fprintf(stderr,"Info about procedures:\n");
  236. fprintf(stderr,"environment accessed: %d\n",Senv);
  237. fprintf(stderr,"recursive: %d\n",Srecursive);
  238. fprintf(stderr,"too many locals: %d\n",Slocals);
  239. fprintf(stderr,"instr. lab in data block: %d\n",Sinstrlab);
  240. fprintf(stderr,"procedures removed: %d\n",Spremoved);
  241. fprintf(stderr,"\nInfo about calls:\n");
  242. fprintf(stderr,"total number of calls: %d\n",Scals);
  243. fprintf(stderr,"total number of calls substituted: %d\n",Ssubst);
  244. fprintf(stderr,"parser failed: %d\n",Sparsefails);
  245. fprintf(stderr,"caller too big: %d\n",Sbig_caller);
  246. fprintf(stderr,"caller dispensable: %d\n",Sdispensable);
  247. fprintf(stderr,"callee is changed: %d\n",Schangedcallee);
  248. fprintf(stderr,"callee too big: %d\n",Sbigcallee);
  249. fprintf(stderr,"no space available: %d\n",Sspace);
  250. fprintf(stderr,"zero ratio: %d\n",Szeroratio);
  251. }
  252. #endif
  253. il_flags(p)
  254. char *p;
  255. {
  256. switch(*p++) {
  257. case 's':
  258. while (*p != '\0') {
  259. space = 10*space +*p++ -'0';
  260. }
  261. break;
  262. case 'a':
  263. complete_program = 1;
  264. break;
  265. case 't':
  266. strcpy(cname, ".");
  267. strcpy(ccname, ".");
  268. strcpy(sname, ".");
  269. strcpy(cname2, ".");
  270. kp_temps = 1;
  271. break;
  272. }
  273. }
  274. main(argc,argv)
  275. int argc;
  276. char *argv[];
  277. {
  278. FILE *f;
  279. go(argc,argv,no_action,no_action,no_action,il_flags);
  280. il_extptab(fproc); /* add extended data structures */
  281. strcat(cname, "/ego.i1.XXXXXX");
  282. strcat(ccname, "/ego.i2.XXXXXX");
  283. strcat(sname, "/ego.i3.XXXXXX");
  284. strcat(cname2, "/ego.i4.XXXXXX");
  285. mktemp(cname);
  286. mktemp(ccname);
  287. mktemp(sname);
  288. mktemp(cname2);
  289. pass1(lname,bname,cname); /* grep calls, analyse procedures */
  290. space = total_size * space / 100 ;
  291. pass2(cname,space); /* select calls to be expanded */
  292. pass3(lname,lname2); /* do substitutions */
  293. f = openfile(dname2,"w");
  294. il_cleanptab(fproc); /* remove extended data structures */
  295. putdtable(fdblock,f);
  296. f = openfile(pname2,"w");
  297. putptable(fproc,f,FALSE);
  298. report("inline substitutions",Ssubst);
  299. #ifdef VERBOSE
  300. if (verbose_flag) {
  301. Sdiagnostics();
  302. }
  303. #endif
  304. #ifdef DEBUG
  305. core_usage();
  306. #endif
  307. exit(0);
  308. }