flow.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /* Data flow analysis 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. /* This file contains the data flow analysis pass of the compiler.
  18. It computes data flow information
  19. which tells combine_instructions which insns to consider combining
  20. and controls register allocation.
  21. Additional data flow information that is too bulky to record
  22. is generated during the analysis, and is used at that time to
  23. create autoincrement and autodecrement addressing.
  24. The first step is dividing the function into basic blocks.
  25. find_basic_blocks does this. Then life_analysis determines
  26. where each register is live and where it is dead.
  27. ** find_basic_blocks **
  28. find_basic_blocks divides the current function's rtl
  29. into basic blocks. It records the beginnings and ends of the
  30. basic blocks in the vectors basic_block_head and basic_block_end,
  31. and the number of blocks in n_basic_blocks.
  32. find_basic_blocks also finds any unreachable loops
  33. and deletes them.
  34. ** life_analysis **
  35. life_analysis is called immediately after find_basic_blocks.
  36. It uses the basic block information to determine where each
  37. hard or pseudo register is live.
  38. ** live-register info **
  39. The information about where each register is live is in two parts:
  40. the REG_NOTES of insns, and the vector basic_block_live_at_start.
  41. basic_block_live_at_start has an element for each basic block,
  42. and the element is a bit-vector with a bit for each hard or pseudo
  43. register. The bit is 1 if the register is live at the beginning
  44. of the basic block.
  45. To each insn's REG_NOTES is added an element for each register
  46. that is live before the insn or set by the insn, but is dead
  47. after the insn.
  48. To determine which registers are live after any insn, one can
  49. start from the beginning of the basic block and scan insns, noting
  50. which registers are set by each insn and which die there.
  51. ** Other actions of life_analysis **
  52. life_analysis sets up the LOG_LINKS fields of insns because the
  53. information needed to do so is readily available.
  54. life_analysis deletes insns whose only effect is to store a value
  55. that is never used.
  56. life_analysis notices cases where a reference to a register as
  57. a memory address can be combined with a preceding or following
  58. incrementation or decrementation of the register. The separate
  59. instruction to increment or decrement is deleted and the address
  60. is changed to a POST_INC or similar rtx.
  61. Each time an incrementing or decrementing address is created,
  62. a REG_INC element is added to the insn's REG_NOTES list.
  63. life_analysis fills in certain vectors containing information about
  64. register usage: reg_n_refs, reg_n_deaths, reg_n_sets,
  65. reg_live_length, reg_crosses_call and reg_basic_block. */
  66. #include <stdio.h>
  67. #include "config.h"
  68. #include "rtl.h"
  69. #include "basic-block.h"
  70. #include "regs.h"
  71. /* Get the basic block number of an insn.
  72. This info should not be expected to remain available
  73. after the end of life_analysis. */
  74. #define BLOCK_NUM(INSN) uid_block_number[INSN_UID (INSN)]
  75. /* This is where the BLOCK_NUM values are really stored.
  76. This is set up by find_basic_blocks and used there and in life_analysis,
  77. and then freed. */
  78. static short *uid_block_number;
  79. /* Number of basic blocks in the current function. */
  80. int n_basic_blocks;
  81. /* Maximum register number used in this function, plus one. */
  82. int max_regno;
  83. /* Indexed by n, gives number of basic block that (REG n) is used in.
  84. Or gives -2 if (REG n) is used in more than one basic block.
  85. Or -1 if it has not yet been seen so no basic block is known.
  86. This information remains valid for the rest of the compilation
  87. of the current function; it is used to control register allocation. */
  88. short *reg_basic_block;
  89. /* Indexed by n, gives number of times (REG n) is used or set, each
  90. weighted by its loop-depth.
  91. This information remains valid for the rest of the compilation
  92. of the current function; it is used to control register allocation. */
  93. short *reg_n_refs;
  94. /* Indexed by n, gives number of times (REG n) is set.
  95. This information remains valid for the rest of the compilation
  96. of the current function; it is used to control register allocation. */
  97. short *reg_n_sets;
  98. /* Indexed by N, gives number of places register N dies.
  99. This information remains valid for the rest of the compilation
  100. of the current function; it is used to control register allocation. */
  101. short *reg_n_deaths;
  102. /* Indexed by N, gives 1 if that reg is live across any CALL_INSNs.
  103. This information remains valid for the rest of the compilation
  104. of the current function; it is used to control register allocation. */
  105. char *reg_crosses_call;
  106. /* Total number of instructions at which (REG n) is live.
  107. The larger this is, the less priority (REG n) gets for
  108. allocation in a real register.
  109. This information remains valid for the rest of the compilation
  110. of the current function; it is used to control register allocation. */
  111. int *reg_live_length;
  112. /* Element N is the next insn that uses (hard or pseudo) register number N
  113. within the current basic block; or zero, if there is no such insn.
  114. This is valid only during the final backward scan in propagate_block. */
  115. static rtx *reg_next_use;
  116. /* Size of a regset for the current function,
  117. in (1) bytes and (2) elements. */
  118. int regset_bytes;
  119. int regset_size;
  120. /* Element N is first insn in basic block N.
  121. This info lasts until we finish compiling the function. */
  122. rtx *basic_block_head;
  123. /* Element N is last insn in basic block N.
  124. This info lasts until we finish compiling the function. */
  125. rtx *basic_block_end;
  126. /* Element N is a regset describing the registers live
  127. at the start of basic block N.
  128. This info lasts until we finish compiling the function. */
  129. regset *basic_block_live_at_start;
  130. /* Element N is nonzero if control can drop into basic block N
  131. from the preceding basic block. Freed after life_analysis. */
  132. char *basic_block_drops_in;
  133. /* Element N is depth within loops of basic block number N.
  134. Freed after life_analysis. */
  135. short *basic_block_loop_depth;
  136. /* Element N nonzero if basic block N can actually be reached.
  137. Vector exists only during find_basic_blocks. */
  138. char *block_live_static;
  139. /* Depth within loops of basic block being scanned for lifetime analysis,
  140. plus one. This is the weight attached to references to registers. */
  141. int loop_depth;
  142. /* Forward declarations */
  143. static void find_basic_blocks ();
  144. static void life_analysis ();
  145. static void mark_label_ref ();
  146. void allocate_for_life_analysis (); /* Used also in stupid_life_analysis */
  147. static void init_regset_vector ();
  148. static void propagate_block ();
  149. static void mark_set_regs ();
  150. static void mark_used_regs ();
  151. static int insn_dead_p ();
  152. static int try_pre_increment ();
  153. static int try_pre_increment_1 ();
  154. static rtx find_use_as_address ();
  155. /* Find basic blocks of the current function and perform data flow analysis.
  156. F is the first insn of the function and NREGS the number of register numbers
  157. in use. */
  158. void
  159. flow_analysis (f, nregs, file)
  160. rtx f;
  161. int nregs;
  162. FILE *file;
  163. {
  164. register rtx insn;
  165. register int i;
  166. register int max_uid = 0;
  167. /* Count the basic blocks. Also find maximum insn uid value used. */
  168. {
  169. register RTX_CODE prev_code = JUMP_INSN;
  170. register RTX_CODE code;
  171. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  172. {
  173. code = GET_CODE (insn);
  174. if (INSN_UID (insn) > max_uid)
  175. max_uid = INSN_UID (insn);
  176. if (code == CODE_LABEL
  177. || (prev_code != INSN && prev_code != CALL_INSN
  178. && prev_code != CODE_LABEL
  179. && (code == INSN || code == CALL_INSN || code == JUMP_INSN)))
  180. i++;
  181. if (code != NOTE)
  182. prev_code = code;
  183. }
  184. }
  185. /* Allocate some tables that last till end of compiling this function
  186. and some needed only in find_basic_blocks and life_analysis. */
  187. n_basic_blocks = i;
  188. basic_block_head = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
  189. basic_block_end = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
  190. basic_block_drops_in = (char *) alloca (n_basic_blocks);
  191. basic_block_loop_depth = (short *) alloca (n_basic_blocks * sizeof (short));
  192. uid_block_number = (short *) alloca ((max_uid + 1) * sizeof (short));
  193. find_basic_blocks (f);
  194. life_analysis (f, nregs);
  195. if (file)
  196. dump_flow_info (file);
  197. basic_block_drops_in = 0;
  198. uid_block_number = 0;
  199. basic_block_loop_depth = 0;
  200. }
  201. /* Find all basic blocks of the function whose first insn is F.
  202. Store the correct data in the tables that describe the basic blocks,
  203. set up the chains of references for each CODE_LABEL, and
  204. delete any entire basic blocks that cannot be reached. */
  205. static void
  206. find_basic_blocks (f)
  207. rtx f;
  208. {
  209. register rtx insn;
  210. register int i;
  211. /* Initialize the ref chain of each label to 0. */
  212. /* Record where all the blocks start and end and their depth in loops. */
  213. /* For each insn, record the block it is in. */
  214. {
  215. register RTX_CODE prev_code = JUMP_INSN;
  216. register RTX_CODE code;
  217. int depth = 1;
  218. for (insn = f, i = -1; insn; insn = NEXT_INSN (insn))
  219. {
  220. code = GET_CODE (insn);
  221. if (code == NOTE)
  222. {
  223. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  224. depth++;
  225. else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  226. depth--;
  227. }
  228. else if (code == CODE_LABEL
  229. || (prev_code != INSN && prev_code != CALL_INSN
  230. && prev_code != CODE_LABEL
  231. && (code == INSN || code == CALL_INSN || code == JUMP_INSN)))
  232. {
  233. basic_block_head[++i] = insn;
  234. basic_block_end[i] = insn;
  235. basic_block_loop_depth[i] = depth;
  236. if (code == CODE_LABEL)
  237. LABEL_REFS (insn) = insn;
  238. }
  239. else if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  240. basic_block_end[i] = insn;
  241. BLOCK_NUM (insn) = i;
  242. if (code != NOTE)
  243. prev_code = code;
  244. }
  245. }
  246. /* Record which basic blocks control can drop in to. */
  247. {
  248. register int i;
  249. for (i = 0; i < n_basic_blocks; i++)
  250. {
  251. register rtx insn = PREV_INSN (basic_block_head[i]);
  252. while (insn && GET_CODE (insn) == NOTE)
  253. insn = PREV_INSN (insn);
  254. basic_block_drops_in[i]
  255. = insn && GET_CODE (insn) != BARRIER;
  256. }
  257. }
  258. /* Now find which basic blocks can actually be reached
  259. and put all jump insns' LABEL_REFS onto the ref-chains
  260. of their target labels. */
  261. if (n_basic_blocks > 0)
  262. {
  263. register char *block_live = (char *) alloca (n_basic_blocks);
  264. register char *block_marked = (char *) alloca (n_basic_blocks);
  265. int something_marked = 1;
  266. /* Initialize with just block 0 reachable and no blocks marked. */
  267. bzero (block_live, n_basic_blocks);
  268. bzero (block_marked, n_basic_blocks);
  269. block_live[0] = 1;
  270. block_live_static = block_live;
  271. /* Pass over all blocks, marking each block that is reachable
  272. and has not yet been marked.
  273. Keep doing this until, in one pass, no blocks have been marked.
  274. Then blocks_live and blocks_marked are identical and correct.
  275. In addition, all jumps actually reachable have been marked. */
  276. while (something_marked)
  277. {
  278. something_marked = 0;
  279. for (i = 0; i < n_basic_blocks; i++)
  280. if (block_live[i] && !block_marked[i])
  281. {
  282. block_marked[i] = 1;
  283. something_marked = 1;
  284. if (i + 1 < n_basic_blocks && basic_block_drops_in[i + 1])
  285. block_live[i + 1] = 1;
  286. insn = basic_block_end[i];
  287. if (GET_CODE (insn) == JUMP_INSN)
  288. mark_label_ref (PATTERN (insn), insn, 0);
  289. }
  290. }
  291. /* Now delete the code for any basic blocks that can't be reached.
  292. They can occur because jump_optimize does not recognize
  293. unreachable loops as unreachable. */
  294. for (i = 0; i < n_basic_blocks; i++)
  295. if (!block_live[i])
  296. {
  297. insn = basic_block_head[i];
  298. while (1)
  299. {
  300. if (GET_CODE (insn) != NOTE)
  301. {
  302. PUT_CODE (insn, NOTE);
  303. NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  304. NOTE_SOURCE_FILE (insn) = 0;
  305. }
  306. if (insn == basic_block_end[i])
  307. break;
  308. insn = NEXT_INSN (insn);
  309. }
  310. /* Each time we delete some basic blocks,
  311. see if there is a jump around them that is
  312. being turned into a no-op. If so, delete it. */
  313. if (block_live[i - 1])
  314. {
  315. register int j;
  316. for (j = i; j < n_basic_blocks; j++)
  317. if (block_live[j])
  318. {
  319. insn = basic_block_end[i - 1];
  320. if (GET_CODE (insn) == JUMP_INSN
  321. && JUMP_LABEL (insn) != 0
  322. && BLOCK_NUM (JUMP_LABEL (insn)) == j)
  323. {
  324. PUT_CODE (insn, NOTE);
  325. NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  326. NOTE_SOURCE_FILE (insn) = 0;
  327. }
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. /* Check expression X for label references;
  335. if one is found, add INSN to the label's chain of references.
  336. CHECKDUP means check for and avoid creating duplicate references
  337. from the same insn. Such duplicates do no serious harm but
  338. can slow life analysis. CHECKDUP is set only when duplicates
  339. are likely. */
  340. static void
  341. mark_label_ref (x, insn, checkdup)
  342. rtx x, insn;
  343. int checkdup;
  344. {
  345. register RTX_CODE code = GET_CODE (x);
  346. register int i;
  347. register char *fmt;
  348. if (code == LABEL_REF)
  349. {
  350. register rtx label = XEXP (x, 0);
  351. register rtx y;
  352. if (GET_CODE (label) != CODE_LABEL)
  353. return;
  354. CONTAINING_INSN (x) = insn;
  355. /* if CHECKDUP is set, check for duplicate ref from same insn
  356. and don't insert. */
  357. if (checkdup)
  358. for (y = LABEL_REFS (label); y != label; y = LABEL_NEXTREF (y))
  359. if (CONTAINING_INSN (y) == insn)
  360. return;
  361. LABEL_NEXTREF (x) = LABEL_REFS (label);
  362. LABEL_REFS (label) = x;
  363. block_live_static[BLOCK_NUM (label)] = 1;
  364. return;
  365. }
  366. fmt = GET_RTX_FORMAT (code);
  367. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  368. {
  369. if (fmt[i] == 'e')
  370. mark_label_ref (XEXP (x, i), insn, 0);
  371. if (fmt[i] == 'E')
  372. {
  373. register int j;
  374. for (j = 0; j < XVECLEN (x, i); j++)
  375. mark_label_ref (XVECEXP (x, i, j), insn, 1);
  376. }
  377. }
  378. }
  379. /* Determine the which registers are live at the start of each
  380. basic block of the function whose first insn is F.
  381. NREGS is the number of registers used in F.
  382. We allocate the vector basic_block_live_at_start
  383. and the regsets that it points to, and fill them with the data.
  384. regset_size and regset_bytes are also set here. */
  385. static void
  386. life_analysis (f, nregs)
  387. rtx f;
  388. int nregs;
  389. {
  390. register regset tem;
  391. int first_pass;
  392. int changed;
  393. /* For each basic block, a bitmask of regs
  394. live on exit from the block. */
  395. regset *basic_block_live_at_end;
  396. /* For each basic block, a bitmask of regs
  397. live on entry to a successor-block of this block.
  398. If this does not match basic_block_live_at_end,
  399. that must be updated, and the block must be rescanned. */
  400. regset *basic_block_new_live_at_end;
  401. /* For each basic block, a bitmask of regs
  402. whose liveness at the end of the basic block
  403. can make a difference in which regs are live on entry to the block.
  404. These are the regs that are set within the basic block,
  405. possibly excluding those that are used after they are set. */
  406. regset *basic_block_significant;
  407. register int i;
  408. max_regno = nregs;
  409. bzero (regs_ever_live, sizeof regs_ever_live);
  410. /* Allocate and zero out many data structures
  411. that will record the data from lifetime analysis. */
  412. allocate_for_life_analysis ();
  413. reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
  414. bzero (reg_next_use, nregs * sizeof (rtx));
  415. /* Set up several regset-vectors used internally within this function.
  416. Their meanings are documented above, with their declarations. */
  417. basic_block_live_at_end = (regset *) alloca (n_basic_blocks * sizeof (regset));
  418. tem = (regset) alloca (n_basic_blocks * regset_bytes);
  419. bzero (tem, n_basic_blocks * regset_bytes);
  420. init_regset_vector (basic_block_live_at_end, tem, n_basic_blocks, regset_bytes);
  421. basic_block_new_live_at_end = (regset *) alloca (n_basic_blocks * sizeof (regset));
  422. tem = (regset) alloca (n_basic_blocks * regset_bytes);
  423. bzero (tem, n_basic_blocks * regset_bytes);
  424. init_regset_vector (basic_block_new_live_at_end, tem, n_basic_blocks, regset_bytes);
  425. basic_block_significant = (regset *) alloca (n_basic_blocks * sizeof (regset));
  426. tem = (regset) alloca (n_basic_blocks * regset_bytes);
  427. bzero (tem, n_basic_blocks * regset_bytes);
  428. init_regset_vector (basic_block_significant, tem, n_basic_blocks, regset_bytes);
  429. /* Propagate life info through the basic blocks
  430. around the graph of basic blocks.
  431. This is a relaxation process: each time a new register
  432. is live at the end of the basic block, we must scan the block
  433. to determine which registers are, as a consequence, live at the beginning
  434. of that block. These registers must then be marked live at the ends
  435. of all the blocks that can transfer control to that block.
  436. The process continues until it reaches a fixed point. */
  437. first_pass = 1;
  438. changed = 1;
  439. while (changed)
  440. {
  441. changed = 0;
  442. for (i = n_basic_blocks - 1; i >= 0; i--)
  443. {
  444. int consider = first_pass;
  445. int must_rescan = first_pass;
  446. register int j;
  447. /* Set CONSIDER if this block needs thinking about at all
  448. (that is, if the regs live now at the end of it
  449. are not the same as were live at the end of it when
  450. we last thought about it).
  451. Set must_rescan if it needs to be thought about
  452. instruction by instruction (that is, if any additional
  453. reg that is live at the end now but was not live there before
  454. is one of the significant regs of this basic block). */
  455. for (j = 0; j < regset_size; j++)
  456. {
  457. register int x = basic_block_new_live_at_end[i][j]
  458. & ~basic_block_live_at_end[i][j];
  459. if (x)
  460. consider = 1;
  461. if (x & basic_block_significant[i][j])
  462. {
  463. must_rescan = 1;
  464. consider = 1;
  465. break;
  466. }
  467. }
  468. if (! consider)
  469. continue;
  470. /* The live_at_start of this block may be changing,
  471. so another pass will be required after this one. */
  472. changed = 1;
  473. if (! must_rescan)
  474. {
  475. /* No complete rescan needed;
  476. just record those variables newly known live at end
  477. as live at start as well. */
  478. for (j = 0; j < regset_size; j++)
  479. {
  480. register int x = basic_block_new_live_at_end[i][j]
  481. & ~basic_block_live_at_end[i][j];
  482. basic_block_live_at_start[i][j] |= x;
  483. basic_block_live_at_end[i][j] |= x;
  484. }
  485. }
  486. else
  487. {
  488. /* Update the basic_block_live_at_start
  489. by propagation backwards through the block. */
  490. bcopy (basic_block_new_live_at_end[i],
  491. basic_block_live_at_end[i], regset_bytes);
  492. bcopy (basic_block_live_at_end[i],
  493. basic_block_live_at_start[i], regset_bytes);
  494. propagate_block (basic_block_live_at_start[i],
  495. basic_block_head[i], basic_block_end[i], 0,
  496. first_pass ? basic_block_significant[i] : 0,
  497. i);
  498. }
  499. {
  500. register rtx jump, head;
  501. /* Update the basic_block_new_live_at_end's of the block
  502. that falls through into this one (if any). */
  503. head = basic_block_head[i];
  504. jump = PREV_INSN (head);
  505. if (basic_block_drops_in[i])
  506. {
  507. register from_block = BLOCK_NUM (jump);
  508. register int j;
  509. for (j = 0; j < regset_size; j++)
  510. basic_block_new_live_at_end[from_block][j]
  511. |= basic_block_live_at_start[i][j];
  512. }
  513. /* Update the basic_block_new_live_at_end's of
  514. all the blocks that jump to this one. */
  515. if (GET_CODE (head) == CODE_LABEL)
  516. for (jump = LABEL_REFS (head);
  517. jump != head;
  518. jump = LABEL_NEXTREF (jump))
  519. {
  520. register from_block = BLOCK_NUM (CONTAINING_INSN (jump));
  521. register int j;
  522. for (j = 0; j < regset_size; j++)
  523. basic_block_new_live_at_end[from_block][j]
  524. |= basic_block_live_at_start[i][j];
  525. }
  526. }
  527. }
  528. first_pass = 0;
  529. }
  530. /* Now the life information is accurate.
  531. Make one more pass over each basic block
  532. to delete dead stores, create autoincrement addressing
  533. and record how many times each register is used, is set, or dies.
  534. To save time, we operate directly in basic_block_live_at_end[i],
  535. thus destroying it (in fact, converting it into a copy of
  536. basic_block_live_at_start[i]). This is ok now because
  537. basic_block_live_at_end[i] is no longer used past this point. */
  538. for (i = 0; i < n_basic_blocks; i++)
  539. {
  540. propagate_block (basic_block_live_at_end[i],
  541. basic_block_head[i], basic_block_end[i], 1, 0, i);
  542. }
  543. }
  544. /* Subroutines of life analysis. */
  545. /* Allocate the permanent data structures that represent the results
  546. of life analysis. Not static since used also for stupid life analysis. */
  547. void
  548. allocate_for_life_analysis ()
  549. {
  550. register int i;
  551. register regset tem;
  552. regset_size = ((max_regno + REGSET_ELT_BITS - 1) / REGSET_ELT_BITS);
  553. regset_bytes = regset_size * sizeof (*(regset)0);
  554. reg_n_refs = (short *) oballoc (max_regno * sizeof (short));
  555. bzero (reg_n_refs, max_regno * sizeof (short));
  556. reg_n_sets = (short *) oballoc (max_regno * sizeof (short));
  557. bzero (reg_n_sets, max_regno * sizeof (short));
  558. reg_n_deaths = (short *) oballoc (max_regno * sizeof (short));
  559. bzero (reg_n_deaths, max_regno * sizeof (short));
  560. reg_live_length = (int *) oballoc (max_regno * sizeof (int));
  561. bzero (reg_live_length, max_regno * sizeof (int));
  562. reg_crosses_call = (char *) oballoc (max_regno);
  563. bzero (reg_crosses_call, max_regno);
  564. reg_basic_block = (short *) oballoc (max_regno * sizeof (short));
  565. for (i = 0; i < max_regno; i++)
  566. reg_basic_block[i] = -1;
  567. basic_block_live_at_start = (regset *) oballoc (n_basic_blocks * sizeof (regset));
  568. tem = (regset) oballoc (n_basic_blocks * regset_bytes);
  569. bzero (tem, n_basic_blocks * regset_bytes);
  570. init_regset_vector (basic_block_live_at_start, tem, n_basic_blocks, regset_bytes);
  571. }
  572. /* Make each element of VECTOR point at a regset,
  573. taking the space for all those regsets from SPACE.
  574. SPACE is of type regset, but it is really as long as NELTS regsets.
  575. BYTES_PER_ELT is the number of bytes in one regset. */
  576. static void
  577. init_regset_vector (vector, space, nelts, bytes_per_elt)
  578. regset *vector;
  579. regset space;
  580. int nelts;
  581. int bytes_per_elt;
  582. {
  583. register int i;
  584. register regset p = space;
  585. for (i = 0; i < nelts; i++)
  586. {
  587. vector[i] = p;
  588. p += bytes_per_elt / sizeof (*p);
  589. }
  590. }
  591. /* Compute the registers live at the beginning of a basic block
  592. from those live at the end.
  593. When called, OLD contains those live at the end.
  594. On return, it contains those live at the beginning.
  595. FIRST and LAST are the first and last insns of the basic block.
  596. FINAL is nonzero if we are doing the final pass which is not
  597. for computing the life info (since that has already been done)
  598. but for acting on it. On this pass, we delete dead stores,
  599. set up the logical links and dead-variables lists of instructions,
  600. and merge instructions for autoincrement and autodecrement addresses.
  601. SIGNIFICANT is nonzero only the first time for each basic block.
  602. If it is nonzero, it points to a regset in which we store
  603. a 1 for each register that is set within the block.
  604. BNUM is the number of the basic block. */
  605. static void
  606. propagate_block (old, first, last, final, significant, bnum)
  607. register regset old;
  608. rtx first;
  609. rtx last;
  610. int final;
  611. regset significant;
  612. int bnum;
  613. {
  614. register rtx insn;
  615. rtx prev;
  616. regset live;
  617. regset dead;
  618. /* The following variables are used only if FINAL is nonzero. */
  619. /* This vector gets one element for each reg that has been live
  620. at any point in the basic block that has been scanned so far.
  621. SOMETIMES_MAX says how many elements are in use so far.
  622. In each element, OFFSET is the byte-number within a regset
  623. for the register described by the element, and BIT is a mask
  624. for that register's bit within the byte. */
  625. register struct foo { short offset; short bit; } *regs_sometimes_live;
  626. int sometimes_max = 0;
  627. /* This regset has 1 for each reg that we have seen live so far.
  628. It and REGS_SOMETIMES_LIVE are updated together. */
  629. regset maxlive;
  630. loop_depth = basic_block_loop_depth[bnum];
  631. dead = (regset) alloca (regset_bytes);
  632. live = (regset) alloca (regset_bytes);
  633. if (final)
  634. {
  635. register int i, offset, bit;
  636. maxlive = (regset) alloca (regset_bytes);
  637. bcopy (old, maxlive, regset_bytes);
  638. regs_sometimes_live
  639. = (struct foo *) alloca (max_regno * sizeof (struct foo));
  640. /* Process the regs live at the end of the block.
  641. Enter them in MAXLIVE and REGS_SOMETIMES_LIVE.
  642. Also mark them as not local to any one basic block. */
  643. for (offset = 0, i = 0; offset < regset_size; offset++)
  644. for (bit = 1; bit; bit <<= 1, i++)
  645. {
  646. if (i == max_regno)
  647. break;
  648. if (old[offset] & bit)
  649. {
  650. reg_basic_block[i] = -2;
  651. regs_sometimes_live[sometimes_max].offset = offset;
  652. regs_sometimes_live[sometimes_max].bit = i % REGSET_ELT_BITS;
  653. sometimes_max++;
  654. }
  655. }
  656. }
  657. /* Scan the block an insn at a time from end to beginning. */
  658. for (insn = last; ; insn = prev)
  659. {
  660. prev = PREV_INSN (insn);
  661. if (final && GET_CODE (insn) == CALL_INSN)
  662. {
  663. /* Any regs live at the time of a call instruction
  664. must not go in a register clobbered by calls.
  665. Find all regs now live and record this for them. */
  666. register int i;
  667. register struct foo *p = regs_sometimes_live;
  668. for (i = 0; i < sometimes_max; i++, p++)
  669. {
  670. if (old[p->offset]
  671. & (1 << p->bit))
  672. reg_crosses_call[p->offset * REGSET_ELT_BITS + p->bit] = 1;
  673. }
  674. }
  675. /* Update the life-status of regs for this insn.
  676. First DEAD gets which regs are set in this insn
  677. then LIVE gets which regs are used in this insn.
  678. Then the regs live before the insn
  679. are those live after, with DEAD regs turned off,
  680. and then LIVE regs turned on. */
  681. if (GET_CODE (insn) == INSN
  682. || GET_CODE (insn) == JUMP_INSN
  683. || GET_CODE (insn) == CALL_INSN)
  684. {
  685. register int i;
  686. for (i = 0; i < regset_size; i++)
  687. {
  688. dead[i] = 0; /* Faster than bzero here */
  689. live[i] = 0; /* since regset_size is usually small */
  690. }
  691. /* If an instruction consists of just dead store(s) on final pass,
  692. "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
  693. We could really delete it with delete_insn, but that
  694. can cause trouble for first or last insn in a basic block. */
  695. if (final && insn_dead_p (PATTERN (insn), old))
  696. {
  697. PUT_CODE (insn, NOTE);
  698. NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  699. NOTE_SOURCE_FILE (insn) = 0;
  700. goto flushed;
  701. }
  702. else
  703. {
  704. /* Check for an opportunity to do predecrement or preincrement addressing. */
  705. #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
  706. register rtx x = PATTERN (insn);
  707. /* Does this instruction increment or decrement a register? */
  708. if (final && GET_CODE (x) == SET
  709. && GET_CODE (SET_DEST (x)) == REG
  710. && (GET_CODE (SET_SRC (x)) == PLUS
  711. || GET_CODE (SET_SRC (x)) == MINUS)
  712. && XEXP (SET_SRC (x), 0) == SET_DEST (x)
  713. && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
  714. /* Ok, look for a following memory ref we can combine with.
  715. If one is found, change the memory ref to a PRE_INC
  716. or PRE_DEC, cancel this insn, and return 1.
  717. Return 0 if nothing has been done. */
  718. && try_pre_increment_1 (insn))
  719. goto flushed;
  720. #endif /* HAVE_PRE_INCREMENT or HAVE_PRE_DECREMENT */
  721. /* LIVE gets the registers used in INSN; DEAD gets those set by it. */
  722. /* A function call implicitly sets the function-value register */
  723. if (GET_CODE (insn) == CALL_INSN)
  724. dead[FUNCTION_VALUE_REGNUM / REGSET_ELT_BITS]
  725. |= 1 << (FUNCTION_VALUE_REGNUM % REGSET_ELT_BITS);
  726. mark_set_regs (old, dead, PATTERN (insn), final ? insn : 0,
  727. significant);
  728. mark_used_regs (old, live, PATTERN (insn), final ? insn : 0);
  729. /* Update OLD for the registers used or set. */
  730. for (i = 0; i < regset_size; i++)
  731. {
  732. old[i] &= ~dead[i];
  733. old[i] |= live[i];
  734. }
  735. /* On final pass, add any additional sometimes-live regs
  736. into MAXLIVE and REGS_SOMETIMES_LIVE.
  737. Also update counts of how many insns each reg is live at. */
  738. if (final)
  739. {
  740. register int diff;
  741. for (i = 0; i < regset_size; i++)
  742. if (diff = live[i] & ~maxlive[i])
  743. {
  744. register int regno;
  745. maxlive[i] |= diff;
  746. for (regno = 0; diff && regno < REGSET_ELT_BITS; regno++)
  747. if (diff & (1 << regno))
  748. {
  749. regs_sometimes_live[sometimes_max].offset = i;
  750. regs_sometimes_live[sometimes_max].bit = regno;
  751. diff &= ~ (1 << regno);
  752. sometimes_max++;
  753. }
  754. }
  755. {
  756. register struct foo *p = regs_sometimes_live;
  757. for (i = 0; i < sometimes_max; i++, p++)
  758. {
  759. if (old[p->offset]
  760. & (1 << p->bit))
  761. reg_live_length[p->offset * REGSET_ELT_BITS + p->bit]++;
  762. }
  763. }
  764. /* This probably gets set to 1 in various places;
  765. make sure it is 0. */
  766. reg_crosses_call[FUNCTION_VALUE_REGNUM] = 0;
  767. }
  768. }
  769. flushed: ;
  770. }
  771. if (insn == first)
  772. break;
  773. }
  774. }
  775. /* Return 1 if X (the body of an insn, or part of it) is just dead stores
  776. (SET expressions whose destinations are registers dead after the insn).
  777. NEEDED is the regset that says which regs are alive after the insn. */
  778. static int
  779. insn_dead_p (x, needed)
  780. rtx x;
  781. regset needed;
  782. {
  783. register RTX_CODE code = GET_CODE (x);
  784. /* Make sure insns to set the stack pointer are never deleted. */
  785. needed[STACK_POINTER_REGNUM / REGSET_ELT_BITS]
  786. |= 1 << (STACK_POINTER_REGNUM % REGSET_ELT_BITS);
  787. if (code == SET && GET_CODE (SET_DEST (x)) == REG)
  788. {
  789. register int regno = REGNO (SET_DEST (x));
  790. register int offset = regno / REGSET_ELT_BITS;
  791. register int bit = 1 << (regno % REGSET_ELT_BITS);
  792. return (needed[offset] & bit) == 0;
  793. }
  794. if (code == PARALLEL)
  795. {
  796. register int i = XVECLEN (x, 0);
  797. for (i--; i >= 0; i--)
  798. if (!insn_dead_p (XVECEXP (x, 0, i), needed))
  799. return 0;
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. /* Return nonzero if register number REGNO is marked as "dying" in INSN's
  805. REG_NOTES list. */
  806. static int
  807. flow_deadp (regno, insn)
  808. int regno;
  809. rtx insn;
  810. {
  811. register rtx link;
  812. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  813. if (XEXP (link, 0)
  814. && (enum reg_note) GET_MODE (link) == REG_DEAD
  815. && regno == REGNO (XEXP (link, 0)))
  816. return 1;
  817. return 0;
  818. }
  819. /* Process the registers that are set within X.
  820. Their bits are set to 1 in the regset DEAD,
  821. because they are dead prior to this insn.
  822. If INSN is nonzero, it is the insn being processed
  823. and the fact that it is nonzero implies this is the FINAL pass
  824. in propagate_block. In this case, various info about register
  825. usage is stored, LOG_LINKS fields of insns are set up. */
  826. static void mark_set_1 ();
  827. static void
  828. mark_set_regs (needed, dead, x, insn, significant)
  829. regset needed;
  830. regset dead;
  831. rtx x;
  832. rtx insn;
  833. regset significant;
  834. {
  835. register RTX_CODE code = GET_CODE (x);
  836. if (code == SET || code == CLOBBER)
  837. mark_set_1 (needed, dead, x, insn, significant);
  838. else if (code == PARALLEL)
  839. {
  840. register int i;
  841. for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  842. {
  843. code = GET_CODE (XVECEXP (x, 0, i));
  844. if (code == SET || code == CLOBBER)
  845. mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
  846. }
  847. }
  848. }
  849. /* Process a single SET rtx, X. */
  850. static void
  851. mark_set_1 (needed, dead, x, insn, significant)
  852. regset needed;
  853. regset dead;
  854. rtx x;
  855. rtx insn;
  856. regset significant;
  857. {
  858. register int regno;
  859. register rtx reg = SET_DEST (x);
  860. if (GET_CODE (reg) == SUBREG)
  861. {
  862. /* Modifying just one hardware register
  863. of a multi-register value does not count as "setting"
  864. for live-dead analysis. Parts of the previous value
  865. might still be significant below this insn. */
  866. if (REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
  867. return;
  868. reg = SUBREG_REG (reg);
  869. }
  870. if (GET_CODE (reg) == REG
  871. && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
  872. && regno != ARG_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
  873. {
  874. register int offset = regno / REGSET_ELT_BITS;
  875. register int bit = 1 << (regno % REGSET_ELT_BITS);
  876. /* Mark the reg being set as dead before this insn. */
  877. dead[offset] |= bit;
  878. /* Mark it as a significant register for this basic block. */
  879. if (significant)
  880. significant[offset] |= bit;
  881. /* Additional data to record if this is the final pass. */
  882. if (insn)
  883. {
  884. register rtx y = reg_next_use[regno];
  885. register int blocknum = BLOCK_NUM (insn);
  886. /* If this is a hard reg, record this function uses the reg. */
  887. if (regno < FIRST_PSEUDO_REGISTER)
  888. {
  889. register int i;
  890. i = HARD_REGNO_NREGS (regno, GET_MODE (reg));
  891. do
  892. regs_ever_live[regno + --i] = 1;
  893. while (i > 0);
  894. }
  895. /* Keep track of which basic blocks each reg appears in. */
  896. if (reg_basic_block[regno] == -1)
  897. reg_basic_block[regno] = blocknum;
  898. else if (reg_basic_block[regno] != blocknum)
  899. reg_basic_block[regno] = -2;
  900. /* Count (weighted) references, stores, etc. */
  901. reg_n_refs[regno] += loop_depth;
  902. reg_n_sets[regno]++;
  903. /* The insns where a reg is live are normally counted elsewhere,
  904. but we want the count to include the insn where the reg is set,
  905. and the normal counting mechanism would not count it. */
  906. reg_live_length[regno]++;
  907. if (needed[offset] & bit)
  908. {
  909. /* Make a logical link from the next following insn
  910. that uses this register, back to this insn.
  911. The following insns have already been processed. */
  912. if (y && (BLOCK_NUM (y) == blocknum))
  913. LOG_LINKS (y)
  914. = gen_rtx (INSN_LIST, VOIDmode, insn, LOG_LINKS (y));
  915. }
  916. else
  917. {
  918. /* Note that dead stores have already been deleted when possible
  919. If we get here, we have found a dead store that cannot
  920. be eliminated (because the same insn does something useful).
  921. Indicate this by marking the reg being set as dying here. */
  922. REG_NOTES (insn)
  923. = gen_rtx (EXPR_LIST, REG_DEAD,
  924. reg, REG_NOTES (insn));
  925. }
  926. }
  927. }
  928. }
  929. /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
  930. This is done assuming the registers needed from X
  931. are those that have 1-bits in NEEDED.
  932. On the final pass, INSN is the containing instruction. */
  933. static void
  934. mark_used_regs (needed, live, x, insn)
  935. regset needed;
  936. regset live;
  937. rtx x;
  938. rtx insn;
  939. {
  940. register RTX_CODE code;
  941. register int regno;
  942. retry:
  943. code = GET_CODE (x);
  944. switch (code)
  945. {
  946. case LABEL_REF:
  947. case SYMBOL_REF:
  948. case CONST_INT:
  949. case CONST:
  950. case CC0:
  951. case PC:
  952. case CLOBBER:
  953. return;
  954. #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
  955. case MEM:
  956. /* Here we detect use of an index register which might
  957. be good for postincrement or postdecrement. */
  958. if (insn)
  959. {
  960. rtx addr = XEXP (x, 0);
  961. register int size = GET_MODE_SIZE (GET_MODE (x));
  962. if (GET_CODE (addr) == REG)
  963. {
  964. register rtx y;
  965. regno = REGNO (addr);
  966. /* Is the next use an increment that might make auto-increment? */
  967. y = reg_next_use[regno];
  968. if (y && GET_CODE (PATTERN (y)) == SET
  969. && BLOCK_NUM (y) == BLOCK_NUM (insn)
  970. && SET_DEST (PATTERN (y)) == addr
  971. /* Can't add side effects to jumps; if reg is spilled and
  972. reloaded, there's no way to store back the altered value. */
  973. && GET_CODE (insn) != JUMP_INSN
  974. && (y = SET_SRC (PATTERN (y)),
  975. (0
  976. #ifdef HAVE_POST_INCREMENT
  977. || GET_CODE (y) == PLUS
  978. #endif
  979. #ifdef HAVE_POST_DECREMENT
  980. || GET_CODE (y) == MINUS
  981. #endif
  982. )
  983. && XEXP (y, 0) == addr
  984. && GET_CODE (XEXP (y, 1)) == CONST_INT
  985. && INTVAL (XEXP (y, 1)) == size))
  986. {
  987. rtx use = find_use_as_address (PATTERN (insn), addr);
  988. /* Make sure this register appears only once in this insn. */
  989. if (use != 0 && use != (rtx) 1)
  990. {
  991. /* We have found a suitable auto-increment:
  992. do POST_INC around the register here,
  993. and patch out the increment instruction that follows. */
  994. XEXP (x, 0)
  995. = gen_rtx (GET_CODE (y) == PLUS ? POST_INC : POST_DEC,
  996. Pmode, addr);
  997. /* Record that this insn has an implicit side effect. */
  998. REG_NOTES (insn)
  999. = gen_rtx (EXPR_LIST, REG_INC, addr, REG_NOTES (insn));
  1000. y = reg_next_use[regno];
  1001. PUT_CODE (y, NOTE);
  1002. NOTE_LINE_NUMBER (y) = NOTE_INSN_DELETED;
  1003. NOTE_SOURCE_FILE (y) = 0;
  1004. /* Count a reference to this reg for the increment
  1005. insn we are deleting. When a reg is incremented.
  1006. spilling it is worse, so we want to make that
  1007. less likely. */
  1008. reg_n_refs[regno] += loop_depth;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. break;
  1014. #endif /* HAVE_POST_INCREMENT or HAVE_POST_DECREMENT */
  1015. case REG:
  1016. /* See a register other than being set
  1017. => mark it as needed. */
  1018. regno = REGNO (x);
  1019. if (regno != FRAME_POINTER_REGNUM
  1020. && regno != ARG_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
  1021. {
  1022. register int offset = regno / REGSET_ELT_BITS;
  1023. register int bit = 1 << (regno % REGSET_ELT_BITS);
  1024. live[offset] |= bit;
  1025. if (insn)
  1026. {
  1027. register int blocknum = BLOCK_NUM (insn);
  1028. /* If a hard reg is being used,
  1029. record that this function does use it. */
  1030. if (regno < FIRST_PSEUDO_REGISTER)
  1031. {
  1032. register int i;
  1033. i = HARD_REGNO_NREGS (regno, GET_MODE (x));
  1034. do
  1035. regs_ever_live[regno + --i] = 1;
  1036. while (i > 0);
  1037. }
  1038. /* Keep track of which basic block each reg appears in. */
  1039. if (reg_basic_block[regno] == -1)
  1040. reg_basic_block[regno] = blocknum;
  1041. else if (reg_basic_block[regno] != blocknum)
  1042. reg_basic_block[regno] = -2;
  1043. /* Record where each reg is used
  1044. so when the reg is set we know the next insn that uses it. */
  1045. reg_next_use[regno] = insn;
  1046. /* Count (weighted) number of uses of each reg. */
  1047. reg_n_refs[regno] += loop_depth;
  1048. /* Record and count the insns in which a reg dies.
  1049. If it is used in this insn and was dead below the insn
  1050. then it dies in this insn. */
  1051. if (!(needed[offset] & bit) && !flow_deadp (regno, insn))
  1052. {
  1053. REG_NOTES (insn)
  1054. = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (insn));
  1055. reg_n_deaths[regno]++;
  1056. }
  1057. }
  1058. }
  1059. return;
  1060. case SET:
  1061. {
  1062. register rtx reg = SET_DEST (x);
  1063. /* Modifying just one hardware register
  1064. of a multi-register value does not count as "setting"
  1065. for live-dead analysis. It is more like a reference.
  1066. But storing in a single register with an alternate mode
  1067. is storing in the register. */
  1068. if (GET_CODE (reg) == SUBREG
  1069. && !(REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg)))
  1070. reg = SUBREG_REG (reg);
  1071. /* If this is a store into a register,
  1072. recursively scan the only value being stored,
  1073. and only if the register's value is live after this insn.
  1074. If the value being computed here would never be used
  1075. then the values it uses don't need to be computed either. */
  1076. if (GET_CODE (reg) == REG
  1077. && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
  1078. && regno != ARG_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
  1079. {
  1080. register int offset = regno / REGSET_ELT_BITS;
  1081. register int bit = 1 << (regno % REGSET_ELT_BITS);
  1082. if (needed[offset] & bit)
  1083. mark_used_regs (needed, live, XEXP (x, 1), insn);
  1084. return;
  1085. }
  1086. }
  1087. break;
  1088. }
  1089. /* Recursively scan the operands of this expression. */
  1090. {
  1091. register char *fmt = GET_RTX_FORMAT (code);
  1092. register int i;
  1093. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1094. {
  1095. if (fmt[i] == 'e')
  1096. {
  1097. /* Tail recursive case: save a function call level. */
  1098. if (i == 0)
  1099. {
  1100. x = XEXP (x, 0);
  1101. goto retry;
  1102. }
  1103. mark_used_regs (needed, live, XEXP (x, i), insn);
  1104. }
  1105. if (fmt[i] == 'E')
  1106. {
  1107. register int j;
  1108. for (j = 0; j < XVECLEN (x, i); j++)
  1109. mark_used_regs (needed, live, XVECEXP (x, i, j), insn);
  1110. }
  1111. }
  1112. }
  1113. }
  1114. #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
  1115. static int
  1116. try_pre_increment_1 (insn)
  1117. rtx insn;
  1118. {
  1119. /* Find the next use of this reg. If in same basic block,
  1120. make it do pre-increment or pre-decrement if appropriate. */
  1121. rtx x = PATTERN (insn);
  1122. int amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
  1123. * INTVAL (XEXP (SET_SRC (x), 1)));
  1124. int regno = REGNO (SET_DEST (x));
  1125. rtx y = reg_next_use[regno];
  1126. if (y != 0
  1127. && BLOCK_NUM (y) == BLOCK_NUM (insn)
  1128. && try_pre_increment (y, SET_DEST (PATTERN (insn)),
  1129. amount))
  1130. {
  1131. /* We have found a suitable auto-increment
  1132. and already changed insn Y to do it.
  1133. So flush this increment-instruction. */
  1134. PUT_CODE (insn, NOTE);
  1135. NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  1136. NOTE_SOURCE_FILE (insn) = 0;
  1137. /* Count a reference to this reg for the increment
  1138. insn we are deleting. When a reg is incremented.
  1139. spilling it is worse, so we want to make that
  1140. less likely. */
  1141. reg_n_refs[regno] += loop_depth;
  1142. return 1;
  1143. }
  1144. return 0;
  1145. }
  1146. /* Try to change INSN so that it does pre-increment or pre-decrement
  1147. addressing on register REG in order to add AMOUNT to REG.
  1148. AMOUNT is negative for pre-decrement.
  1149. Returns 1 if the change could be made.
  1150. This checks all about the validity of the result of modifying INSN. */
  1151. static int
  1152. try_pre_increment (insn, reg, amount)
  1153. rtx insn, reg;
  1154. int amount;
  1155. {
  1156. register rtx use;
  1157. #ifndef HAVE_PRE_INCREMENT
  1158. #ifndef HAVE_PRE_DECREMENT
  1159. return 0;
  1160. #else
  1161. if (amount > 0)
  1162. return 0;
  1163. #endif
  1164. #endif
  1165. #ifndef HAVE_PRE_DECREMENT
  1166. if (amount < 0)
  1167. return 0;
  1168. #endif
  1169. /* It is not safe to add a side effect to a jump insn
  1170. because if the incremented register is spilled and must be reloaded
  1171. there would be no way to store the incremented value back in memory. */
  1172. if (GET_CODE (insn) == JUMP_INSN)
  1173. return 0;
  1174. use = find_use_as_address (PATTERN (insn), reg);
  1175. if (use == 0 || use == (rtx) 1)
  1176. return 0;
  1177. if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
  1178. return 0;
  1179. XEXP (use, 0) = gen_rtx (amount > 0 ? PRE_INC : PRE_DEC,
  1180. Pmode, reg);
  1181. /* Record that this insn now has an implicit side effect on X. */
  1182. REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, reg, REG_NOTES (insn));
  1183. return 1;
  1184. }
  1185. /* Find the place in the rtx X where REG is used as a memory address.
  1186. Return the MEM rtx that so uses it.
  1187. If REG does not appear, return 0.
  1188. If REG appears more than once, or is used other than as a memory address,
  1189. return (rtx)1. */
  1190. static rtx
  1191. find_use_as_address (x, reg)
  1192. register rtx x;
  1193. rtx reg;
  1194. {
  1195. enum rtx_code code = GET_CODE (x);
  1196. char *fmt = GET_RTX_FORMAT (code);
  1197. register int i;
  1198. register rtx value = 0;
  1199. register rtx tem;
  1200. if (code == MEM && XEXP (x, 0) == reg)
  1201. return x;
  1202. if (x == reg)
  1203. return (rtx) 1;
  1204. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1205. {
  1206. if (fmt[i] == 'e')
  1207. {
  1208. tem = find_use_as_address (XEXP (x, i), reg);
  1209. if (value == 0)
  1210. value = tem;
  1211. else if (tem != 0)
  1212. return (rtx) 1;
  1213. }
  1214. if (fmt[i] == 'E')
  1215. {
  1216. register int j;
  1217. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1218. {
  1219. tem = find_use_as_address (XVECEXP (x, i, j), reg);
  1220. if (value == 0)
  1221. value = tem;
  1222. else if (tem != 0)
  1223. return (rtx) 1;
  1224. }
  1225. }
  1226. }
  1227. return value;
  1228. }
  1229. #endif /* HAVE_PRE_INCREMENT or HAVE_PRE_DECREMENT */
  1230. /* Write information about registers and basic blocks into FILE.
  1231. This is part of making a debugging dump. */
  1232. dump_flow_info (file)
  1233. FILE *file;
  1234. {
  1235. register int i;
  1236. static char *reg_class_names[] = REG_CLASS_NAMES;
  1237. fprintf (file, "%d registers.\n", max_regno);
  1238. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1239. if (reg_n_refs[i])
  1240. {
  1241. register rtx chain;
  1242. enum reg_class class;
  1243. fprintf (file, "\nRegister %d used %d times across %d insns",
  1244. i, reg_n_refs[i], reg_live_length[i]);
  1245. if (reg_basic_block[i] >= 0)
  1246. fprintf (file, " in block %d", reg_basic_block[i]);
  1247. if (reg_n_deaths[i] != 1)
  1248. fprintf (file, "; dies in %d places", reg_n_deaths[i]);
  1249. if (reg_crosses_call[i])
  1250. fprintf (file, "; crosses calls");
  1251. if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
  1252. fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
  1253. class = reg_preferred_class (i);
  1254. if (class != GENERAL_REGS)
  1255. fprintf (file, "; pref %s", reg_class_names[(int) class]);
  1256. if (REGNO_POINTER_FLAG (i))
  1257. fprintf (file, "; pointer");
  1258. fprintf (file, ".\n");
  1259. }
  1260. fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
  1261. for (i = 0; i < n_basic_blocks; i++)
  1262. {
  1263. register rtx head, jump;
  1264. register int regno;
  1265. fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
  1266. i,
  1267. INSN_UID (basic_block_head[i]),
  1268. INSN_UID (basic_block_end[i]));
  1269. /* The control flow graph's storage is freed
  1270. now when flow_analysis returns.
  1271. Don't try to print it if it is gone. */
  1272. if (basic_block_drops_in)
  1273. {
  1274. fprintf (file, "Reached from blocks: ");
  1275. head = basic_block_head[i];
  1276. if (GET_CODE (head) == CODE_LABEL)
  1277. for (jump = LABEL_REFS (head);
  1278. jump != head;
  1279. jump = LABEL_NEXTREF (jump))
  1280. {
  1281. register from_block = BLOCK_NUM (CONTAINING_INSN (jump));
  1282. fprintf (file, " %d", from_block);
  1283. }
  1284. if (basic_block_drops_in[i])
  1285. fprintf (file, " previous");
  1286. }
  1287. fprintf (file, "\nRegisters live at start:");
  1288. for (regno = 0; regno < max_regno; regno++)
  1289. {
  1290. register int offset = regno / REGSET_ELT_BITS;
  1291. register int bit = 1 << (regno % REGSET_ELT_BITS);
  1292. if (basic_block_live_at_start[i][offset] & bit)
  1293. fprintf (file, " %d", regno);
  1294. }
  1295. fprintf (file, "\n");
  1296. }
  1297. fprintf (file, "\n");
  1298. }