stupid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This file performs stupid register allocation, which is used
  18. when cc1 gets the -noreg switch (which is when cc does not get -O).
  19. Stupid register allocation goes in place of the the flow_analysis,
  20. local_alloc and global_alloc passes. combine_instructions cannot
  21. be done with stupid allocation because the data flow info that it needs
  22. is not computed here.
  23. In stupid allocation, the only user-defined variables that can
  24. go in registers are those declared "register". They are assumed
  25. to have a life span equal to their scope. Other user variables
  26. are given stack slots in the rtl-generation pass and are not
  27. represented as pseudo regs. A compiler-generated temporary
  28. is assumed to live from its first mention to its last mention.
  29. Since each pseudo-reg's life span is just an interval, it can be
  30. represented as a pair of numbers, each of which identifies an insn by
  31. its position in the function (number of insns before it). The first
  32. thing done for stupid allocation is to compute such a number for each
  33. insn. It is called the suid. Then the life-interval of each
  34. pseudo reg is computed. Then the pseudo regs are ordered by priority
  35. and assigned hard regs in priority order. */
  36. #include <stdio.h>
  37. #include "config.h"
  38. #include "rtl.h"
  39. #include "hard-reg-set.h"
  40. #include "regs.h"
  41. /* Vector mapping INSN_UIDs to suids.
  42. The suids are like uids but increase monononically always.
  43. We use them to see whether a subroutine call came
  44. between a variable's birth and its death. */
  45. static short *uid_suid;
  46. /* Get the suid of an insn. */
  47. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  48. /* Record the suid of the last CALL_INSN
  49. so we can tell whether a potential combination crosses any calls. */
  50. static int last_call_suid;
  51. /* Element N is suid of insn where life span of pseudo reg N ends.
  52. Element is 0 if register N has not been seen yet on backward scan. */
  53. static short *reg_where_dead;
  54. /* Element N is suid of insn where life span of pseudo reg N begins. */
  55. static short *reg_where_born;
  56. /* Numbers of pseudo-regs to be allocated, highest priority first. */
  57. static short *reg_order;
  58. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  59. at the current point in the instruction stream. */
  60. static char *regs_live;
  61. /* Indexed by insn's suid, the set of hard regs live after that insn. */
  62. static HARD_REG_SET *after_insn_hard_regs;
  63. /* Record that hard reg REGNO is live after insn INSN. */
  64. #define MARK_LIVE_AFTER(INSN,REGNO) \
  65. SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  66. static void stupid_mark_refs ();
  67. static int stupid_reg_compare ();
  68. /* Stupid life analysis is for the case where only variables declared
  69. `register' go in registers. For this case, we mark all
  70. pseudo-registers that belong to register variables as
  71. dying in the last instruction of the function, and all other
  72. pseudo registers as dying in the last place they are referenced.
  73. Hard registers are marked as dying in the last reference before
  74. the end or before each store into them. */
  75. void
  76. stupid_life_analysis (f, nregs, file)
  77. rtx f;
  78. int nregs;
  79. FILE *file;
  80. {
  81. register int i;
  82. register rtx last, insn;
  83. int max_uid;
  84. bzero (regs_ever_live, sizeof regs_ever_live);
  85. regs_live = (char *) alloca (nregs);
  86. /* First find the last real insn, and count the number of insns,
  87. and assign insns their suids. */
  88. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  89. if (INSN_UID (insn) > i)
  90. i = INSN_UID (insn);
  91. max_uid = i + 1;
  92. uid_suid = (short *) alloca ((i + 1) * sizeof (short));
  93. /* Compute the mapping from uids to suids.
  94. Suids are numbers assigned to insns, like uids,
  95. except that suids increase monotonically through the code. */
  96. last = 0; /* In case of empty function body */
  97. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  98. {
  99. if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  100. || GET_CODE (insn) == JUMP_INSN)
  101. last = insn;
  102. INSN_SUID (insn) = ++i;
  103. }
  104. last_call_suid = 0;
  105. max_regno = nregs;
  106. /* Allocate tables to record info about regs. */
  107. reg_where_dead = (short *) alloca (nregs * sizeof (short));
  108. bzero (reg_where_dead, nregs * sizeof (short));
  109. reg_where_born = (short *) alloca (nregs * sizeof (short));
  110. bzero (reg_where_born, nregs * sizeof (short));
  111. reg_order = (short *) alloca (nregs * sizeof (short));
  112. bzero (reg_order, nregs * sizeof (short));
  113. reg_renumber = (short *) oballoc (nregs * sizeof (short));
  114. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  115. reg_renumber[i] = i;
  116. after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  117. bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  118. /* Allocate and zero out many data structures
  119. that will record the data from lifetime analysis. */
  120. allocate_for_life_analysis ();
  121. for (i = 0; i < max_regno; i++)
  122. {
  123. reg_n_deaths[i] = 1;
  124. }
  125. bzero (regs_live, nregs);
  126. /* Find where each pseudo register is born and dies,
  127. by scanning all insns from the end to the start
  128. and noting all mentions of the registers.
  129. Also find where each hard register is live
  130. and record that info in after_insn_hard_regs.
  131. regs_live[I] is 1 if hard reg I is live
  132. at the current point in the scan. */
  133. for (insn = last; insn; insn = PREV_INSN (insn))
  134. {
  135. register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  136. /* Copy the info in regs_live
  137. into the element of after_insn_hard_regs
  138. for the current position in the rtl code. */
  139. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  140. if (regs_live[i])
  141. SET_HARD_REG_BIT (*p, i);
  142. /* Mark all call-clobbered regs as live after each call insn
  143. so that a pseudo whose life span includes this insn
  144. will not go in one of them.
  145. Then mark those regs as all dead for the continuing scan
  146. of the insns before the call. */
  147. if (GET_CODE (insn) == CALL_INSN)
  148. {
  149. last_call_suid = INSN_SUID (insn);
  150. IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  151. call_used_reg_set);
  152. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  153. if (call_used_regs[i])
  154. regs_live[i] = 0;
  155. }
  156. /* Update which hard regs are currently live
  157. and also the birth and death suids of pseudo regs
  158. based on the pattern of this insn. */
  159. if (GET_CODE (insn) == INSN
  160. || GET_CODE (insn) == CALL_INSN
  161. || GET_CODE (insn) == JUMP_INSN)
  162. {
  163. stupid_mark_refs (PATTERN (insn), insn);
  164. }
  165. }
  166. /* Now decide the order in which to allocate the pseudo registers. */
  167. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  168. reg_order[i] = i;
  169. qsort (&reg_order[FIRST_PSEUDO_REGISTER],
  170. max_regno - FIRST_PSEUDO_REGISTER, sizeof (short),
  171. stupid_reg_compare);
  172. /* Now, in that order, try to find hard registers for those pseudo regs. */
  173. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  174. {
  175. register int r = reg_order[i];
  176. enum reg_class class;
  177. /* Now find the best hard-register class for this pseudo register */
  178. if (N_REG_CLASSES > 1)
  179. class = reg_preferred_class (r);
  180. reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], class,
  181. PSEUDO_REGNO_MODE (r),
  182. reg_where_born[r],
  183. reg_where_dead[r]);
  184. /* If no reg available in that class,
  185. try any reg. */
  186. if (reg_renumber[r] == -1)
  187. reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], GENERAL_REGS,
  188. PSEUDO_REGNO_MODE (r),
  189. reg_where_born[r],
  190. reg_where_dead[r]);
  191. }
  192. if (file)
  193. dump_flow_info (file);
  194. }
  195. /* Comparison function for qsort.
  196. Returns -1 (1) if register *R1P is higher priority than *R2P. */
  197. static int
  198. stupid_reg_compare (r1p, r2p)
  199. short *r1p, *r2p;
  200. {
  201. register int r1 = *r1p, r2 = *r2p;
  202. register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  203. register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  204. if (len1 != len2)
  205. return len2 - len1;
  206. return reg_n_refs[r1] - reg_n_refs[r2];
  207. }
  208. /* Find a block of SIZE words of hard registers in reg_class CLASS
  209. that can hold a value of machine-mode MODE
  210. (but actually we test only the first of the block for holding MODE)
  211. currently free from after insn whose suid is BIRTH
  212. through the insn whose suid is DEATH,
  213. and return the number of the first of them.
  214. Return -1 if such a block cannot be found.
  215. If CALL_PRESERVED is nonzero, insist on registers preserved
  216. over subroutine calls, and return -1 if cannot find such. */
  217. static int
  218. stupid_find_reg (call_preserved, class, mode,
  219. born_insn, dead_insn)
  220. int call_preserved;
  221. enum reg_class class;
  222. enum machine_mode mode;
  223. int born_insn, dead_insn;
  224. {
  225. register int i, ins;
  226. #ifdef HARD_REG_SET
  227. register /* Declare them register if they are scalars. */
  228. #endif
  229. HARD_REG_SET used, this_reg;
  230. COPY_HARD_REG_SET (used,
  231. call_preserved ? call_used_reg_set : fixed_reg_set);
  232. for (ins = born_insn; ins < dead_insn; ins++)
  233. IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  234. IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  235. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  236. if (! TEST_HARD_REG_BIT (used, i)
  237. && HARD_REGNO_MODE_OK (i, mode))
  238. {
  239. register int j;
  240. register int size1 = HARD_REGNO_NREGS (i, mode);
  241. for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  242. if (j == size1)
  243. {
  244. CLEAR_HARD_REG_SET (this_reg);
  245. while (--j >= 0)
  246. SET_HARD_REG_BIT (this_reg, i + j);
  247. for (ins = born_insn; ins < dead_insn; ins++)
  248. {
  249. IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  250. }
  251. return i;
  252. }
  253. i += j; /* Skip starting points we know will lose */
  254. }
  255. return -1;
  256. }
  257. /* Walk X, noting all assignments and references to registers
  258. and recording what they imply about life spans.
  259. INSN is the current insn, supplied so we can find its suid. */
  260. static void
  261. stupid_mark_refs (x, insn)
  262. rtx x, insn;
  263. {
  264. register RTX_CODE code = GET_CODE (x);
  265. register char *fmt;
  266. register int regno, i;
  267. if (code == SET || code == CLOBBER)
  268. {
  269. if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  270. {
  271. /* Register is being assigned. */
  272. regno = REGNO (SET_DEST (x));
  273. /* For hard regs, update the where-live info. */
  274. if (regno < FIRST_PSEUDO_REGISTER)
  275. {
  276. register int j
  277. = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  278. while (--j >= 0)
  279. {
  280. regs_ever_live[regno+j] = 1;
  281. regs_live[regno+j] = 0;
  282. /* The following line is for unused outputs;
  283. they do get stored even though never used again. */
  284. MARK_LIVE_AFTER (insn, regno);
  285. }
  286. }
  287. /* For pseudo regs, record where born, where dead, number of
  288. times used, and whether live across a call. */
  289. else
  290. {
  291. /* Update the life-interval bounds of this reg. */
  292. reg_where_born[regno] = INSN_SUID (insn);
  293. /* The reg must live at least one insn even
  294. if it is never again used--because it has to go
  295. in SOME hard reg. */
  296. if (reg_where_dead[regno] < INSN_SUID (insn) + 1)
  297. reg_where_dead[regno] = INSN_SUID (insn) + 1;
  298. /* Count the refs of this reg. */
  299. reg_n_refs[regno]++;
  300. if (last_call_suid < reg_where_dead[regno])
  301. reg_crosses_call[regno] = 1;
  302. }
  303. }
  304. /* Record references from the value being set,
  305. or from addresses in the place being set if that's not a reg.
  306. If setting a SUBREG, we treat the entire reg as *used*. */
  307. if (code == SET)
  308. {
  309. stupid_mark_refs (SET_SRC (x), insn);
  310. if (GET_CODE (SET_DEST (x)) != REG)
  311. stupid_mark_refs (SET_DEST (x), insn);
  312. }
  313. return;
  314. }
  315. /* Register value being used, not set. */
  316. if (code == REG)
  317. {
  318. regno = REGNO (x);
  319. if (regno < FIRST_PSEUDO_REGISTER)
  320. {
  321. /* Hard reg: mark it live for continuing scan of previous insns. */
  322. register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  323. while (--j >= 0)
  324. {
  325. regs_ever_live[regno+j] = 1;
  326. regs_live[regno+j] = 1;
  327. }
  328. }
  329. else
  330. {
  331. /* Pseudo reg: record first use, last use and number of uses. */
  332. reg_where_born[regno] = INSN_SUID (insn);
  333. reg_n_refs[regno]++;
  334. if (regs_live[regno] == 0)
  335. {
  336. regs_live[regno] = 1;
  337. reg_where_dead[regno] = INSN_SUID (insn);
  338. }
  339. }
  340. return;
  341. }
  342. /* Recursive scan of all other rtx's. */
  343. fmt = GET_RTX_FORMAT (code);
  344. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  345. {
  346. if (fmt[i] == 'e')
  347. stupid_mark_refs (XEXP (x, i), insn);
  348. if (fmt[i] == 'E')
  349. {
  350. register int j;
  351. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  352. stupid_mark_refs (XVECEXP (x, i, j), insn);
  353. }
  354. }
  355. }