il.c 8.4 KB

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