il.c 8.1 KB

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