local-alloc.c 34 KB

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