stupid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include "stupid.h"
  42. /* Two vectors of length `first_temp_reg_num'
  43. pointing to the last insn before the birth
  44. and the last insn before the death
  45. of pseudo-register N. For user-variables only.
  46. This info was set up in stmt.c. */
  47. rtx *reg_birth_insn;
  48. rtx *reg_death_insn;
  49. /* Some constants from the machine description. */
  50. static char fixed_regs[] = FIXED_REGISTERS;
  51. static char call_clobbered_regs[] = CALL_USED_REGISTERS;
  52. static HARD_REG_SET fixed_reg_set, call_clobbered_reg_set;
  53. static HARD_REG_SET reg_class_contents[] = REG_CLASS_CONTENTS;
  54. /* Vector mapping INSN_UIDs to suids.
  55. The suids are like uids but increase monononically always.
  56. We use them to see whether a subroutine call came
  57. between a variable's birth and its death. */
  58. static short *uid_suid;
  59. /* Get the suid of an insn. */
  60. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  61. /* Record the suid of the last CALL_INSN
  62. so we can tell whether a potential combination crosses any calls. */
  63. static int last_call_suid;
  64. /* Element N is suid of insn where life span of pseudo reg N ends.
  65. Element is 0 if register N has not been seen yet on backward scan. */
  66. static short *reg_where_dead;
  67. /* Element N is suid of insn where life span of pseudo reg N begins. */
  68. static short *reg_where_born;
  69. /* Numbers of pseudo-regs to be allocated, highest priority first. */
  70. static short *reg_order;
  71. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  72. at the current point in the instruction stream. */
  73. static char *regs_live;
  74. /* Indexed by insn's suid, the set of hard regs live after that insn. */
  75. static HARD_REG_SET *after_insn_hard_regs;
  76. /* Record that hard reg REGNO is live after insn INSN. */
  77. #define MARK_LIVE_AFTER(INSN,REGNO) \
  78. SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  79. static void stupid_mark_refs ();
  80. static int stupid_reg_better_p ();
  81. /* Stupid life analysis is for the case where only variables declared
  82. `register' go in registers. For this case, we mark all
  83. pseudo-registers that belong to register variables as
  84. dying in the last instruction of the function, and all other
  85. pseudo registers as dying in the last place they are referenced.
  86. Hard registers are marked as dying in the last reference before
  87. the end or before each store into them. */
  88. void
  89. stupid_life_analysis (f, nregs, file)
  90. rtx f;
  91. int nregs;
  92. FILE *file;
  93. {
  94. register int i;
  95. register rtx last, insn;
  96. int max_uid;
  97. bzero (regs_ever_live, sizeof regs_ever_live);
  98. regs_live = (char *) alloca (nregs);
  99. /* First find the last real insn, and count the number of insns,
  100. and assign insns their suids. */
  101. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  102. if (INSN_UID (insn) > i)
  103. i = INSN_UID (insn);
  104. max_uid = i + 1;
  105. uid_suid = (short *) alloca ((i + 1) * sizeof (short));
  106. /* Compute the mapping from uids to suids.
  107. Suids are numbers assigned to insns, like uids,
  108. except that suids increase monotonically through the code. */
  109. last = 0; /* In case of empty function body */
  110. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  111. {
  112. if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  113. || GET_CODE (insn) == JUMP_INSN)
  114. last = insn;
  115. INSN_SUID (insn) = ++i;
  116. }
  117. last_call_suid = 0;
  118. max_regno = nregs;
  119. /* Allocate tables to record info about regs. */
  120. reg_where_dead = (short *) alloca (nregs * sizeof (short));
  121. bzero (reg_where_dead, nregs * sizeof (short));
  122. reg_where_born = (short *) alloca (nregs * sizeof (short));
  123. bzero (reg_where_born, nregs * sizeof (short));
  124. reg_order = (short *) alloca (nregs * sizeof (short));
  125. bzero (reg_order, nregs * sizeof (short));
  126. reg_renumber = (short *) oballoc (nregs * sizeof (short));
  127. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  128. reg_renumber[i] = i;
  129. after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  130. bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  131. /* Express the sets of fixed and call-clobbered regs
  132. as HARD_REG_SETS. These sets are constant
  133. once the initialization here is finished. */
  134. CLEAR_HARD_REG_SET (fixed_reg_set);
  135. CLEAR_HARD_REG_SET (call_clobbered_reg_set);
  136. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  137. {
  138. if (fixed_regs[i])
  139. SET_HARD_REG_BIT (fixed_reg_set, i);
  140. if (call_clobbered_regs[i])
  141. SET_HARD_REG_BIT (call_clobbered_reg_set, i);
  142. }
  143. /* Allocate and zero out many data structures
  144. that will record the data from lifetime analysis. */
  145. allocate_for_life_analysis ();
  146. for (i = 0; i < max_regno; i++)
  147. {
  148. reg_n_deaths[i] = 1;
  149. }
  150. bzero (regs_live, nregs);
  151. /* Find where each pseudo register is born and dies,
  152. by scanning all insns from the end to the start
  153. and noting all mentions of the registers.
  154. Also find where each hard register is live
  155. and record that info in after_insn_hard_regs.
  156. regs_live[I] is 1 if hard reg I is live
  157. at the current point in the scan. */
  158. for (insn = last; insn; insn = PREV_INSN (insn))
  159. {
  160. register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  161. /* Copy the info in regs_live
  162. into the element of after_insn_hard_regs
  163. for the current position in the rtl code. */
  164. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  165. if (regs_live[i])
  166. SET_HARD_REG_BIT (*p, i);
  167. if (regs_live[FUNCTION_VALUE_REGNUM])
  168. SET_HARD_REG_BIT (*p, 1 + FUNCTION_VALUE_REGNUM);
  169. /* Mark all call-clobbered regs as live after each call insn
  170. so that a pseudo whose life span includes this insn
  171. will not go in one of them.
  172. Then mark those regs as all dead for the continuing scan
  173. of the insns before the call. */
  174. if (GET_CODE (insn) == CALL_INSN)
  175. {
  176. last_call_suid = INSN_SUID (insn);
  177. IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  178. call_clobbered_reg_set);
  179. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  180. if (call_clobbered_regs[i])
  181. regs_live[i] = 0;
  182. }
  183. /* Update which hard regs are currently live
  184. and also the birth and death suids of pseudo regs
  185. based on the pattern of this insn. */
  186. if (GET_CODE (insn) == INSN
  187. || GET_CODE (insn) == CALL_INSN
  188. || GET_CODE (insn) == JUMP_INSN)
  189. {
  190. stupid_mark_refs (PATTERN (insn), insn, last, regs_live);
  191. }
  192. }
  193. /* The births and deaths of user register variables
  194. are passed in from stmt.c. Put that info into our format. */
  195. for (i = FIRST_PSEUDO_REGISTER; i < first_temp_reg_num; i++)
  196. {
  197. if (reg_birth_insn[i] == 0 || reg_death_insn[i] == 0)
  198. abort ();
  199. reg_where_born[i] = INSN_SUID (reg_birth_insn[i]) + 1;
  200. reg_where_dead[i] = INSN_SUID (reg_death_insn[i]);
  201. }
  202. /* Now decide the order in which to allocate the pseudo registers. */
  203. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  204. reg_order[i] = i;
  205. qsort (&reg_order[FIRST_PSEUDO_REGISTER],
  206. max_regno - FIRST_PSEUDO_REGISTER, sizeof (short),
  207. stupid_reg_better_p);
  208. /* Now, in that order, try to find hard registers for those pseudo regs. */
  209. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  210. {
  211. register int r = reg_order[i];
  212. enum reg_class class;
  213. /* Now find the best hard-register class for this pseudo register */
  214. if (N_REG_CLASSES > 1)
  215. class = reg_preferred_class (r);
  216. reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], class,
  217. PSEUDO_REGNO_MODE (r),
  218. reg_where_born[r],
  219. reg_where_dead[r]);
  220. /* If no reg available in that class,
  221. try any reg. */
  222. if (reg_renumber[r] == -1)
  223. reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], GENERAL_REGS,
  224. PSEUDO_REGNO_MODE (r),
  225. reg_where_born[r],
  226. reg_where_dead[r]);
  227. }
  228. if (file)
  229. dump_flow_info (file);
  230. }
  231. /* Comparison function for qsort.
  232. Returns -1 (1) if register *R1P is higher priority than *R2P. */
  233. static int
  234. stupid_reg_better_p (r1p, r2p)
  235. short *r1p, *r2p;
  236. {
  237. register int r1 = *r1p, r2 = *r2p;
  238. register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  239. register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  240. if (len1 != len2)
  241. return len2 - len1;
  242. return reg_n_refs[r1] - reg_n_refs[r2];
  243. }
  244. /* Find a block of SIZE words of hard registers in reg_class CLASS
  245. that can hold a value of machine-mode MODE
  246. (but actually we test only the first of the block for holding MODE)
  247. currently free from after insn whose suid is BIRTH
  248. through the insn whose suid is DEATH,
  249. and return the number of the first of them.
  250. Return -1 if such a block cannot be found.
  251. If CALL_PRESERVED is nonzero, insist on registers preserved
  252. over subroutine calls, and return -1 if cannot find such. */
  253. static int
  254. stupid_find_reg (call_preserved, class, mode,
  255. born_insn, dead_insn)
  256. int call_preserved;
  257. enum reg_class class;
  258. enum machine_mode mode;
  259. int born_insn, dead_insn;
  260. {
  261. register int i, ins;
  262. register HARD_REG_SET used, this_reg;
  263. COPY_HARD_REG_SET (used,
  264. call_preserved ? call_clobbered_reg_set : fixed_reg_set);
  265. for (ins = born_insn; ins < dead_insn; ins++)
  266. IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  267. IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  268. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  269. if (! TEST_HARD_REG_BIT (used, i)
  270. && HARD_REGNO_MODE_OK (i, mode))
  271. {
  272. register int j;
  273. register int size1 = HARD_REGNO_NREGS (i, mode);
  274. for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  275. if (j == size1)
  276. {
  277. CLEAR_HARD_REG_SET (this_reg);
  278. while (--j >= 0)
  279. SET_HARD_REG_BIT (this_reg, i + j);
  280. for (ins = born_insn; ins < dead_insn; ins++)
  281. {
  282. IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  283. }
  284. return i;
  285. }
  286. i += j; /* Skip starting points we know will lose */
  287. }
  288. return -1;
  289. }
  290. /* Walk X, noting all assignments and references to registers
  291. and recording what they imply about life spans.
  292. INSN is the current insn, supplied so we can find its suid. */
  293. static void
  294. stupid_mark_refs (x, insn)
  295. rtx x, insn;
  296. {
  297. register RTX_CODE code = GET_CODE (x);
  298. register char *fmt;
  299. register int regno, i;
  300. if (code == SET || code == CLOBBER)
  301. {
  302. if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  303. {
  304. /* Register is being assigned. */
  305. regno = REGNO (SET_DEST (x));
  306. /* Update the life-interval bounds of this reg. */
  307. reg_where_born[regno] = INSN_SUID (insn);
  308. if (reg_where_dead[regno] == 0)
  309. /* The following line is for unused outputs;
  310. they have to be stored somewhere. */
  311. reg_where_dead[regno] = INSN_SUID (insn) + 1;
  312. /* Count the refs of this reg. */
  313. reg_n_refs[regno]++;
  314. /* For hard regs, update the where-live info. */
  315. if (regno < FIRST_PSEUDO_REGISTER)
  316. {
  317. regs_ever_live[regno] = 1;
  318. regs_live[regno] = 0;
  319. /* The following line is for unused outputs;
  320. they have to be stored somewhere. */
  321. MARK_LIVE_AFTER (insn, regno);
  322. }
  323. /* For pseudo regs, record whether they are live across a call. */
  324. else if (last_call_suid < reg_where_dead[regno])
  325. reg_crosses_call[regno] = 1;
  326. }
  327. /* Record references from the value being set,
  328. or from addresses in the place being set if that's not a reg. */
  329. if (code == SET)
  330. {
  331. stupid_mark_refs (SET_SRC (x), insn);
  332. if (GET_CODE (SET_DEST (x)) != REG)
  333. stupid_mark_refs (SET_DEST (x), insn);
  334. }
  335. return;
  336. }
  337. /* Register value being used, not set. */
  338. if (code == REG)
  339. {
  340. regno = REGNO (x);
  341. if (regno < FIRST_PSEUDO_REGISTER)
  342. regs_ever_live[regno] = 1;
  343. reg_where_born[regno] = INSN_SUID (insn);
  344. reg_n_refs[regno]++;
  345. /* Don't deal here with pseudo-regs for user's register variables. */
  346. if (regs_live[regno] == 0
  347. && (regno < FIRST_PSEUDO_REGISTER || regno >= first_temp_reg_num))
  348. {
  349. regs_live[regno] = 1;
  350. reg_where_dead[regno] = INSN_SUID (insn);
  351. }
  352. return;
  353. }
  354. /* Recursive scan of all other rtx's. */
  355. fmt = GET_RTX_FORMAT (code);
  356. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  357. {
  358. if (fmt[i] == 'e')
  359. stupid_mark_refs (XEXP (x, i), insn);
  360. if (fmt[i] == 'E')
  361. {
  362. register int j;
  363. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  364. stupid_mark_refs (XVECEXP (x, i, j), insn);
  365. }
  366. }
  367. }