global-alloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* Allocate registers for pseudo-registers that span basic blocks.
  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. #include <stdio.h>
  18. #include "config.h"
  19. #include "rtl.h"
  20. #include "basic-block.h"
  21. #include "hard-reg-set.h"
  22. #include "regs.h"
  23. #include "insn-config.h"
  24. /* This pass of the compiler performs global register allocation.
  25. It assigns hard register numbers to all the pseudo registers
  26. that were not handled in local_alloc. Assignments are recorded
  27. in the vector reg_renumber, not by changing the rtl code.
  28. (Such changes are made by final). The entry point is
  29. the function global_alloc.
  30. After allocation is complete, the reload pass is run as a subroutine
  31. of this pass, so that when a pseudo reg loses its hard reg due to
  32. spilling it is possible to make a second attempt to find a hard
  33. reg for it. The reload pass is independent in other respects
  34. and it is run even when stupid register allocation is in use.
  35. 1. count the pseudo-registers still needing allocation
  36. and assign allocation-numbers (allocnos) to them.
  37. Set up tables reg_allocno and allocno_reg to map
  38. reg numbers to allocnos and vice versa.
  39. max_allocno gets the number of allocnos in use.
  40. 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
  41. Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
  42. for conflicts between allocnos and explicit hard register use
  43. (which includes use of pseudo-registers allocated by local_alloc).
  44. 3. for each basic block
  45. walk forward through the block, recording which
  46. unallocated registers and which hardware registers are live.
  47. Build the conflict matrix between the unallocated registers
  48. and another of unallocated registers versus hardware registers.
  49. Someday also record the preferred hardware registers
  50. for each unallocated one.
  51. 4. Sort a table of the allocnos into order of
  52. desirability of the variables.
  53. 5. Allocate the variables in that order; each if possible into
  54. an preferred register, else into another register. */
  55. /* Number of pseudo-registers still requiring allocation
  56. (not allocated by local_allocate). */
  57. static int max_allocno;
  58. /* Indexed by (pseudo) reg number, gives the allocno, or -1
  59. for pseudo registers already allocated by local_allocate. */
  60. static int *reg_allocno;
  61. /* Indexed by allocno, gives the reg number. */
  62. static int *allocno_reg;
  63. /* A vector of the integers from 0 to max_allocno-1,
  64. sorted in the order of first-to-be-allocated first. */
  65. static int *allocno_order;
  66. /* Indexed by an allocno, gives the number of consecutive
  67. hard registers needed by that pseudo reg. */
  68. static int *allocno_size;
  69. /* max_allocno by max_allocno array of bits,
  70. recording whether two allocno's conflict (can't go in the same
  71. hardware register).
  72. `conflicts' is not symmetric; a conflict between allocno's i and j
  73. is recorded either in element i,j or in element j,i. */
  74. static int *conflicts;
  75. /* Number of ints require to hold max_allocno bits.
  76. This is the length of a row in `conflicts'. */
  77. static int allocno_row_words;
  78. /* Two macros to test or store 1 in an element of `conflicts'. */
  79. #define CONFLICTP(I, J) \
  80. (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
  81. & (1 << ((J) % INT_BITS)))
  82. #define SET_CONFLICT(I, J) \
  83. (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
  84. |= (1 << ((J) % INT_BITS)))
  85. /* Set of hard regs currently live (during scan of all insns). */
  86. static HARD_REG_SET hard_regs_live;
  87. /* Indexed by N, set of hard regs conflicting with allocno N. */
  88. static HARD_REG_SET *hard_reg_conflicts;
  89. #if 0
  90. /* Indexed by N, set of hard regs preferred by allocno N.
  91. This was intended to be used to make allocnos go into regs
  92. that they are loaded from, when possible, to reduce register shuffling. */
  93. static HARD_REG_SET *hard_reg_preferences;
  94. #endif
  95. /* Test a bit in TABLE, a vector of HARD_REG_SETs,
  96. for vector element I, and hard register number J. */
  97. #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)
  98. /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
  99. #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)
  100. /* Bit mask for allocnos live at current point in the scan. */
  101. static int *allocnos_live;
  102. #define INT_BITS HOST_BITS_PER_INT
  103. /* Test, set or clear bit number I in allocnos_live,
  104. a bit vector indexed by allocno. */
  105. #define ALLOCNO_LIVE_P(I) \
  106. (allocnos_live[(I) / INT_BITS] & (1 << ((I) % INT_BITS)))
  107. #define SET_ALLOCNO_LIVE(I) \
  108. (allocnos_live[(I) / INT_BITS] |= (1 << ((I) % INT_BITS)))
  109. #define CLEAR_ALLOCNO_LIVE(I) \
  110. (allocnos_live[(I) / INT_BITS] &= ~(1 << ((I) % INT_BITS)))
  111. static int alloc_before_p ();
  112. static void mark_reg_store ();
  113. static void mark_reg_live_nc ();
  114. static void mark_reg_death ();
  115. static void dump_conflicts ();
  116. static void find_reg ();
  117. static void global_conflicts ();
  118. static void record_conflicts ();
  119. /* Tables describing and classifying the hardware registers. */
  120. /* Indexed by hard register number, contains 1 for registers
  121. that are fixed use (stack pointer, pc, frame pointer, etc.).
  122. These are the registers that cannot be used to allocate
  123. a pseudo reg whose life does not cross calls. */
  124. static char fixed_regs[] = FIXED_REGISTERS;
  125. /* Indexed by hard register number, contains 1 for registers
  126. that are fixed use or are clobbered by function calls.
  127. These are the registers that cannot be used to allocate
  128. a pseudo reg whose life crosses calls. */
  129. static char call_clobbered_regs[] = CALL_USED_REGISTERS;
  130. static HARD_REG_SET fixed_reg_set, call_clobbered_reg_set;
  131. /* Indexed by register class (cast as an int),
  132. gives the set of registers in that class. */
  133. static HARD_REG_SET reg_class_contents[] = REG_CLASS_CONTENTS;
  134. /* Perform allocation of pseudo-registers not allocated by local_alloc.
  135. FILE is a file to output debugging information on,
  136. or zero if such output is not desired. */
  137. void
  138. global_alloc (file)
  139. FILE *file;
  140. {
  141. register int i;
  142. register rtx insn;
  143. int spills;
  144. /* Initialize "constant" tables. */
  145. CLEAR_HARD_REG_SET (fixed_reg_set);
  146. CLEAR_HARD_REG_SET (call_clobbered_reg_set);
  147. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  148. {
  149. if (fixed_regs[i])
  150. SET_HARD_REG_BIT (fixed_reg_set, i);
  151. if (call_clobbered_regs[i])
  152. SET_HARD_REG_BIT (call_clobbered_reg_set, i);
  153. }
  154. max_allocno = 0;
  155. /* Establish mappings from register number to allocation number
  156. and vice versa. In the process, count the allocnos. */
  157. reg_allocno = (int *) alloca (max_regno * sizeof (int));
  158. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  159. reg_allocno[i] = -1;
  160. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  161. /* Note that reg_live_length[i] < 0 indicates a "constant" reg
  162. that we are supposed to refrain from putting in a hard reg. */
  163. if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] >= 0)
  164. {
  165. reg_allocno[i] = max_allocno++;
  166. if (reg_live_length[i] == 0)
  167. abort ();
  168. }
  169. else
  170. reg_allocno[i] = -1;
  171. allocno_reg = (int *) alloca (max_allocno * sizeof (int));
  172. allocno_size = (int *) alloca (max_allocno * sizeof (int));
  173. bzero (allocno_size, max_allocno * sizeof (int));
  174. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  175. if (reg_allocno[i] >= 0)
  176. {
  177. allocno_reg[reg_allocno[i]] = i;
  178. allocno_size[reg_allocno[i]] = PSEUDO_REGNO_SIZE (i);
  179. }
  180. /* Allocate the space for the conflict tables. */
  181. hard_reg_conflicts = (HARD_REG_SET *)
  182. alloca (max_allocno * sizeof (int));
  183. bzero (hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
  184. #if 0
  185. hard_reg_preferences = (HARD_REG_SET *)
  186. alloca (max_allocno * sizeof (int));
  187. bzero (hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
  188. #endif
  189. allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
  190. conflicts = (int *)
  191. alloca (max_allocno * allocno_row_words * sizeof (int));
  192. bzero (conflicts, max_allocno * allocno_row_words * sizeof (int));
  193. allocnos_live = (int *) alloca (allocno_row_words * sizeof (int));
  194. /* If there is work to be done (at least one reg to allocate),
  195. perform global conflict analysis and allocate the regs. */
  196. if (max_allocno > 0)
  197. {
  198. /* Scan all the insns and compute the conflicts among allocnos
  199. and between allocnos and hard regs. */
  200. global_conflicts ();
  201. /* Determine the order to allocate the remaining pseudo registers. */
  202. allocno_order = (int *) alloca (max_allocno * sizeof (int));
  203. for (i = 0; i < max_allocno; i++)
  204. allocno_order[i] = i;
  205. /* Default the size to 1, since alloc_before_p uses it to divide by. */
  206. for (i = 0; i < max_allocno; i++)
  207. if (allocno_size[i] == 0)
  208. allocno_size[i] = 1;
  209. qsort (allocno_order, max_allocno, sizeof (int), alloc_before_p);
  210. if (file)
  211. dump_conflicts (file);
  212. /* Try allocating them, one by one, in that order. */
  213. for (i = 0; i < max_allocno; i++)
  214. {
  215. /* If we have more than one register class,
  216. first try allocating in the class that is cheapest
  217. for this pseudo-reg. If that fails, try any reg. */
  218. if (N_REG_CLASSES > 1)
  219. {
  220. find_reg (allocno_order[i], 0, 0);
  221. if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  222. continue;
  223. }
  224. find_reg (allocno_order[i], 0, 1);
  225. }
  226. }
  227. /* Do the reloads now while the allocno data still exist, so that we can
  228. try to assign new hard regs to any pseudo regs that are spilled. */
  229. if (n_basic_blocks > 0)
  230. reload (basic_block_head[0], 1, file);
  231. }
  232. /* Sort predicate for ordering the allocnos.
  233. Returns -1 (1) if *v1 should be allocated before (after) *v2. */
  234. static int
  235. alloc_before_p (v1, v2)
  236. int *v1, *v2;
  237. {
  238. register int r1 = allocno_reg[*v1];
  239. register int r2 = allocno_reg[*v2];
  240. register double v
  241. = ((double) (floor_log2 (reg_n_refs[r1]) * reg_n_refs[r1])
  242. / (reg_live_length[r1] * allocno_size[*v1]))
  243. - ((double) (floor_log2 (reg_n_refs[r2]) * reg_n_refs[r2])
  244. / (reg_live_length[r2] * allocno_size[*v2]));
  245. if (v < 0)
  246. return 1;
  247. if (v > 0)
  248. return -1;
  249. return 0;
  250. }
  251. /* Scan the rtl code and record all conflicts in the conflict matrices. */
  252. static void
  253. global_conflicts ()
  254. {
  255. register int b, i;
  256. register rtx insn;
  257. short *block_start_allocnos;
  258. block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
  259. for (b = 0; b < n_basic_blocks; b++)
  260. {
  261. bzero (allocnos_live, allocno_row_words * sizeof (int));
  262. /* Initialize table of registers currently live
  263. to the state at the beginning of this basic block.
  264. This also marks the conflicts among them.
  265. For pseudo-regs, there is only one bit for each one
  266. no matter how many hard regs it occupies.
  267. This is ok; we know the size from PSEUDO_REGNO_SIZE.
  268. For explicit hard regs, we cannot know the size that way
  269. since one hard reg can be used with various sizes.
  270. Therefore, we must require that all the hard regs
  271. implicitly live as part of a multi-word hard reg
  272. are explicitly marked in basic_block_live_at_start. */
  273. {
  274. register int offset, bit;
  275. register regset old = basic_block_live_at_start[b];
  276. int ax = 0;
  277. #ifdef HARD_REG_SET
  278. hard_regs_live = old[0];
  279. #else
  280. COPY_HARD_REG_SET (hard_regs_live, old);
  281. #endif
  282. for (offset = 0, i = 0; offset < regset_size; offset++)
  283. if (old[offset] == 0)
  284. i += HOST_BITS_PER_INT;
  285. else
  286. for (bit = 1; bit; bit <<= 1, i++)
  287. {
  288. if (i >= max_regno)
  289. break;
  290. if (old[offset] & bit)
  291. {
  292. register int a = reg_allocno[i];
  293. if (a >= 0)
  294. {
  295. SET_ALLOCNO_LIVE (a);
  296. block_start_allocnos[ax++] = a;
  297. }
  298. else if ((a = reg_renumber[i]) >= 0)
  299. mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
  300. }
  301. }
  302. /* Record that each allocno now live conflicts with each other
  303. allocno now live, and with each hard reg now live. */
  304. record_conflicts (block_start_allocnos, ax);
  305. }
  306. insn = basic_block_head[b];
  307. /* Scan the code of this basic block, noting which allocnos
  308. and hard regs are born or die. When one is born,
  309. record a conflict with all others currently live. */
  310. while (1)
  311. {
  312. register RTX_CODE code = GET_CODE (insn);
  313. register rtx link;
  314. rtx regs_set[MAX_SETS_PER_INSN];
  315. int n_regs_set = 0;
  316. if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  317. {
  318. /* Mark any registers dead after INSN as dead now. */
  319. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  320. if ((enum reg_note) GET_MODE (link) == REG_DEAD)
  321. mark_reg_death (XEXP (link, 0), 0);
  322. /* Mark any registers set in INSN as live,
  323. and mark them as conflicting with all other live regs. */
  324. if (GET_CODE (PATTERN (insn)) == SET
  325. && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  326. {
  327. register rtx z = SET_DEST (PATTERN (insn));
  328. regs_set[n_regs_set++] = z;
  329. mark_reg_store (z, insn);
  330. }
  331. if (GET_CODE (PATTERN (insn)) == SET
  332. && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG)
  333. {
  334. register rtx z = SUBREG_REG (SET_DEST (PATTERN (insn)));
  335. regs_set[n_regs_set++] = z;
  336. mark_reg_store (z, insn);
  337. }
  338. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  339. {
  340. register rtx y = PATTERN (insn);
  341. for (i = XVECLEN (y, 0) - 1;
  342. i >= 0; i--)
  343. if (GET_CODE (XVECEXP (y, 0, i)) == SET)
  344. {
  345. rtx z = SET_DEST (XVECEXP (y, 0, i));
  346. if (GET_CODE (z) == REG)
  347. {
  348. regs_set[n_regs_set++] = z;
  349. mark_reg_store (z, insn);
  350. }
  351. if (GET_CODE (z) == SUBREG)
  352. {
  353. regs_set[n_regs_set++] = SUBREG_REG (z);
  354. mark_reg_store (SUBREG_REG (z), insn);
  355. }
  356. }
  357. }
  358. /* Mark any registers both set and dead after INSN as dead.
  359. This is not redundant!
  360. A register may be set and killed in the same insn.
  361. It is necessary to mark them as live, above, to get
  362. the right conflicts within the insn. */
  363. while (n_regs_set > 0)
  364. if (reg_dead_p (REGNO (regs_set[--n_regs_set]), insn))
  365. mark_reg_death (regs_set[n_regs_set], 0);
  366. /* Likewise for regs set by incrementation. */
  367. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  368. if ((enum reg_note) GET_MODE (link) == REG_INC
  369. && reg_dead_p (REGNO (XEXP (link, 0)), insn))
  370. mark_reg_death (XEXP (link, 0), 0);
  371. }
  372. if (insn == basic_block_end[b])
  373. break;
  374. insn = NEXT_INSN (insn);
  375. }
  376. }
  377. }
  378. /* Assign a hard register to ALLOCNO; look for one that is the beginning
  379. of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
  380. If we find one, record it in reg_renumber.
  381. If not, do nothing. */
  382. static void
  383. find_reg (allocno, losers, all_regs_p)
  384. int allocno;
  385. register short *losers;
  386. int all_regs_p;
  387. {
  388. register int i;
  389. register HARD_REG_SET used;
  390. enum reg_class class
  391. = all_regs_p ? GENERAL_REGS : reg_preferred_class (allocno_reg[allocno]);
  392. enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
  393. COPY_HARD_REG_SET (used,
  394. (reg_crosses_call[allocno_reg[allocno]]
  395. ? call_clobbered_reg_set : fixed_reg_set));
  396. IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  397. IOR_HARD_REG_SET (used, hard_reg_conflicts[allocno]);
  398. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  399. if (! TEST_HARD_REG_BIT (used, i)
  400. && (losers == 0 || losers[i] < 0)
  401. && HARD_REGNO_MODE_OK (i, mode))
  402. {
  403. register int j;
  404. register int lim = i + HARD_REGNO_NREGS (i, mode);
  405. for (j = i + 1;
  406. (j < lim
  407. && ! TEST_HARD_REG_BIT (used, j)
  408. && (losers == 0 || losers[j] < 0));
  409. j++);
  410. if (j == lim)
  411. break;
  412. i = j; /* Skip starting points we know will lose */
  413. }
  414. /* Did we find a register? */
  415. if (i < FIRST_PSEUDO_REGISTER)
  416. {
  417. register int lim, j;
  418. HARD_REG_SET this_reg;
  419. /* Yes. Record it as the hard register of this pseudo-reg. */
  420. reg_renumber[allocno_reg[allocno]] = i;
  421. /* For each other pseudo-reg conflicting with this one,
  422. mark it as conflicting with the hard regs this one occupies. */
  423. CLEAR_HARD_REG_SET (this_reg);
  424. lim = i + HARD_REGNO_NREGS (i, mode);
  425. for (j = i; j < lim; j++)
  426. SET_HARD_REG_BIT (this_reg, j);
  427. lim = allocno;
  428. for (j = 0; j < max_allocno; j++)
  429. if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
  430. {
  431. IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
  432. }
  433. }
  434. }
  435. /* Called from `reload' when pseudo reg I is being spilled from its
  436. previously assigned hard reg (OLD)
  437. in order to use that hard reg for reloads.
  438. Attempt to find another hard reg to allocate this pseudo in. */
  439. retry_global_alloc (regno, old, spill_reg_order)
  440. int regno, old;
  441. short *spill_reg_order;
  442. {
  443. int allocno = reg_allocno[regno];
  444. if (allocno >= 0)
  445. {
  446. /* Mark this pseudo as conflicting with the
  447. hard reg it is being spilled from. */
  448. SET_HARD_REG_BIT (hard_reg_conflicts[allocno], old);
  449. /* Try to find another hard register for it. */
  450. /* If we have more than one register class,
  451. first try allocating in the class that is cheapest
  452. for this pseudo-reg. If that fails, try any reg. */
  453. if (N_REG_CLASSES > 1)
  454. find_reg (allocno, spill_reg_order, 0);
  455. if (reg_renumber[regno] < 0)
  456. find_reg (allocno, spill_reg_order, 1);
  457. }
  458. }
  459. /* Record a conflict between register REGNO
  460. and everything currently live.
  461. REGNO must not be a pseudo reg that was allocated
  462. by local_alloc; such numbers must be translated through
  463. reg_renumber before calling here. */
  464. static void
  465. record_one_conflict (regno)
  466. int regno;
  467. {
  468. register int j;
  469. if (regno < FIRST_PSEUDO_REGISTER)
  470. /* When a hard register becomes live,
  471. record conflicts with live pseudo regs. */
  472. for (j = 0; j < max_allocno; j++)
  473. {
  474. if (ALLOCNO_LIVE_P (j))
  475. SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
  476. }
  477. else
  478. /* When a pseudo-register becomes live,
  479. record conflicts first with hard regs,
  480. then with other pseudo regs. */
  481. {
  482. register int ialloc = reg_allocno[regno];
  483. register int ialloc_prod = ialloc * allocno_row_words;
  484. IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
  485. for (j = allocno_row_words - 1; j >= 0; j--)
  486. conflicts[ialloc_prod + j] |= allocnos_live[j];
  487. }
  488. }
  489. /* Record all allocnos currently live as conflicting
  490. with each other and with all hard regs currently live.
  491. ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
  492. are currently live. Their bits are also flagged in allocnos_live. */
  493. static void
  494. record_conflicts (allocno_vec, len)
  495. register short *allocno_vec;
  496. register int len;
  497. {
  498. register int allocno;
  499. register int j;
  500. register int ialloc_prod;
  501. while (--len >= 0)
  502. {
  503. allocno = allocno_vec[len];
  504. ialloc_prod = allocno * allocno_row_words;
  505. IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
  506. for (j = allocno_row_words - 1; j >= 0; j--)
  507. conflicts[ialloc_prod + j] |= allocnos_live[j];
  508. }
  509. }
  510. /* Return nonzero if register number REGNO
  511. is one of the "dead registers" of INSN.
  512. If this is true for one of the regs set by INSN,
  513. it means that the reg should not be marked as live
  514. following INSN. (But it still conflicts with everything
  515. else that is live after INSN). */
  516. static int
  517. reg_dead_p (regno, insn)
  518. int regno;
  519. rtx insn;
  520. {
  521. register rtx link;
  522. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  523. if (XEXP (link, 0)
  524. && (enum reg_note) GET_MODE (link) == REG_DEAD
  525. && regno == REGNO (XEXP (link, 0)))
  526. return 1;
  527. return 0;
  528. }
  529. /* Handle the case where REG is set by INSN, during the forward
  530. scan to accumulate conflicts.
  531. Store a 1 in regs_live or allocnos_live for this register, record how many
  532. consecutive hardware registers it actually needs,
  533. and record a conflict with all other registers already live.
  534. Note that even if REG does not remain alive after this insn,
  535. we must mark it here as live, to ensure a conflict between
  536. REG and any other regs set in this insn that really do live.
  537. This is because those other regs could be considered after this. */
  538. static void
  539. mark_reg_store (reg)
  540. rtx reg;
  541. {
  542. register int regno = REGNO (reg);
  543. if (reg_renumber[regno] >= 0)
  544. regno = reg_renumber[regno];
  545. /* Either this is one of the max_allocno pseudo regs not allocated,
  546. or it is or has a hardware reg. First handle the pseudo-regs. */
  547. if (regno >= FIRST_PSEUDO_REGISTER)
  548. {
  549. if (reg_allocno[regno] >= 0)
  550. {
  551. SET_ALLOCNO_LIVE (reg_allocno[regno]);
  552. record_one_conflict (regno);
  553. }
  554. }
  555. /* Handle hardware regs (and pseudos allocated to hard regs). */
  556. else if (! fixed_regs[regno])
  557. {
  558. register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  559. while (regno < last)
  560. {
  561. record_one_conflict (regno);
  562. SET_HARD_REG_BIT (hard_regs_live, regno);
  563. regno++;
  564. }
  565. }
  566. }
  567. /* Mark REG as being dead (following the insn being scanned now).
  568. Store a 0 in regs_live or allocnos_live for this register. */
  569. static void
  570. mark_reg_death (reg)
  571. rtx reg;
  572. {
  573. register int regno = REGNO (reg);
  574. /* For pseudo reg, see if it has been assigned a hardware reg. */
  575. if (reg_renumber[regno] >= 0)
  576. regno = reg_renumber[regno];
  577. /* Either this is one of the max_allocno pseudo regs not allocated,
  578. or it is a hardware reg. First handle the pseudo-regs. */
  579. if (regno >= FIRST_PSEUDO_REGISTER)
  580. {
  581. if (reg_allocno[regno] >= 0)
  582. CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
  583. }
  584. /* Handle hardware regs (and pseudos allocated to hard regs). */
  585. else if (! fixed_regs[regno])
  586. {
  587. /* Pseudo regs already assigned hardware regs are treated
  588. almost the same as explicit hardware regs. */
  589. register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  590. while (regno < last)
  591. {
  592. CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  593. regno++;
  594. }
  595. }
  596. }
  597. /* Mark hard reg REGNO as currently live, assuming machine mode MODE
  598. for the value stored in it. MODE determines how many consecutive
  599. registers are actually in use. Do not record conflicts;
  600. it is assumed that the caller will do that. */
  601. static void
  602. mark_reg_live_nc (regno, mode)
  603. register int regno;
  604. enum machine_mode mode;
  605. {
  606. register int last = regno + HARD_REGNO_NREGS (regno, mode);
  607. while (regno < last)
  608. {
  609. SET_HARD_REG_BIT (hard_regs_live, regno);
  610. regno++;
  611. }
  612. }
  613. /* Print debugging trace information if -greg switch is given,
  614. showing the information on which the allocation decisions are based. */
  615. static void
  616. dump_conflicts (file)
  617. FILE *file;
  618. {
  619. register int i;
  620. fprintf (file, ";; %d regs to allocate:", max_allocno);
  621. for (i = 0; i < max_allocno; i++)
  622. {
  623. fprintf (file, " %d", allocno_reg[allocno_order[i]]);
  624. if (allocno_size[allocno_order[i]] != 1)
  625. fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
  626. }
  627. fprintf (file, "\n");
  628. for (i = 0; i < max_allocno; i++)
  629. {
  630. register int j;
  631. fprintf (file, ";; %d conflicts:", allocno_reg[i],
  632. reg_renumber[allocno_reg[i]]);
  633. for (j = 0; j < max_allocno; j++)
  634. if (CONFLICTP (i, j) || CONFLICTP (j, i))
  635. fprintf (file, " %d", allocno_reg[j]);
  636. for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  637. if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
  638. fprintf (file, " %d", j);
  639. fprintf (file, "\n");
  640. }
  641. fprintf (file, "\n");
  642. }
  643. void
  644. dump_global_regs (file)
  645. FILE *file;
  646. {
  647. register int i;
  648. fprintf (file, ";; Register dispositions:");
  649. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  650. {
  651. if (reg_renumber[i] >= 0)
  652. fprintf (file, " %d in %d ", i, reg_renumber[i]);
  653. }
  654. fprintf (file, "\n\n;; Hard regs used: ");
  655. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  656. if (regs_ever_live[i])
  657. fprintf (file, " %d", i);
  658. fprintf (file, "\n\n");
  659. }