local-alloc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* Allocate registers within a basic block, for GNU compiler.
  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. /* Allocation of hard register numbers to pseudo registers is done in
  18. two passes. In this pass we consider only regs that are born and
  19. die once within one basic block. We do this one basic block at a
  20. time. Then the next pass allocates the registers that remain.
  21. Two passes are used because this pass uses methods that work only
  22. on linear code, but that do a better job than the general methods
  23. used in global_alloc, and more quickly too.
  24. The assignments made are recorded in the vector reg_renumber
  25. whose space is allocated here. The rtl code itself is not altered.
  26. We assign each instruction in the basic block a number
  27. which is its order from the beginning of the block.
  28. Then we can represent the lifetime of a pseudo register with
  29. a pair of numbers, and check for conflicts easily.
  30. We can record the availability of hard registers with a
  31. HARD_REG_SET for each instruction. The HARD_REG_SET
  32. contains 0 or 1 for each hard reg.
  33. To avoid register shuffling, we tie registers together when one
  34. dies by being copied into another, or dies in an instruction that
  35. does arithmetic to produce another. The tied registers are
  36. allocated as one. Registers with different reg class preferences
  37. can never be tied unless the class preferred by one is a subclass
  38. of the one preferred by the other.
  39. Tying is represented with "quantity numbers".
  40. A non-tied register is given a new quantity number.
  41. Tied registers have the same quantity number.
  42. We have provision to exempt registers, even when they are contained
  43. within the block, that can be tied to others that are not contained in it.
  44. This is so that global_alloc could process them both and tie them then.
  45. But this is currently disabled since tying in global_alloc is not
  46. yet implemented. */
  47. #include <stdio.h>
  48. #include "config.h"
  49. #include "rtl.h"
  50. #include "basic-block.h"
  51. #include "regs.h"
  52. #include "hard-reg-set.h"
  53. /* What about hardware registers used and set within same insn?
  54. Will that ever happen for a non-fixed register?
  55. Our lifetime-tracking for hardware registers would lose.
  56. [This caution is an old comment that may be obsolete;
  57. I think there is no longer a problem, but I'm not sure.] */
  58. /* Some constants defined by config.h. */
  59. static char fixed_regs[] = FIXED_REGISTERS;
  60. static char call_clobbered_regs[] = CALL_USED_REGISTERS;
  61. static HARD_REG_SET reg_class_contents[] = REG_CLASS_CONTENTS;
  62. /* HARD_REG_SETs containing the same information found in
  63. FIXED_REGISTERS and CALL_USED_REGISTERS. */
  64. static HARD_REG_SET fixed_reg_set, call_clobbered_reg_set;
  65. /* Next quantity number available for allocation. */
  66. static int next_qty;
  67. /* Element Q is the hard reg number chosen for quantity Q,
  68. or -1 if none was found. */
  69. static int *qty_phys_reg;
  70. /* Insn number (counting from head of basic block)
  71. where quantity Q was born. */
  72. static int *qty_birth;
  73. /* Insn number (counting from head of basic block)
  74. where quantity Q died. Due to the way tying is done,
  75. and the fact that we consider in this pass only regs that die but once,
  76. a quantity can die only once. Each quantity's life span
  77. is a set of consecutive insns. */
  78. static int *qty_death;
  79. /* Number of words needed to hold the data in quantity Q.
  80. This depends on its machine mode. It is used for these purposes:
  81. 1. If it is 0, the qty is not really in use and is not allocated.
  82. 2. It is used in computing the relative importances of qtys,
  83. which determines the order in which we look for regs for them.
  84. 3. It is used in rules that prevent tying several registers of
  85. different sizes in a way that is geometrically impossible
  86. (see combine_regs). */
  87. static int *qty_size;
  88. /* This holds the mode of the registers that are tied to qty Q,
  89. or VOIDmode if registers with differing modes are tied together. */
  90. static enum machine_mode *qty_mode;
  91. /* Nonzero if any of the regs tied to qty Q lives across a CALL_INSN. */
  92. static char *qty_crosses_call;
  93. /* Preferred reg class of qty Q. */
  94. static enum reg_class *qty_reg_class;
  95. /* reg_qty[n] is the qty number of (REG n),
  96. or -1 if (REG n) is not local to the current basic block,
  97. or -2 if not known yet. */
  98. static int *reg_qty;
  99. /* The offset (in words) of register N within its quantity.
  100. This can be nonzero if register N is SImode, and has been tied
  101. to a subreg of a DImode register. */
  102. static int *reg_offset;
  103. /* Vector of substitutions of register numbers,
  104. used to map pseudo regs into hardware regs.
  105. This is set up as a result of register allocation.
  106. Element N is the hard reg assigned to pseudo reg N,
  107. or is -1 if no hard reg was assigned.
  108. If N is a hard reg number, element N is N. */
  109. short *reg_renumber;
  110. /* Set of hard registers live at the current point in the scan
  111. of the instructions in a basic block. */
  112. static HARD_REG_SET regs_live;
  113. /* Indexed by insn-number-within-basic-block,
  114. a set or hard registers live *after* that insn. */
  115. static HARD_REG_SET *regs_live_at;
  116. /* Nonzero if a CALL_INSN has been scanned
  117. but we have not yet seen a reference to the value returned. */
  118. static int call_seen;
  119. static void block_alloc ();
  120. static int combine_regs ();
  121. static void wipe_dead_reg ();
  122. static void reg_is_born ();
  123. static void reg_clobbered ();
  124. static void mark_life ();
  125. static void post_mark_life ();
  126. static int qty_better_p ();
  127. static int qty_better_p_1 ();
  128. /* Allocate a new quantity (new within current basic block)
  129. for register number REGNO which is born in insn number INSN_NUMBER
  130. within the block. MODE and SIZE are info on reg REGNO. */
  131. static void
  132. alloc_qty (regno, mode, size, insn_number)
  133. int regno;
  134. enum machine_mode mode;
  135. int size, insn_number;
  136. {
  137. register int qty = next_qty++;
  138. reg_qty[regno] = qty;
  139. reg_offset[regno] = 0;
  140. qty_size[qty] = size;
  141. qty_mode[qty] = mode;
  142. qty_birth[qty] = insn_number;
  143. qty_crosses_call[qty] = reg_crosses_call[regno];
  144. qty_reg_class[qty] = reg_preferred_class (regno);
  145. }
  146. /* Main entry point of this file. */
  147. void
  148. local_alloc ()
  149. {
  150. register int b, i;
  151. /* Initialize "constant" tables. */
  152. CLEAR_HARD_REG_SET (fixed_reg_set);
  153. CLEAR_HARD_REG_SET (call_clobbered_reg_set);
  154. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  155. {
  156. if (fixed_regs[i])
  157. SET_HARD_REG_BIT (fixed_reg_set, i);
  158. if (call_clobbered_regs[i])
  159. SET_HARD_REG_BIT (call_clobbered_reg_set, i);
  160. }
  161. /* Allocate vectors of temporary data.
  162. See the declarations of these variables, above,
  163. for what they mean. */
  164. qty_phys_reg = (int *) alloca (max_regno * sizeof (int));
  165. qty_birth = (int *) alloca (max_regno * sizeof (int));
  166. qty_death = (int *) alloca (max_regno * sizeof (int));
  167. qty_size = (int *) alloca (max_regno * sizeof (int));
  168. qty_mode = (enum machine_mode *) alloca (max_regno * sizeof (enum machine_mode));
  169. qty_crosses_call = (char *) alloca (max_regno);
  170. qty_reg_class = (enum reg_class *) alloca (max_regno * sizeof (enum reg_class));
  171. reg_qty = (int *) alloca (max_regno * sizeof (int));
  172. reg_offset = (int *) alloca (max_regno * sizeof (int));
  173. reg_renumber = (short *) oballoc (max_regno * sizeof (short));
  174. for (i = 0; i < max_regno; i++)
  175. reg_renumber[i] = -1;
  176. /* Allocate each block's local registers, block by block. */
  177. for (b = 0; b < n_basic_blocks; b++)
  178. {
  179. for (i = 0; i < max_regno; i++)
  180. reg_qty[i] = -2;
  181. bzero (reg_offset, max_regno * sizeof (int));
  182. bzero (qty_birth, max_regno * sizeof (int));
  183. bzero (qty_death, max_regno * sizeof (int));
  184. bzero (qty_size, max_regno * sizeof (int));
  185. bzero (qty_mode, max_regno * sizeof (enum machine_mode));
  186. bzero (qty_phys_reg, max_regno * sizeof (int));
  187. bzero (qty_crosses_call, max_regno);
  188. bzero (qty_reg_class, max_regno * sizeof (enum reg_class));
  189. next_qty = FIRST_PSEUDO_REGISTER;
  190. block_alloc (b);
  191. }
  192. }
  193. /* Allocate hard regs to the pseudo regs used only within block number B.
  194. Only hard regs that die but once can be handled. */
  195. static void
  196. block_alloc (b)
  197. int b;
  198. {
  199. register int i, q;
  200. register rtx insn;
  201. int insn_number = 0;
  202. int insn_count = 0;
  203. short *qty_order;
  204. call_seen = 0;
  205. /* Count the instructions in the basic block. */
  206. insn = basic_block_end[b];
  207. while (1)
  208. {
  209. insn_count++;
  210. if (insn == basic_block_head[b])
  211. break;
  212. insn = PREV_INSN (insn);
  213. }
  214. regs_live_at = (HARD_REG_SET *) alloca (insn_count * sizeof (HARD_REG_SET));
  215. bzero (regs_live_at, insn_count * sizeof (HARD_REG_SET));
  216. /* Initialize table of hardware registers currently live. */
  217. #ifdef HARD_REG_SET
  218. regs_live = *basic_block_live_at_start[b];
  219. #else
  220. COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
  221. #endif
  222. /* This loop scans the instructions of the basic block
  223. and assigns quantities to registers.
  224. It computes which registers to tie. */
  225. insn = basic_block_head[b];
  226. while (1)
  227. {
  228. register rtx body = PATTERN (insn);
  229. insn_number++;
  230. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  231. || GET_CODE (insn) == CALL_INSN)
  232. {
  233. register rtx link;
  234. register int win = 0;
  235. register rtx r0, r1;
  236. int combined_regno = -1;
  237. /* Is this insn suitable for tying two registers?
  238. If so, try doing that.
  239. Suitable insns are (set reg0 reg1) and
  240. (set reg0 (arithop reg1 ...)).
  241. Subregs in place of regs are also ok.
  242. An insn with parallel sets is ok if the first set is suitable.
  243. If tying is done, WIN is set nonzero. */
  244. if (GET_CODE (body) == SET
  245. && (r0 = SET_DEST (body),
  246. GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  247. && (r1 = SET_SRC (body),
  248. GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  249. win = combine_regs (r1, r0, b, insn_number, insn);
  250. else if (GET_CODE (body) == SET
  251. && (r0 = SET_DEST (body),
  252. GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  253. && GET_RTX_FORMAT (GET_CODE (SET_SRC (body)))[0] == 'e'
  254. && (r1 = XEXP (SET_SRC (body), 0),
  255. GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  256. win = combine_regs (r1, r0, b, insn_number, insn);
  257. else if (GET_CODE (body) == PARALLEL)
  258. {
  259. rtx set1 = XVECEXP (body, 0, 0);
  260. if ((r0 = SET_DEST (set1),
  261. GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  262. && GET_RTX_FORMAT (GET_CODE (SET_SRC (set1)))[0] == 'e'
  263. && (r1 = XEXP (SET_SRC (set1), 0),
  264. GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  265. win = combine_regs (r1, r0, b, insn_number, insn);
  266. }
  267. /* If registers were just tied, set COMBINED_REGNO
  268. to the number of the register being set here that was tied.
  269. It should not be assigned a new quantity in the normal
  270. way for registers that are set. */
  271. if (win)
  272. {
  273. while (GET_CODE (r1) == SUBREG)
  274. r1 = SUBREG_REG (r1);
  275. combined_regno = REGNO (r1);
  276. }
  277. /* Mark the death of everything that dies in this instruction,
  278. except for anything that was just combined.
  279. They can be found on the REG_NOTES list of the instruction. */
  280. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  281. if (XEXP (link, 0)
  282. && (enum reg_note) GET_MODE (link) == REG_DEAD
  283. && combined_regno != REGNO (XEXP (link, 0)))
  284. {
  285. if (combined_regno >= 0 &&
  286. reg_qty[combined_regno] == reg_qty[REGNO (XEXP (link, 0))])
  287. /* Here for the death of the quotient in a divmod insn:
  288. something that was born and dead in this insn
  289. but combined with something else that also dies here.
  290. Mark the qty as dying one instruction later. */
  291. wipe_dead_reg (XEXP (link, 0), insn_number,
  292. insn_number + 1, b);
  293. else
  294. wipe_dead_reg (XEXP (link, 0), insn_number, insn_number, b);
  295. }
  296. else if ((enum reg_note) GET_MODE (link) == REG_CONST)
  297. {
  298. /* Also, if this insn introduces a "constant" register,
  299. that could just be replaced by the value it is given here,
  300. tell global-alloc not to allocate it
  301. unless it is used at least twice more. */
  302. i = REGNO (XEXP (link, 0));
  303. if (reg_n_refs[i] <= 2)
  304. {
  305. reg_live_length[i] = -1;
  306. /* If value is not constant, we have a parameter
  307. or a static chain pointer. Tell local-alloc
  308. as well not to allocate it. */
  309. if (! CONSTANT_ADDRESS_P (SET_SRC (PATTERN (insn))))
  310. reg_basic_block[i] = -2;
  311. }
  312. else
  313. /* In any case, lower its priority for global-alloc. */
  314. reg_live_length[i] *= 2;
  315. }
  316. /* Allocate qty numbers for all registers local to this block
  317. that are born (set) in this instruction.
  318. A pseudo that already has a qty is not changed. */
  319. if (GET_CODE (PATTERN (insn)) == SET
  320. && (GET_CODE (SET_DEST (PATTERN (insn))) == REG
  321. || GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG))
  322. reg_is_born (SET_DEST (PATTERN (insn)), insn_number, b);
  323. else if (GET_CODE (PATTERN (insn)) == CLOBBER)
  324. reg_clobbered (XEXP (PATTERN (insn), 0), insn_number);
  325. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  326. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  327. {
  328. if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
  329. && (GET_CODE (SET_DEST (XVECEXP (PATTERN (insn), 0, i))) == REG
  330. || GET_CODE (SET_DEST (XVECEXP (PATTERN (insn), 0, i))) == SUBREG))
  331. reg_is_born (SET_DEST (XVECEXP (PATTERN (insn), 0, i)),
  332. insn_number, b);
  333. else if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
  334. reg_clobbered (XEXP (XVECEXP (PATTERN (insn), 0, i), 0),
  335. insn_number);
  336. }
  337. }
  338. if (GET_CODE (insn) == CALL_INSN)
  339. call_seen = 1;
  340. if (insn == basic_block_end[b])
  341. break;
  342. /* We don't need this for the block's first instruction
  343. since no regs we care about are live before that instruction.
  344. Also we do not allocate space in regs_live_at for that instruction. */
  345. IOR_HARD_REG_SET (regs_live_at[insn_number], regs_live);
  346. insn = NEXT_INSN (insn);
  347. }
  348. /* Now every register that is local to this basic block
  349. has been given a hardware register (its reg_qty is < FIRST_PSEUDO_REGISTER)
  350. or is tied to something not local to this block (reg_qty is -1)
  351. or belongs to a qty with a known birth. (Verify this now.)
  352. If a qty's death has not been established, it indicates a dead store.
  353. That is ok if the insn is not entirely dead.
  354. So set the qty'd death to just after its birth. */
  355. for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  356. {
  357. if (qty_birth[i] == 0)
  358. abort ();
  359. if (qty_death[i] == 0)
  360. qty_death[i] = qty_birth[i] + 1;
  361. }
  362. /* Now order the qtys so we assign them registers
  363. in order of decreasing length of life. */
  364. qty_order = (short *) alloca (next_qty * sizeof (short));
  365. for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  366. qty_order[i] = i;
  367. #define EXCHANGE(I1, I2) \
  368. { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
  369. if (next_qty == 2 + FIRST_PSEUDO_REGISTER)
  370. {
  371. if (qty_better_p (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER))
  372. EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  373. }
  374. else if (next_qty == 3 + FIRST_PSEUDO_REGISTER)
  375. {
  376. if (qty_better_p (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER))
  377. EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  378. if (qty_better_p (FIRST_PSEUDO_REGISTER + 2, FIRST_PSEUDO_REGISTER + 1))
  379. EXCHANGE (FIRST_PSEUDO_REGISTER + 2, FIRST_PSEUDO_REGISTER + 1);
  380. if (qty_better_p (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER))
  381. EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  382. }
  383. else if (next_qty > 3 + FIRST_PSEUDO_REGISTER)
  384. qsort (qty_order + FIRST_PSEUDO_REGISTER,
  385. next_qty - FIRST_PSEUDO_REGISTER, sizeof (short), qty_better_p_1);
  386. /* Now for each qty that is not a hardware register,
  387. look for a hardware register to put it in.
  388. First try the register class that is cheapest for this qty,
  389. if there is more than one class. */
  390. for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  391. {
  392. q = qty_order[i];
  393. if (qty_size[q] >= 0)
  394. {
  395. if (N_REG_CLASSES > 1)
  396. {
  397. qty_phys_reg[q] = find_free_reg (qty_crosses_call[q],
  398. qty_reg_class[q],
  399. qty_mode[q], q,
  400. qty_birth[q], qty_death[q]);
  401. if (qty_phys_reg[q] >= 0)
  402. continue;
  403. }
  404. qty_phys_reg[q] = find_free_reg (qty_crosses_call[q], GENERAL_REGS,
  405. qty_mode[q], q,
  406. qty_birth[q], qty_death[q]);
  407. }
  408. }
  409. /* Now propagate the register assignments
  410. to the pseudo regs belonging to the qtys. */
  411. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  412. if (reg_qty[i] >= 0 && qty_phys_reg[reg_qty[i]] >= 0)
  413. {
  414. reg_renumber[i] = qty_phys_reg[reg_qty[i]] + reg_offset[i];
  415. }
  416. }
  417. /* Compare two quantities' priority for getting real registers.
  418. We give longer-lived quantities higher priority
  419. so that the shorter-lived ones will tend to be in the same places
  420. which gives in general the maximum room for the regs to
  421. be allocated by global-alloc. */
  422. static int
  423. qty_better_p (q1, q2)
  424. int q1, q2;
  425. {
  426. return ((qty_death[q1] - qty_birth[q1]) * qty_size[q2]
  427. > (qty_death[q2] - qty_birth[q2]) * qty_size[q1]);
  428. }
  429. static int
  430. qty_better_p_1 (q1, q2)
  431. short *q1, *q2;
  432. {
  433. return ((qty_death[*q1] - qty_birth[*q1]) * qty_size[*q2]
  434. > (qty_death[*q2] - qty_birth[*q2]) * qty_size[*q1]);
  435. }
  436. /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
  437. Returns 1 if have done so, or 0 if cannot.
  438. Combining registers means marking them as having the same quantity
  439. and adjusting the offsets within the quantity if either of
  440. them is a SUBREG).
  441. There are elaborate checks for the validity of combining. */
  442. static int
  443. combine_regs (usedreg, setreg, b, insn_number, insn)
  444. rtx usedreg, setreg;
  445. int b;
  446. int insn_number;
  447. rtx insn;
  448. {
  449. register int ureg, sreg;
  450. register int offset = 0;
  451. int usize, ssize;
  452. register int sqty;
  453. while (GET_CODE (usedreg) == SUBREG)
  454. {
  455. offset += SUBREG_WORD (usedreg);
  456. usedreg = SUBREG_REG (usedreg);
  457. }
  458. if (GET_CODE (usedreg) != REG)
  459. return 0;
  460. ureg = REGNO (usedreg);
  461. usize = REG_SIZE (usedreg);
  462. /* REG 0 is assigned implicitly by function calls,
  463. but since that is implicit, reg_is_born will not
  464. have been called for it. Do so now
  465. if this is the first use following a function call. */
  466. if (ureg == FUNCTION_VALUE_REGNUM
  467. && call_seen)
  468. {
  469. reg_is_born (usedreg, insn_number, -1);
  470. call_seen = 0;
  471. }
  472. while (GET_CODE (setreg) == SUBREG)
  473. {
  474. offset -= SUBREG_WORD (setreg);
  475. setreg = SUBREG_REG (setreg);
  476. }
  477. if (GET_CODE (setreg) != REG)
  478. return 0;
  479. sreg = REGNO (setreg);
  480. ssize = REG_SIZE (setreg);
  481. /* Do not combine registers unless one fits within the other. */
  482. if (offset > 0 && usize + offset > ssize)
  483. return 0;
  484. if (offset < 0 && usize + offset < ssize)
  485. return 0;
  486. /* Do not combine with a smaller already-assigned object
  487. if that smaller object is already combined with something bigger
  488. or if that smaller object is a hard reg.
  489. In the latter case, we would implicitly be using consecutive
  490. hard regs, and there is no code to keep track of that.
  491. (This is overcautious; we could check that ssize actually
  492. requires more hard regs at this spot.) */
  493. if (ssize > usize && reg_qty[ureg] >= 0
  494. && (usize < qty_size[reg_qty[ureg]]
  495. || reg_qty[ureg] < FIRST_PSEUDO_REGISTER))
  496. return 0;
  497. /* Don't do anything with the non-allocatable registers.
  498. Also, don't tie a call-clobberable register
  499. to something that must live across calls.
  500. Also, don't tie a hardware register to anything larger than it. */
  501. if (ureg < FIRST_PSEUDO_REGISTER)
  502. {
  503. if (fixed_regs[ureg])
  504. return 0;
  505. if (reg_crosses_call[sreg] && call_clobbered_regs[ureg])
  506. return 0;
  507. if (usize < ssize)
  508. return 0;
  509. }
  510. /* Don't tie something that crosses calles
  511. to something tied to a call-clobbered hardware register. */
  512. if (reg_qty[ureg] < FIRST_PSEUDO_REGISTER && reg_qty[ureg] >= 0
  513. && call_clobbered_regs[reg_qty[ureg]]
  514. && reg_crosses_call[sreg])
  515. return 0;
  516. if (reg_qty[sreg] < FIRST_PSEUDO_REGISTER && reg_qty[sreg] >= 0
  517. && call_clobbered_regs[reg_qty[sreg]]
  518. && reg_crosses_call[ureg])
  519. return 0;
  520. if (sreg < FIRST_PSEUDO_REGISTER)
  521. {
  522. if (fixed_regs[sreg])
  523. return 0;
  524. if (reg_crosses_call[ureg] && call_clobbered_regs[sreg])
  525. return 0;
  526. if (ssize < usize)
  527. return 0;
  528. }
  529. /* Tying something to itself is ok iff no offset involved. */
  530. if (ureg == sreg)
  531. return offset == 0;
  532. /* Don't try to connect two different hardware registers. */
  533. if (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
  534. return 0;
  535. /* Don't connect two different machine modes if they have different
  536. implications as to which registers may be used. */
  537. if (!MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
  538. return 0;
  539. /* Do nothing if SREG is a pseudo that already has a quantity.
  540. Also do nothing if it's a hard register that already has one,
  541. since that means it has been used already in this basic block
  542. and something else still live may already be tied to it. */
  543. if (reg_qty[sreg] != -2)
  544. return 0;
  545. /* Summarize the status of what we know about SREG in SQTY:
  546. >= 0 for a hard reg, -2 for a pseudo local to the basic block,
  547. -1 for a pseudo not local to the basic block.
  548. Note that reg_n_deaths[sreg]==0 for a dead store. */
  549. sqty = -2;
  550. if (sreg < FIRST_PSEUDO_REGISTER)
  551. sqty = sreg;
  552. else if (reg_basic_block[sreg] != b || reg_n_deaths[sreg] > 1)
  553. sqty = -1;
  554. /* For now, since global_alloc has no idea of tying,
  555. there is no use noting those local pseudos that could
  556. profitably be delayed till global_alloc and get tied to global ones.
  557. So right now give up if either SREG or UREG is a pseudo
  558. not local to the block. */
  559. if (reg_qty[ureg] == -1 || sqty == -1)
  560. return 0;
  561. /* If SREG is not local to the basic block, or if it is a hard reg,
  562. then tie UREG (and all others it is tied to) to SREG.
  563. Only if UREG is a pseudo-reg local to this basic block
  564. and not already tied to a hardware register,
  565. and SREG is 1) external to the block or 2) a hardware register.
  566. Also if SREG is a hardware register insist that it be in the class
  567. that UREG and its other tied regs want to be in. */
  568. if (sqty != -2 && ureg >= FIRST_PSEUDO_REGISTER
  569. && reg_qty[ureg] >= FIRST_PSEUDO_REGISTER
  570. #if 0
  571. /* qty_best_class would require info not currently computed until
  572. after this scan is complete. */
  573. &&
  574. (sqty == -1 ||
  575. TEST_HARD_REG_BIT (reg_class_contents[(int) qty_best_class (reg_qty[ureg])],
  576. sreg))
  577. #else
  578. &&
  579. (sqty == -1 ||
  580. TEST_HARD_REG_BIT (reg_class_contents[(int) reg_preferred_class (ureg)],
  581. sreg))
  582. #endif
  583. )
  584. {
  585. /* We get rid of the quantity that ureg belongs to
  586. and make all regs of that quantity get sqty instead. */
  587. register int i;
  588. register int v = reg_qty[ureg];
  589. if (sqty == -1) offset = 0;
  590. else
  591. {
  592. reg_is_born (setreg, insn_number, b);
  593. post_mark_life (sqty, qty_mode[sqty], 1, qty_birth[v], insn_number);
  594. }
  595. qty_birth[sqty] = qty_birth[v];
  596. qty_death[v] = qty_birth[v]; /* So qty V won't occupy any hard reg */
  597. qty_crosses_call[sqty] |= qty_crosses_call[v];
  598. if (qty_size[v] > qty_size[sqty])
  599. {
  600. qty_size[sqty] = qty_size[v];
  601. qty_mode[sqty] = qty_mode[v];
  602. }
  603. for (i = 0; i < max_regno; i++)
  604. if (reg_qty[i] == v)
  605. {
  606. reg_qty[i] = sqty;
  607. reg_offset[i] -= offset;
  608. }
  609. }
  610. /* Else if we don't already know about SREG, tie it to UREG
  611. if this is the last use of UREG.
  612. If UREG is a hardware register (or tied to one), don't tie
  613. if it is not in the class that SREG wants.
  614. If UREG is not a hardware register, don't tie
  615. if it and SREG want different classes. */
  616. else if (sqty == -2 && regno_dead_p (ureg, insn)
  617. && (reg_qty[ureg] >= FIRST_PSEUDO_REGISTER
  618. ? reg_classes_fit (ureg, sreg)
  619. : TEST_HARD_REG_BIT (reg_class_contents[(int) reg_preferred_class (sreg)],
  620. reg_qty[ureg])))
  621. {
  622. if (reg_qty[ureg] == -2)
  623. reg_is_born (usedreg, insn_number, b);
  624. sqty = reg_qty[sreg] = reg_qty[ureg];
  625. reg_offset[sreg] = reg_offset[ureg] + offset;
  626. if (sqty >= 0)
  627. {
  628. qty_crosses_call[sqty] |= reg_crosses_call[sreg];
  629. if (usize < ssize)
  630. {
  631. register int i;
  632. for (i = 0; i < max_regno; i++)
  633. if (reg_qty[i] == sqty)
  634. reg_offset[i] -= offset;
  635. qty_size[sqty] = ssize;
  636. qty_mode[sqty] = GET_MODE (setreg);
  637. }
  638. }
  639. }
  640. else
  641. return 0;
  642. return 1;
  643. }
  644. /* Return nonzero if R2's preferred class is the same as or contains
  645. R1's preferred class. R1 and R2 are pseudo-register numbers. */
  646. int
  647. reg_classes_fit (r1, r2)
  648. int r1, r2;
  649. {
  650. register enum reg_class c1 = reg_preferred_class (r1);
  651. register enum reg_class c2 = reg_preferred_class (r2);
  652. if (c1 == c2) return 1;
  653. if (c2 == ALL_REGS)
  654. win:
  655. return 1;
  656. GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
  657. reg_class_contents[(int)c2],
  658. win);
  659. return 0;
  660. }
  661. /* Handle the beginning of the life of register REG.
  662. REG can actually be a SUBREG instead of a REG.
  663. Note that combine_regs assumes that BLOCKNUM is irrelevant
  664. for hard registers. */
  665. static void
  666. reg_is_born (reg, insn_number, blocknum)
  667. rtx reg;
  668. int insn_number;
  669. int blocknum;
  670. {
  671. register int regno;
  672. while (GET_CODE (reg) == SUBREG)
  673. reg = SUBREG_REG (reg);
  674. regno = REGNO (reg);
  675. if (regno < FIRST_PSEUDO_REGISTER)
  676. {
  677. reg_qty[regno] = regno;
  678. qty_phys_reg[regno] = regno;
  679. qty_mode[regno] = GET_MODE (reg);
  680. mark_life (regno, GET_MODE (reg), 1);
  681. }
  682. else if (reg_qty[regno] >= -1)
  683. ;
  684. else if (reg_basic_block[regno] == blocknum
  685. && reg_n_deaths[regno] == 1)
  686. alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), insn_number);
  687. else
  688. reg_qty[regno] = -1;
  689. }
  690. /* Handle the clobberage of register REG in insn INSN_NUMBER.
  691. Just mark the register as in use, only just after this instruction. */
  692. static void
  693. reg_clobbered (reg, insn_number)
  694. rtx reg;
  695. register int insn_number;
  696. {
  697. register int regno;
  698. if (reg == 0)
  699. return;
  700. if (GET_CODE (reg) != REG)
  701. return;
  702. regno = REGNO (reg);
  703. if (regno < FIRST_PSEUDO_REGISTER)
  704. {
  705. register int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  706. register int i;
  707. for (i = regno; i < lim; i++)
  708. SET_HARD_REG_BIT (regs_live_at[insn_number], i);
  709. }
  710. }
  711. /* Record the death in insn DEATH_INSN_NUMBER for the register REG. */
  712. static void
  713. wipe_dead_reg (reg, this_insn_number, death_insn_number, blocknum)
  714. register rtx reg;
  715. int this_insn_number;
  716. int death_insn_number;
  717. int blocknum;
  718. {
  719. register int regno = REGNO (reg);
  720. /* If a pseudo reg is referred to but was never set,
  721. we will find here that its qty is -2.
  722. Since these regs do not conflict with anything,
  723. mark them as born and dead in the same place. */
  724. if (reg_qty[regno] == -2
  725. && regno >= FIRST_PSEUDO_REGISTER
  726. && reg_basic_block[regno] == blocknum
  727. && reg_n_deaths[regno] == 1)
  728. alloc_qty (regno, GET_MODE (reg), REG_SIZE (reg), this_insn_number);
  729. if (reg_qty[regno] >= 0)
  730. {
  731. qty_death[reg_qty[regno]] = death_insn_number;
  732. if (reg_qty[regno] < FIRST_PSEUDO_REGISTER)
  733. {
  734. mark_life (reg_qty[regno], GET_MODE (reg), 0);
  735. if (this_insn_number != death_insn_number)
  736. post_mark_life (reg_qty[regno], GET_MODE (reg), 1,
  737. this_insn_number, death_insn_number);
  738. }
  739. }
  740. }
  741. /* Find a block of SIZE words of hard regs in reg_class CLASS
  742. that can hold something of machine-mode MODE
  743. (but actually we test only the first of the block for holding MODE)
  744. and still free between insn BORN_INSN and insn DEAD_INSN,
  745. and return the number of the first of them.
  746. Return -1 if such a block cannot be found.
  747. If CALL_PRESERVED is nonzero, insist on registers preserved
  748. over subroutine calls, and return -1 if cannot find such. */
  749. static int
  750. find_free_reg (call_preserved, class, mode, qty, born_insn, dead_insn)
  751. int call_preserved;
  752. enum reg_class class;
  753. enum machine_mode mode;
  754. int qty;
  755. int born_insn, dead_insn;
  756. {
  757. register int i, ins;
  758. register HARD_REG_SET used;
  759. COPY_HARD_REG_SET (used,
  760. call_preserved ? call_clobbered_reg_set : fixed_reg_set);
  761. for (ins = born_insn; ins < dead_insn; ins++)
  762. IOR_HARD_REG_SET (used, regs_live_at[ins]);
  763. IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  764. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  765. if (! TEST_HARD_REG_BIT (used, i)
  766. && HARD_REGNO_MODE_OK (i, mode))
  767. {
  768. register int j;
  769. register int size1 = HARD_REGNO_NREGS (i, mode);
  770. for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  771. if (j == size1)
  772. {
  773. post_mark_life (i, mode, 1, born_insn, dead_insn);
  774. return i;
  775. }
  776. i += j; /* Skip starting points we know will lose */
  777. }
  778. return -1;
  779. }
  780. static void
  781. mark_life (regno, mode, life)
  782. register int regno;
  783. enum machine_mode mode;
  784. int life;
  785. {
  786. register int j = HARD_REGNO_NREGS (regno, mode);
  787. if (life)
  788. while (--j >= 0)
  789. SET_HARD_REG_BIT (regs_live, regno + j);
  790. else
  791. while (--j >= 0)
  792. CLEAR_HARD_REG_BIT (regs_live, regno + j);
  793. }
  794. static void
  795. post_mark_life (regno, mode, life, birth, death)
  796. register int regno, life, birth;
  797. enum machine_mode mode;
  798. int death;
  799. {
  800. register int j = HARD_REGNO_NREGS (regno, mode);
  801. register HARD_REG_SET this_reg;
  802. CLEAR_HARD_REG_SET (this_reg);
  803. while (--j >= 0)
  804. SET_HARD_REG_BIT (this_reg, regno + j);
  805. if (life)
  806. while (birth < death)
  807. {
  808. IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
  809. birth++;
  810. }
  811. else
  812. while (birth < death)
  813. {
  814. AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
  815. birth++;
  816. }
  817. }
  818. void
  819. dump_local_alloc (file)
  820. FILE *file;
  821. {
  822. register int i;
  823. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  824. if (reg_renumber[i] != -1)
  825. fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
  826. }