il.c 8.2 KB

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