loop.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /* Move constant computations out of loops.
  2. Copyright (C) 1987, 1988 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 is the loop optimization pass of the compiler.
  18. It finds invariant computations within loops and moves them
  19. to the beginning of the loop.
  20. It also finds cases where
  21. a register is set within the loop by zero-extending a narrower value
  22. and changes these to zero the entire register once before the loop
  23. and merely copy the low part within the loop.
  24. Most of the complexity is in heuristics to decide when it is worth
  25. while to do these things. */
  26. /* ??? verify_loop would run faster if we made one table
  27. of the minimum and maximum luids from which each label is reached.
  28. Also, it would be faster if loop_store_addrs were a hash table. */
  29. #include "config.h"
  30. #include "rtl.h"
  31. #include "insn-config.h"
  32. #include "regs.h"
  33. #include "recog.h"
  34. #include <stdio.h>
  35. /* Vector mapping INSN_UIDs to luids.
  36. The luids are like uids but increase monononically always.
  37. We use them to see whether a jump comes from outside a given loop. */
  38. static short *uid_luid;
  39. /* Get the luid of an insn. */
  40. #define INSN_LUID(INSN) (uid_luid[INSN_UID (INSN)])
  41. /* 1 + largest uid of any insn. */
  42. static int max_uid;
  43. /* 1 + luid of last insn. */
  44. static int max_luid;
  45. /* Nonzero if somewhere in the current loop
  46. there is either a subroutine call,
  47. or a store into a memory address that is not fixed,
  48. or a store in a BLKmode memory operand,
  49. or too many different fixed addresses stored in
  50. to record them all in `loop_store_addrs'.
  51. In any of these cases, no memory location can be regarded
  52. as invariant. */
  53. static int unknown_address_altered;
  54. /* Nonzero if somewhere in the current loop there is a store
  55. into a memory address that is not fixed but is known to be
  56. part of an aggregate.
  57. In this case, no memory reference in an aggregate may be
  58. considered invariant. */
  59. static int unknown_aggregate_altered;
  60. /* Nonzero if somewhere in the current loop there is a store
  61. into a memory address other than a fixed address not in an aggregate.
  62. In this case, no memory reference in an aggregate at a varying address
  63. may be considered invariant. */
  64. static int fixed_aggregate_altered;
  65. /* Nonzero if there is a subroutine call in the current loop.
  66. (unknown_address_altered is also nonzero in this case.) */
  67. static int loop_has_call;
  68. /* Array of fixed memory addresses that are stored in this loop.
  69. If there are too many to fit here,
  70. we just turn on unknown_address_altered. */
  71. #define NUM_STORES 10
  72. static rtx loop_store_addrs[NUM_STORES];
  73. static int loop_store_widths[NUM_STORES];
  74. /* Index of first available slot in above array. */
  75. static int loop_store_addrs_idx;
  76. /* During the analysis of a loop, a chain of `struct movable's
  77. is made to record all the movable insns found.
  78. Then the entire chain can be scanned to decide which to move. */
  79. struct movable
  80. {
  81. rtx insn; /* A movable insn */
  82. int consec; /* Number of consecutive following insns
  83. that must be moved with this one. */
  84. int regno; /* The register it sets */
  85. short lifetime; /* lifetime of that register;
  86. may be adjusted when matching movables
  87. that load the same value are found. */
  88. short times_used; /* Number of times the register is used,
  89. plus uses of related insns that could
  90. be moved if this one is. */
  91. unsigned int cond : 1; /* 1 if only conditionally movable */
  92. unsigned int force : 1; /* 1 means MUST move this insn */
  93. unsigned int global : 1; /* 1 means reg is live outside this loop */
  94. unsigned int done : 1; /* 1 inhibits further processing of this */
  95. unsigned int partial : 1; /* Moving this doesn't make it invariant. */
  96. struct movable *match; /* First entry for same value */
  97. struct movable *forces; /* An insn that must be moved if this is */
  98. struct movable *next;
  99. };
  100. static FILE *loop_dump_stream;
  101. static rtx verify_loop ();
  102. static int invariant_p ();
  103. static int can_jump_into_range_p ();
  104. static void count_loop_regs_set ();
  105. static void note_addr_stored ();
  106. static int loop_reg_used_before_p ();
  107. static void constant_high_bytes ();
  108. static void scan_loop ();
  109. static rtx replace_regs ();
  110. static void move_movables ();
  111. static int may_trap_p ();
  112. /* Entry point of this file. Perform loop optimization
  113. on the current function. F is the first insn of the function
  114. and NREGS is the number of register numbers used. */
  115. void
  116. loop_optimize (f, nregs, dumpfile)
  117. /* f is the first instruction of a chain of insns for one function */
  118. rtx f;
  119. /* nregs is the total number of registers used in it */
  120. int nregs;
  121. FILE *dumpfile;
  122. {
  123. register rtx insn;
  124. register int i;
  125. rtx end;
  126. rtx last_insn;
  127. loop_dump_stream = dumpfile;
  128. init_recog ();
  129. /* First find the last real insn, and count the number of insns,
  130. and assign insns their suids. */
  131. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  132. if (INSN_UID (insn) > i)
  133. i = INSN_UID (insn);
  134. max_uid = i + 1;
  135. uid_luid = (short *) alloca ((i + 1) * sizeof (short));
  136. bzero (uid_luid, (i + 1) * sizeof (short));
  137. /* Compute the mapping from uids to luids.
  138. LUIDs are numbers assigned to insns, like uids,
  139. except that luids increase monotonically through the code. */
  140. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  141. {
  142. last_insn = insn;
  143. INSN_LUID (insn) = ++i;
  144. }
  145. max_luid = i;
  146. /* Don't leave gaps in uid_luid for insns that have been
  147. deleted. It is possible that the first or last insn
  148. using some register has been deleted by cross-jumping.
  149. Make sure that uid_luid for that former insn's uid
  150. points to the general area where that insn used to be. */
  151. for (i = 0; i < max_uid; i++)
  152. {
  153. uid_luid[0] = uid_luid[i];
  154. if (uid_luid[0] != 0)
  155. break;
  156. }
  157. for (i = 0; i < max_uid; i++)
  158. if (uid_luid[i] == 0)
  159. uid_luid[i] = uid_luid[i - 1];
  160. /* Find and process each loop.
  161. We scan from the end, and process each loop when its start is seen,
  162. so we process innermost loops first. */
  163. for (insn = last_insn; insn; insn = PREV_INSN (insn))
  164. if (GET_CODE (insn) == NOTE
  165. && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  166. /* Make sure it really is a loop -- no jumps in from outside. */
  167. && (end = verify_loop (f, insn)))
  168. scan_loop (insn, end, nregs);
  169. }
  170. /* Optimize one loop whose start is LOOP_START and end is END.
  171. LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
  172. NOTE_INSN_LOOP_END. */
  173. static void
  174. scan_loop (loop_start, end, nregs)
  175. rtx loop_start, end;
  176. int nregs;
  177. {
  178. register int i;
  179. register rtx p = NEXT_INSN (loop_start);
  180. /* 1 if we are scanning insns that could be executed zero times. */
  181. int maybe_never = 0;
  182. /* For a rotated loop that is entered near the bottom,
  183. this is the label at the top. Otherwise it is zero. */
  184. rtx loop_top = 0;
  185. /* This is the insn (whatever kind) before the NOTE that starts the loop.
  186. Any insns moved out of the loop will follow it. */
  187. rtx before_start = PREV_INSN (loop_start);
  188. /* Jump insn that enters the loop, or 0 if control drops in. */
  189. rtx loop_entry_jump = 0;
  190. /* Place in the loop where control enters. */
  191. rtx scan_start;
  192. /* Number of insns in the loop. */
  193. int insn_count;
  194. int tem;
  195. /* Indexed by register number, contains the number of times the reg
  196. is set during the loop being scanned, or -1 if the insns that set it
  197. have all been scanned as candidates for being moved out of the loop.
  198. 0 indicates an invariant register; -1 a conditionally invariant one. */
  199. short *n_times_set;
  200. /* Indexed by register number, contains the number of times the reg
  201. was set during the loop being scanned, not counting changes due
  202. to moving these insns out of the loop. */
  203. short *n_times_used;
  204. /* Indexed by register number, contains 1 for a register whose
  205. assignments may not be moved out of the loop. */
  206. char *may_not_move;
  207. /* Chain describing insns movable in current loop. */
  208. struct movable *movables = 0;
  209. /* Last element in `movables' -- so we can add elements at the end. */
  210. struct movable *last_movable = 0;
  211. /* Ratio of extra register life span we can justify
  212. for saving an instruction. More if loop doesn't call subroutines
  213. since in that case saving an insn makes more difference
  214. and more registers are available. */
  215. int threshold = loop_has_call ? 17 : 34;
  216. n_times_set = (short *) alloca (nregs * sizeof (short));
  217. n_times_used = (short *) alloca (nregs * sizeof (short));
  218. may_not_move = (char *) alloca (nregs);
  219. /* Determine whether this loop starts with a jump down
  220. to a test at the end. */
  221. while (p != end
  222. && GET_CODE (p) != CODE_LABEL && GET_CODE (p) != JUMP_INSN)
  223. p = NEXT_INSN (p);
  224. /* "Loop" contains neither jumps nor labels;
  225. it must have been a dummy. Think no more about it. */
  226. if (p == end)
  227. return;
  228. scan_start = p;
  229. /* If loop has a jump before the first label,
  230. the true entry is the target of that jump.
  231. Start scan from there.
  232. But record in LOOP_TOP the place where the end-test jumps
  233. back to so we can scan that after the end of the loop. */
  234. if (GET_CODE (p) == JUMP_INSN)
  235. {
  236. loop_entry_jump = p;
  237. loop_top = NEXT_INSN (p);
  238. /* Loop entry will never be a conditional jump.
  239. If we see one, this must not be a real loop. */
  240. if (GET_CODE (loop_top) != BARRIER)
  241. return;
  242. p = JUMP_LABEL (p);
  243. /* Check to see whether the jump actually
  244. jumps out of the loop (meaning it's no loop).
  245. This case can happen for things like
  246. do {..} while (0). */
  247. if (p == 0
  248. || INSN_LUID (p) < INSN_LUID (loop_start)
  249. || INSN_LUID (p) >= INSN_LUID (end))
  250. {
  251. if (loop_dump_stream)
  252. fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
  253. INSN_UID (loop_start), INSN_UID (end));
  254. return;
  255. }
  256. /* Find the first label after the entry-jump. */
  257. while (GET_CODE (loop_top) != CODE_LABEL)
  258. {
  259. loop_top = NEXT_INSN (loop_top);
  260. if (loop_top == 0)
  261. abort ();
  262. }
  263. /* Maybe rearrange the loop to drop straight in
  264. with a new test to jump around it entirely.
  265. (The latter is considered outside the loop.)
  266. If this is done, we no longer enter with a jump. */
  267. if (loop_skip_over (loop_start, end, loop_entry_jump))
  268. loop_top = 0;
  269. else
  270. /* We really do enter with a jump;
  271. scan the loop from the place where the jump jumps to. */
  272. scan_start = p;
  273. }
  274. /* Count number of times each reg is set during this loop.
  275. Set MAY_NOT_MOVE[I] if it is not safe to move out
  276. the setting of register I. */
  277. bzero (n_times_set, nregs * sizeof (short));
  278. bzero (may_not_move, nregs);
  279. count_loop_regs_set (loop_top ? loop_top : loop_start, end,
  280. n_times_set, may_not_move,
  281. &insn_count, nregs);
  282. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  283. may_not_move[i] = 1, n_times_set[i] = 1;
  284. bcopy (n_times_set, n_times_used, nregs * sizeof (short));
  285. if (loop_dump_stream)
  286. fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns\n\n",
  287. INSN_UID (loop_start), INSN_UID (end), insn_count);
  288. /* Scan through the loop finding insns that are safe to move.
  289. In each such insn, store QImode as the mode, to mark it.
  290. Then set n_times_set to -1 for the reg being set, so that
  291. this reg will be considered invariant for subsequent insns.
  292. We consider whether subsequent insns use the reg
  293. in deciding whether it is worth actually moving.
  294. MAYBE_NEVER is nonzero if we have passed a conditional jump insn
  295. and therefore it is possible that the insns we are scanning
  296. would never be executed. At such times, we must make sure
  297. that it is safe to execute the insn once instead of zero times.
  298. When MAYBE_NEVER is 0, all insns will be executed at least once
  299. so that is not a problem. */
  300. p = scan_start;
  301. while (1)
  302. {
  303. p = NEXT_INSN (p);
  304. /* At end of a straight-in loop, we are done.
  305. At end of a loop entered at the bottom, scan the top. */
  306. if (p == scan_start)
  307. break;
  308. if (p == end)
  309. {
  310. if (loop_top != 0)
  311. p = NEXT_INSN (loop_top);
  312. else
  313. break;
  314. if (p == scan_start)
  315. break;
  316. }
  317. if (GET_CODE (p) == INSN
  318. && GET_CODE (PATTERN (p)) == SET
  319. && GET_CODE (SET_DEST (PATTERN (p))) == REG
  320. && ! may_not_move[REGNO (SET_DEST (PATTERN (p)))])
  321. {
  322. /* If this register is used or set outside the loop,
  323. then we can move it only if we know this insn is
  324. executed exactly once per iteration,
  325. and we can check all the insns executed before it
  326. to make sure none of them used the value that
  327. was lying around at entry to the loop. */
  328. if ((uid_luid[regno_last_uid[REGNO (SET_DEST (PATTERN (p)))]] > INSN_LUID (end)
  329. || uid_luid[regno_first_uid[REGNO (SET_DEST (PATTERN (p)))]] < INSN_LUID (loop_start))
  330. && (maybe_never
  331. || loop_reg_used_before_p (p, loop_start, scan_start, end)))
  332. ;
  333. /* If this can cause a trap (such as divide by zero), can't move it
  334. unless it's guaranteed to be executed once loop is entered. */
  335. else if (maybe_never && may_trap_p (SET_SRC (PATTERN (p))))
  336. ;
  337. else if ((tem = invariant_p (SET_SRC (PATTERN (p)), n_times_set))
  338. && (n_times_set[REGNO (SET_DEST (PATTERN (p)))] == 1
  339. || consec_sets_invariant_p (SET_DEST (PATTERN (p)),
  340. p, n_times_set)))
  341. {
  342. register struct movable *m;
  343. register int regno = REGNO (SET_DEST (PATTERN (p)));
  344. int count;
  345. m = (struct movable *) alloca (sizeof (struct movable));
  346. m->next = 0;
  347. m->insn = p;
  348. m->force = 0;
  349. m->consec = n_times_set[REGNO (SET_DEST (PATTERN (p)))] - 1;
  350. m->done = 0;
  351. m->forces = 0;
  352. m->partial = 0;
  353. m->regno = regno;
  354. m->cond = (tem > 1);
  355. m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
  356. || uid_luid[regno_first_uid[regno]] < INSN_LUID (loop_start));
  357. m->match = 0;
  358. m->lifetime = (uid_luid[regno_last_uid[regno]]
  359. - uid_luid[regno_first_uid[regno]]);
  360. m->times_used = n_times_used[regno];
  361. n_times_set[regno] = -1;
  362. /* Add M to the end of the chain MOVABLES. */
  363. if (movables == 0)
  364. movables = m;
  365. else
  366. last_movable->next = m;
  367. last_movable = m;
  368. /* Skip the consecutive insns, if there are any. */
  369. for (count = m->consec - 1; count >= 0; count--)
  370. {
  371. do p = NEXT_INSN (p);
  372. while (GET_CODE (p) == NOTE);
  373. }
  374. }
  375. /* If this register is always set within a STRICT_LOW_PART
  376. or set to zero, then its high bytes are constant.
  377. So clear them outside the loop and within the loop
  378. just load the low bytes.
  379. We must check that the machine has an instruction to do so.
  380. Also, if the value loaded into the register
  381. depends on the same register, this cannot be done. */
  382. else if (SET_SRC (PATTERN (p)) == const0_rtx
  383. && GET_CODE (NEXT_INSN (p)) == INSN
  384. && GET_CODE (PATTERN (NEXT_INSN (p))) == SET
  385. && (GET_CODE (SET_DEST (PATTERN (NEXT_INSN (p))))
  386. == STRICT_LOW_PART)
  387. && (GET_CODE (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
  388. == SUBREG)
  389. && (SUBREG_REG (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
  390. == SET_DEST (PATTERN (p)))
  391. && !reg_mentioned_p (SET_DEST (PATTERN (p)),
  392. SET_SRC (PATTERN (NEXT_INSN (p)))))
  393. {
  394. register int regno = REGNO (SET_DEST (PATTERN (p)));
  395. if (n_times_set[regno] == 2)
  396. {
  397. register struct movable *m;
  398. int count;
  399. m = (struct movable *) alloca (sizeof (struct movable));
  400. m->next = 0;
  401. m->insn = p;
  402. m->force = 0;
  403. m->consec = 0;
  404. m->done = 0;
  405. m->forces = 0;
  406. m->partial = 1;
  407. m->regno = regno;
  408. m->cond = 0;
  409. /* Say "global" so this register is not combined
  410. with any other. In fact, it is sometimes possible
  411. to combine two of these registers, but the criteria
  412. are special and have not been programmed in. */
  413. m->global = 1;
  414. m->match = 0;
  415. m->lifetime = (uid_luid[regno_last_uid[regno]]
  416. - uid_luid[regno_first_uid[regno]]);
  417. m->times_used = n_times_used[regno];
  418. n_times_set[regno] = -1;
  419. /* Add M to the end of the chain MOVABLES. */
  420. if (movables == 0)
  421. movables = m;
  422. else
  423. last_movable->next = m;
  424. last_movable = m;
  425. /* Skip the consecutive insns, if there are any. */
  426. for (count = m->consec - 1; count >= 0; count--)
  427. {
  428. do p = NEXT_INSN (p);
  429. while (GET_CODE (p) == NOTE);
  430. }
  431. }
  432. }
  433. }
  434. /* Past a label or a jump, we get to insns for which we
  435. can't count on whether or how many times they will be
  436. executed during each iteration. Therefore, we can
  437. only move out sets of trivial variables
  438. (those not used after the loop). */
  439. else if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
  440. maybe_never = 1;
  441. }
  442. /* For each movable insn, see if the reg that it loads
  443. leads when it dies right into another conditionally movable insn.
  444. If so, record that the second insn "forces" the first one,
  445. since the second can be moved only if the first is. */
  446. {
  447. register struct movable *m, *m1;
  448. for (m1 = movables; m1; m1 = m1->next)
  449. {
  450. int regno = m1->regno;
  451. for (m = m1->next; m; m = m->next)
  452. if (INSN_UID (m->insn) == regno_last_uid[regno])
  453. break;
  454. if (m != 0 && SET_SRC (PATTERN (m->insn)) == SET_DEST (PATTERN (m1->insn)))
  455. m = 0;
  456. /* Increase the priority of the moving the first insn
  457. since it permits the second to be moved as well. */
  458. if (m != 0)
  459. {
  460. m->forces = m1;
  461. m1->lifetime += m->lifetime;
  462. m1->times_used += m1->times_used;
  463. }
  464. }
  465. }
  466. /* See if there are multiple movable insns that load the same value.
  467. If there are, make all but the first point at the first one
  468. through the `match' field, and add the priorities of them
  469. all together as the priority of the first. */
  470. {
  471. register struct movable *m;
  472. char *matched_regs = (char *) alloca (nregs);
  473. /* Regs that are used more than once are not allowed to match
  474. or be matched. I'm no longer sure why not. */
  475. for (m = movables; m; m = m->next)
  476. if (m->match == 0 && n_times_used[m->regno] == 1)
  477. {
  478. register struct movable *m1;
  479. int regno = m->regno;
  480. bzero (matched_regs, nregs);
  481. matched_regs[regno] = 1;
  482. for (m1 = m->next; m1; m1 = m1->next)
  483. if (m1->match == 0 && n_times_used[m1->regno] == 1
  484. /* A reg used outside the loop mustn't be eliminated. */
  485. && !m1->global
  486. && (matched_regs[m1->regno]
  487. ||
  488. (
  489. /* Can't combine regs with different modes
  490. even if loaded from the same constant. */
  491. (GET_MODE (SET_DEST (PATTERN (m->insn)))
  492. == GET_MODE (SET_DEST (PATTERN (m1->insn))))
  493. /* See if the source of M1 says it matches M. */
  494. && ((GET_CODE (SET_SRC (PATTERN (m1->insn))) == REG
  495. && matched_regs[REGNO (SET_SRC (PATTERN (m1->insn)))])
  496. || rtx_equal_p (SET_SRC (PATTERN (m->insn)),
  497. SET_SRC (PATTERN (m1->insn)))
  498. || (REG_NOTES (m->insn) && REG_NOTES (m1->insn)
  499. && REG_NOTE_KIND (REG_NOTES (m->insn)) == REG_EQUIV
  500. && REG_NOTE_KIND (REG_NOTES (m1->insn)) == REG_EQUIV
  501. && rtx_equal_p (XEXP (REG_NOTES (m->insn), 0),
  502. XEXP (REG_NOTES (m1->insn), 0)))))))
  503. {
  504. m->lifetime += m1->lifetime;
  505. m->times_used += m1->times_used;
  506. m1->match = m;
  507. matched_regs[m1->regno] = 1;
  508. }
  509. }
  510. }
  511. /* Now consider each movable insn to decide whether it is worth moving. */
  512. move_movables (movables, n_times_set, n_times_used, threshold,
  513. insn_count, loop_start, end, nregs);
  514. }
  515. /* Scan MOVABLES, and move the insns that deserve to be moved.
  516. If two matching movables are combined, replace one reg with the
  517. other throughout. */
  518. static void
  519. move_movables (movables, n_times_set, n_times_used, threshold,
  520. insn_count, loop_start, end, nregs)
  521. struct movable *movables;
  522. short *n_times_set;
  523. short *n_times_used;
  524. int threshold;
  525. int insn_count;
  526. rtx loop_start;
  527. rtx end;
  528. int nregs;
  529. {
  530. rtx new_start = 0;
  531. register struct movable *m;
  532. register rtx p;
  533. /* Map of pseudo-register replacements to handle combining
  534. when we move several insns that load the same value
  535. into different pseudo-registers. */
  536. rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
  537. char *already_moved = (char *) alloca (nregs);
  538. bzero (already_moved, nregs);
  539. bzero (reg_map, nregs * sizeof (rtx));
  540. for (m = movables; m; m = m->next)
  541. {
  542. /* Describe this movable insn. */
  543. if (loop_dump_stream)
  544. {
  545. fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
  546. INSN_UID (m->insn), m->regno, m->lifetime);
  547. if (m->consec > 0)
  548. fprintf (loop_dump_stream, "consec %d, ", m->consec);
  549. if (m->cond)
  550. fprintf (loop_dump_stream, "cond ");
  551. if (m->force)
  552. fprintf (loop_dump_stream, "force ");
  553. if (m->global)
  554. fprintf (loop_dump_stream, "global ");
  555. if (m->done)
  556. fprintf (loop_dump_stream, "done ");
  557. if (m->match)
  558. fprintf (loop_dump_stream, "matches %d ",
  559. INSN_UID (m->match->insn));
  560. if (m->forces)
  561. fprintf (loop_dump_stream, "forces %d ",
  562. INSN_UID (m->forces->insn));
  563. }
  564. /* Ignore the insn if it's already done (it matched something else).
  565. Otherwise, see if it is now safe to move. */
  566. if (!m->done
  567. && (! m->cond
  568. || 1 == invariant_p (SET_SRC (PATTERN (m->insn)), n_times_set))
  569. && (! m->forces || m->forces->done))
  570. {
  571. register int regno;
  572. register rtx p;
  573. int times_used = m->times_used + m->consec;
  574. /* We have an insn that is safe to move.
  575. Compute its desirability. */
  576. p = m->insn;
  577. regno = m->regno;
  578. if (loop_dump_stream)
  579. fprintf (loop_dump_stream, "reg uses %d ", times_used);
  580. /* An insn MUST be moved if we already moved something else
  581. which is safe only if this one is moved too: that is,
  582. if already_moved[REGNO] is nonzero. */
  583. /* An insn is desirable to move if the new lifetime of the
  584. register is no more than THRESHOLD times the old lifetime.
  585. If it's not desirable, it means the loop is so big
  586. that moving won't speed things up much,
  587. and it is liable to make register usage worse. */
  588. /* It is also desirable to move if it can be moved at no
  589. extra cost because something else was already moved. */
  590. if (already_moved[regno]
  591. || (threshold * times_used * m->lifetime) >= insn_count
  592. || (m->forces && m->forces->done
  593. && n_times_used[m->forces->regno] == 1))
  594. {
  595. int count;
  596. register struct movable *m1;
  597. for (count = m->consec; count >= 0; count--)
  598. {
  599. rtx i1 = emit_insn_before (PATTERN (p), loop_start);
  600. if (new_start == 0)
  601. new_start = i1;
  602. if (loop_dump_stream)
  603. fprintf (loop_dump_stream, "moved to %d", INSN_UID (i1));
  604. /* Mark the moved, invariant reg as being equivalent to
  605. its constant value. */
  606. REG_NOTES (i1) = REG_NOTES (p);
  607. if (REG_NOTES (i1) == 0
  608. && ! m->partial /* But not if its a zero-extend clr. */
  609. && ! m->global /* and not if used outside the loop
  610. (since it might get set outside). */
  611. && CONSTANT_P (SET_SRC (PATTERN (p))))
  612. REG_NOTES (i1)
  613. = gen_rtx (EXPR_LIST, REG_EQUIV,
  614. SET_SRC (PATTERN (p)), REG_NOTES (i1));
  615. delete_insn (p);
  616. do p = NEXT_INSN (p);
  617. while (GET_CODE (p) == NOTE);
  618. /* The more insns we move, the less we like moving them. */
  619. threshold -= 2;
  620. }
  621. /* Any other movable that loads the same register
  622. MUST be moved. */
  623. already_moved[regno] = 1;
  624. /* The reg set here is now invariant. */
  625. if (! m->partial)
  626. n_times_set[regno] = 0;
  627. m->done = 1;
  628. /* Combine with this moved insn any other matching movables. */
  629. for (m1 = m->next; m1; m1 = m1->next)
  630. if (m1->match == m)
  631. {
  632. /* Schedule the reg loaded by M1
  633. for replacement so that shares the reg of M. */
  634. reg_map[m1->regno] = SET_DEST (PATTERN (m->insn));
  635. /* Get rid of the matching insn
  636. and prevent further processing of it. */
  637. m1->done = 1;
  638. delete_insn (m1->insn);
  639. /* Any other movable that loads the same register
  640. MUST be moved. */
  641. already_moved[m1->regno] = 1;
  642. /* The reg merged here is now invariant. */
  643. if (m->partial)
  644. n_times_set[m1->regno] = 0;
  645. }
  646. }
  647. else if (loop_dump_stream)
  648. fprintf (loop_dump_stream, "not desirable");
  649. }
  650. else if (loop_dump_stream && !m->match)
  651. fprintf (loop_dump_stream, "not safe");
  652. if (loop_dump_stream)
  653. fprintf (loop_dump_stream, "\n");
  654. }
  655. if (new_start == 0)
  656. new_start = loop_start;
  657. /* Go through all the instructions in the loop, making
  658. all the register substitutions scheduled in REG_MAP. */
  659. for (p = new_start; p != end; p = NEXT_INSN (p))
  660. if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
  661. || GET_CODE (p) == CALL_INSN)
  662. replace_regs (PATTERN (p), reg_map);
  663. }
  664. /* Optionally change a loop which enters just before the endtest
  665. to one which falls straight in
  666. after skipping around the entire loop if the endtest would drop out.
  667. Returns 1 if the change was made, 0 if the loop was not really suitable. */
  668. int
  669. loop_skip_over (start, end, loop_entry_jump)
  670. rtx start;
  671. rtx end;
  672. rtx loop_entry_jump;
  673. {
  674. rtx endtestjump;
  675. register rtx p = JUMP_LABEL (loop_entry_jump);
  676. while (GET_CODE (p) != INSN && GET_CODE (p) != JUMP_INSN
  677. && GET_CODE (p) != CALL_INSN)
  678. p = NEXT_INSN (p);
  679. endtestjump = next_real_insn (p);
  680. /* Check that we (1) enter at a compare insn and (2)
  681. the insn (presumably a jump) following that compare
  682. is the last in the loop and jumps back to the loop beginning. */
  683. if (GET_CODE (PATTERN (p)) == SET
  684. && SET_DEST (PATTERN (p)) == cc0_rtx
  685. && endtestjump == prev_real_insn (end)
  686. && prev_real_insn (JUMP_LABEL (endtestjump)) == loop_entry_jump)
  687. {
  688. rtx newlab;
  689. /* This is the jump that we insert. */
  690. rtx new_jump;
  691. /* Ok, duplicate that test before start of loop. */
  692. emit_insn_before (copy_rtx (PATTERN (p)), start);
  693. /* Make a new entry-jump (before the original one)
  694. whose condition is opposite to the loop-around endtest
  695. and which jumps around the loop (to just after the endtest). */
  696. newlab = gen_label_rtx ();
  697. emit_label_after (newlab, endtestjump);
  698. emit_jump_insn_before (copy_rtx (PATTERN (endtestjump)), start);
  699. new_jump = PREV_INSN (start);
  700. JUMP_LABEL (new_jump) = JUMP_LABEL (endtestjump);
  701. LABEL_NUSES (JUMP_LABEL (endtestjump))++;
  702. invert_jump (new_jump, newlab);
  703. /* Delete the original entry-jump. */
  704. delete_insn (loop_entry_jump);
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. /* Throughout the rtx X, replace many registers according to REG_MAP.
  710. Return the replacement for X (which may be X with altered contents).
  711. REG_MAP[R] is the replacement for register R, or 0 for don't replace. */
  712. static rtx
  713. replace_regs (x, reg_map)
  714. rtx x;
  715. rtx *reg_map;
  716. {
  717. register RTX_CODE code = GET_CODE (x);
  718. register int i;
  719. register char *fmt;
  720. switch (code)
  721. {
  722. case PC:
  723. case CC0:
  724. case CONST_INT:
  725. case CONST_DOUBLE:
  726. case CONST:
  727. case SYMBOL_REF:
  728. case LABEL_REF:
  729. return x;
  730. case REG:
  731. if (reg_map[REGNO (x)] != 0)
  732. return reg_map[REGNO (x)];
  733. return x;
  734. }
  735. fmt = GET_RTX_FORMAT (code);
  736. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  737. {
  738. if (fmt[i] == 'e')
  739. XEXP (x, i) = replace_regs (XEXP (x, i), reg_map);
  740. if (fmt[i] == 'E')
  741. {
  742. register int j;
  743. for (j = 0; j < XVECLEN (x, i); j++)
  744. XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map);
  745. }
  746. }
  747. return x;
  748. }
  749. /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
  750. Replace it with an instruction to load just the low bytes
  751. if the machine supports such an instruction,
  752. and insert above LOOP_START an instruction to clear the register. */
  753. static void
  754. constant_high_bytes (p, loop_start)
  755. rtx p, loop_start;
  756. {
  757. register rtx new;
  758. register int insn_code_number;
  759. /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
  760. to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
  761. new = gen_rtx (SET, VOIDmode,
  762. gen_rtx (STRICT_LOW_PART, VOIDmode,
  763. gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
  764. SET_DEST (PATTERN (p)),
  765. 0)),
  766. XEXP (SET_SRC (PATTERN (p)), 0));
  767. insn_code_number = recog (new, p);
  768. if (insn_code_number)
  769. {
  770. register int i;
  771. /* Clear destination register before the loop. */
  772. emit_insn_before (gen_rtx (SET, VOIDmode,
  773. SET_DEST (PATTERN (p)),
  774. const0_rtx),
  775. loop_start);
  776. /* Inside the loop, just load the low part. */
  777. PATTERN (p) = new;
  778. }
  779. }
  780. /* Verify that the ostensible loop starting at START
  781. really is a loop: nothing jumps into it from outside.
  782. Return the marker for the end of the loop, or zero if not a real loop.
  783. Also set the variables `unknown_*_altered' and `loop_has_call',
  784. and fill in the array `loop_store_addrs'. */
  785. static rtx
  786. verify_loop (f, start)
  787. rtx f, start;
  788. {
  789. register int level = 1;
  790. register rtx insn, end;
  791. /* First find the LOOP_END that matches.
  792. Also check each insn for storing in memory and record where. */
  793. unknown_address_altered = 0;
  794. unknown_aggregate_altered = 0;
  795. fixed_aggregate_altered = 0;
  796. loop_has_call = 0;
  797. loop_store_addrs_idx = 0;
  798. for (insn = NEXT_INSN (start); level > 0; insn = NEXT_INSN (insn))
  799. {
  800. if (insn == 0)
  801. /* Parse errors can cause a loop-beg with no loop-end. */
  802. return 0;
  803. if (GET_CODE (insn) == NOTE)
  804. {
  805. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  806. ++level;
  807. else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  808. {
  809. --level;
  810. if (level == 0)
  811. {
  812. end = insn;
  813. break;
  814. }
  815. }
  816. }
  817. else if (GET_CODE (insn) == CALL_INSN)
  818. {
  819. unknown_address_altered = 1;
  820. loop_has_call = 1;
  821. }
  822. else if (! unknown_address_altered)
  823. {
  824. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  825. note_stores (PATTERN (insn), note_addr_stored);
  826. }
  827. }
  828. /* Now scan all jumps in the function and see if any of them can
  829. reach a label within the range of the loop. */
  830. for (insn = f; insn; insn = NEXT_INSN (insn))
  831. if (GET_CODE (insn) == JUMP_INSN
  832. /* Don't get fooled by jumps inserted by loop-optimize.
  833. They don't have valid LUIDs, and they never jump into loops. */
  834. && INSN_UID (insn) < max_uid
  835. && (INSN_LUID (insn) < INSN_LUID (start)
  836. || INSN_LUID (insn) > INSN_LUID (end))
  837. /* We have a jump that is outside the loop.
  838. Does it jump into the loop? */
  839. && can_jump_into_range_p (PATTERN (insn),
  840. INSN_LUID (start), INSN_LUID (end)))
  841. return 0;
  842. #if 0
  843. /* Now scan all labels between them and check for any jumps from outside.
  844. This uses the ref-chains set up by find_basic_blocks.
  845. This code is not used because it's more convenient for other reasons
  846. to do the loop optimization before find_basic_blocks. */
  847. for (insn = start; insn != end; insn = NEXT_INSN (insn))
  848. if (GET_CODE (insn) == CODE_LABEL)
  849. {
  850. register rtx y;
  851. for (y = LABEL_REFS (insn); y != insn; y = LABEL_NEXTREF (y))
  852. if (INSN_LUID (CONTAINING_INSN (y)) < INSN_LUID (start)
  853. || INSN_LUID (CONTAINING_INSN (y)) > INSN_LUID (end))
  854. return 0;
  855. }
  856. #endif
  857. return end;
  858. }
  859. /* Return 1 if somewhere in X is a LABEL_REF to a label
  860. located between BEG and END. */
  861. static int
  862. can_jump_into_range_p (x, beg, end)
  863. rtx x;
  864. int beg, end;
  865. {
  866. register RTX_CODE code = GET_CODE (x);
  867. register int i;
  868. register char *fmt;
  869. if (code == LABEL_REF)
  870. {
  871. register int luid = INSN_LUID (XEXP (x, 0));
  872. return luid > beg && luid < end;
  873. }
  874. fmt = GET_RTX_FORMAT (code);
  875. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  876. {
  877. if (fmt[i] == 'e')
  878. {
  879. if (can_jump_into_range_p (XEXP (x, i), beg, end))
  880. return 1;
  881. }
  882. else if (fmt[i] == 'E')
  883. {
  884. register int j;
  885. for (j = 0; j < XVECLEN (x, i); j++)
  886. if (can_jump_into_range_p (XVECEXP (x, i, j), beg, end))
  887. return 1;
  888. }
  889. }
  890. return 0;
  891. }
  892. /* Record that a memory reference X is being set. */
  893. static void
  894. note_addr_stored (x)
  895. rtx x;
  896. {
  897. rtx addr;
  898. if (x == 0 || GET_CODE (x) != MEM)
  899. return;
  900. if (GET_MODE (x) == BLKmode)
  901. unknown_address_altered = 1;
  902. else if (rtx_addr_varies_p (x))
  903. {
  904. if (GET_CODE (XEXP (x, 0)) == PLUS)
  905. unknown_aggregate_altered = 1;
  906. else
  907. unknown_address_altered = 1;
  908. }
  909. else
  910. {
  911. register int i;
  912. register rtx addr = XEXP (x, 0);
  913. if (x->in_struct)
  914. fixed_aggregate_altered = 1;
  915. for (i = 0; i < loop_store_addrs_idx; i++)
  916. if (rtx_equal_p (loop_store_addrs[i], addr))
  917. {
  918. if (loop_store_widths[i] < GET_MODE_SIZE (GET_MODE (x)))
  919. loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
  920. break;
  921. }
  922. if (i == NUM_STORES)
  923. unknown_address_altered = 1;
  924. else if (i == loop_store_addrs_idx)
  925. {
  926. loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
  927. loop_store_addrs[loop_store_addrs_idx++] = addr;
  928. }
  929. }
  930. }
  931. /* Return nonzero if the rtx X is invariant over the current loop.
  932. N_TIMES_SET is a vector whose element I is nonzero if register I
  933. is set during the loop.
  934. The value is 2 if we refer to something only conditionally invariant.
  935. If `unknown_address_altered' is nonzero, no memory ref is invariant.
  936. Otherwise if `unknown_aggregate_altered' is nonzero,
  937. a memory ref is invariant if it is not part of an aggregate
  938. and its address is fixed and not in `loop_store_addrs'.
  939. Otherwise if `fixed_aggregate_altered' is nonzero,
  940. a memory ref is invariant
  941. if its address is fixed and not in `loop_store_addrs'.
  942. Otherwise, a memory ref is invariant if its address is fixed and not in
  943. `loop_store_addrs' or if it is not an aggregate. */
  944. static int
  945. invariant_p (x, n_times_set)
  946. register rtx x;
  947. short *n_times_set;
  948. {
  949. register int i;
  950. register RTX_CODE code = GET_CODE (x);
  951. register char *fmt;
  952. int conditional = 0;
  953. switch (code)
  954. {
  955. case CONST_INT:
  956. case CONST_DOUBLE:
  957. case SYMBOL_REF:
  958. case LABEL_REF:
  959. case CONST:
  960. return 1;
  961. case PC:
  962. case CC0:
  963. return 0;
  964. case REG:
  965. if (x == frame_pointer_rtx || x == arg_pointer_rtx
  966. || x->unchanging)
  967. return 1;
  968. if (n_times_set[REGNO (x)] == -1)
  969. return 2;
  970. return n_times_set[REGNO (x)] == 0;
  971. case MEM:
  972. /* A store in a varying-address scalar (or a subroutine call)
  973. could clobber anything in memory. */
  974. if (unknown_address_altered)
  975. return 0;
  976. /* Don't mess with volatile memory references. */
  977. if (x->volatil)
  978. return 0;
  979. /* If it's declared read-only, it is invariant
  980. if its address is invariant. */
  981. if (x->unchanging)
  982. return invariant_p (XEXP (x, 0), n_times_set);
  983. /* A store in a varying-address aggregate component
  984. could clobber anything except a scalar with a fixed address. */
  985. if (unknown_aggregate_altered
  986. && ((x->in_struct || GET_CODE (XEXP (x, 0)) == PLUS)
  987. || rtx_addr_varies_p (x)))
  988. return 0;
  989. /* A store in a fixed-address aggregate component
  990. could clobber anything whose address is not fixed,
  991. even an aggregate component. */
  992. if (fixed_aggregate_altered
  993. && rtx_addr_varies_p (x))
  994. return 0;
  995. /* Any store could clobber a varying-address scalar. */
  996. if (loop_store_addrs_idx
  997. && !(x->in_struct || GET_CODE (XEXP (x, 0)) == PLUS)
  998. && rtx_addr_varies_p (x))
  999. return 0;
  1000. /* A store in a fixed address clobbers overlapping references. */
  1001. for (i = loop_store_addrs_idx - 1; i >= 0; i--)
  1002. if (addr_overlap_p (x, loop_store_addrs[i], loop_store_widths[i]))
  1003. return 0;
  1004. /* It's not invalidated by a store in memory
  1005. but we must still verify the address is invariant. */
  1006. break;
  1007. case ASM_OPERANDS:
  1008. /* Don't mess with insns declared volatile. */
  1009. if (x->volatil)
  1010. return 0;
  1011. }
  1012. fmt = GET_RTX_FORMAT (code);
  1013. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1014. {
  1015. if (fmt[i] == 'e')
  1016. {
  1017. int tem = invariant_p (XEXP (x, i), n_times_set);
  1018. if (tem == 0)
  1019. return 0;
  1020. if (tem == 2)
  1021. conditional = 1;
  1022. }
  1023. else if (fmt[i] == 'E')
  1024. {
  1025. register int j;
  1026. for (j = 0; j < XVECLEN (x, i); j++)
  1027. {
  1028. int tem = invariant_p (XVECEXP (x, i, j), n_times_set);
  1029. if (tem == 0)
  1030. return 0;
  1031. if (tem == 2)
  1032. conditional = 1;
  1033. }
  1034. }
  1035. }
  1036. return 1 + conditional;
  1037. }
  1038. /* Return 1 if OTHER (a mem ref) overlaps the area of memory
  1039. which is SIZE bytes starting at BASE. */
  1040. int
  1041. addr_overlap_p (other, base, size)
  1042. rtx other;
  1043. rtx base;
  1044. int size;
  1045. {
  1046. int start = 0, end;
  1047. if (GET_CODE (base) == CONST)
  1048. base = XEXP (base, 0);
  1049. if (GET_CODE (base) == PLUS
  1050. && GET_CODE (XEXP (base, 1)) == CONST_INT)
  1051. {
  1052. start = INTVAL (XEXP (base, 1));
  1053. base = XEXP (base, 0);
  1054. }
  1055. end = start + size;
  1056. return refers_to_mem_p (other, base, start, end);
  1057. }
  1058. /* Return 1 if all the insns in the loop that set REG
  1059. are INSN and the immediately following insns,
  1060. and if each of those insns sets REG in an invariant way
  1061. according to TABLE (not counting uses of REG in them).
  1062. We assume that INSN itself is the first set of REG
  1063. and that its source is invariant. */
  1064. static int
  1065. consec_sets_invariant_p (reg, insn, table)
  1066. rtx reg, insn;
  1067. short *table;
  1068. {
  1069. register rtx p = insn;
  1070. register int regno = REGNO (reg);
  1071. /* Number of sets we have to insist on finding after INSN. */
  1072. int count = table[regno] - 1;
  1073. int old = table[regno];
  1074. table[regno] = 0;
  1075. while (count > 0)
  1076. {
  1077. register enum rtx_code code;
  1078. p = NEXT_INSN (p);
  1079. code = GET_CODE (p);
  1080. if (code == INSN && GET_CODE (PATTERN (p)) == SET
  1081. && GET_CODE (SET_DEST (PATTERN (p))) == REG
  1082. && REGNO (SET_DEST (PATTERN (p))) == regno
  1083. && invariant_p (SET_SRC (PATTERN (p)), table))
  1084. count--;
  1085. else if (code != NOTE)
  1086. {
  1087. table[regno] = old;
  1088. return 0;
  1089. }
  1090. }
  1091. table[regno] = old;
  1092. return 1;
  1093. }
  1094. #if 0
  1095. /* I don't think this condition is sufficient to allow INSN
  1096. to be moved, so we no longer test it. */
  1097. /* Return 1 if all insns in the basic block of INSN and following INSN
  1098. that set REG are invariant according to TABLE. */
  1099. static int
  1100. all_sets_invariant_p (reg, insn, table)
  1101. rtx reg, insn;
  1102. short *table;
  1103. {
  1104. register rtx p = insn;
  1105. register int regno = REGNO (reg);
  1106. while (1)
  1107. {
  1108. register enum rtx_code code;
  1109. p = NEXT_INSN (p);
  1110. code = GET_CODE (p);
  1111. if (code == CODE_LABEL || code == JUMP_INSN)
  1112. return 1;
  1113. if (code == INSN && GET_CODE (PATTERN (p)) == SET
  1114. && GET_CODE (SET_DEST (PATTERN (p))) == REG
  1115. && REGNO (SET_DEST (PATTERN (p))) == regno)
  1116. {
  1117. if (!invariant_p (SET_SRC (PATTERN (p)), table))
  1118. return 0;
  1119. }
  1120. }
  1121. }
  1122. #endif /* 0 */
  1123. /* Increment N_TIMES_SET at the index of each register
  1124. that is modified by an insn between FROM and TO.
  1125. If the value of an element of N_TIMES_SET becomes 2 or more,
  1126. do not keep incrementing it; all values >= 2 would be
  1127. equivalent anyway, and this way we avoid danger of overflow.
  1128. Store in *COUNT_PTR the number of actual instruction
  1129. in the loop. We use this to decide what is worth moving out. */
  1130. /* last_set[n] is nonzero iff reg n has been set in the current basic block.
  1131. In that case, it is the insn that last set reg n. */
  1132. static void
  1133. count_loop_regs_set (from, to, n_times_set, may_not_move, count_ptr, nregs)
  1134. register rtx from, to;
  1135. short *n_times_set;
  1136. char *may_not_move;
  1137. int *count_ptr;
  1138. int nregs;
  1139. {
  1140. register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
  1141. register rtx insn;
  1142. register int count = 0;
  1143. register rtx dest;
  1144. bzero (last_set, nregs * sizeof (rtx));
  1145. for (insn = from; insn != to; insn = NEXT_INSN (insn))
  1146. {
  1147. if (GET_CODE (insn) == CALL_INSN)
  1148. {
  1149. /* If a register is used as a subroutine address,
  1150. don't allow this register's setting to be moved out of the loop.
  1151. This condition is not at all logically correct
  1152. but it averts a very common lossage pattern
  1153. and creates lossage much less often. */
  1154. if (GET_CODE (PATTERN (insn)) == CALL
  1155. && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM
  1156. && GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == REG)
  1157. {
  1158. register int regno
  1159. = REGNO (XEXP (XEXP (PATTERN (insn), 0), 0));
  1160. may_not_move[regno] = 1;
  1161. }
  1162. else if (GET_CODE (PATTERN (insn)) == SET
  1163. && GET_CODE (SET_SRC (PATTERN (insn))) == CALL
  1164. && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0)) == MEM
  1165. && GET_CODE (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0)) == REG)
  1166. {
  1167. register int regno
  1168. = REGNO (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0));
  1169. may_not_move[regno] = 1;
  1170. /* The call insn itself sets a reg, which cannot be moved. */
  1171. may_not_move[REGNO (SET_DEST (PATTERN (insn)))] = 1;
  1172. n_times_set[REGNO (SET_DEST (PATTERN (insn)))]++;
  1173. }
  1174. }
  1175. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  1176. {
  1177. ++count;
  1178. if (GET_CODE (PATTERN (insn)) == CLOBBER
  1179. && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
  1180. /* Don't move a reg that has an explicit clobber.
  1181. We might do so sometimes, but it's not worth the pain. */
  1182. may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
  1183. else if (GET_CODE (PATTERN (insn)) == SET)
  1184. {
  1185. dest = SET_DEST (PATTERN (insn));
  1186. while (GET_CODE (dest) == SUBREG
  1187. || GET_CODE (dest) == ZERO_EXTRACT
  1188. || GET_CODE (dest) == SIGN_EXTRACT
  1189. || GET_CODE (dest) == STRICT_LOW_PART)
  1190. dest = XEXP (dest, 0);
  1191. if (GET_CODE (dest) == REG)
  1192. {
  1193. register int regno = REGNO (dest);
  1194. /* If this is the first setting of this reg
  1195. in current basic block, and it was set before,
  1196. it must be set in two basic blocks, so it cannot
  1197. be moved out of the loop. */
  1198. if (n_times_set[regno] > 0 && last_set[regno] == 0)
  1199. may_not_move[regno] = 1;
  1200. /* If this is not first setting in current basic block,
  1201. see if reg was used in between previous one and this.
  1202. If so, neither one can be moved. */
  1203. if (last_set[regno] != 0
  1204. && reg_used_between_p (dest, last_set[regno], insn))
  1205. may_not_move[regno] = 1;
  1206. ++n_times_set[regno];
  1207. last_set[regno] = insn;
  1208. }
  1209. }
  1210. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1211. {
  1212. register int i;
  1213. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  1214. {
  1215. register rtx x = XVECEXP (PATTERN (insn), 0, i);
  1216. if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
  1217. /* Don't move a reg that has an explicit clobber.
  1218. It's not worth the pain to try to do it correctly. */
  1219. may_not_move[REGNO (XEXP (x, 0))] = 1;
  1220. if (GET_CODE (x) == SET)
  1221. {
  1222. dest = SET_DEST (x);
  1223. while (GET_CODE (dest) == SUBREG
  1224. || GET_CODE (dest) == ZERO_EXTRACT
  1225. || GET_CODE (dest) == SIGN_EXTRACT
  1226. || GET_CODE (dest) == STRICT_LOW_PART)
  1227. dest = XEXP (dest, 0);
  1228. if (GET_CODE (dest) == REG)
  1229. {
  1230. register int regno = REGNO (dest);
  1231. ++n_times_set[regno];
  1232. may_not_move[regno] = 1;
  1233. last_set[regno] = insn;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
  1240. bzero (last_set, nregs * sizeof (rtx));
  1241. }
  1242. *count_ptr = count;
  1243. }
  1244. /* Given a loop that is bounded by LOOP_START and LOOP_END
  1245. and that is entered at SCAN_START,
  1246. return 1 if the register set by insn INSN is used by
  1247. any insn that precedes INSN in cyclic order starting
  1248. from the loop entry point. */
  1249. static int
  1250. loop_reg_used_before_p (insn, loop_start, scan_start, loop_end)
  1251. rtx insn, loop_start, scan_start, loop_end;
  1252. {
  1253. rtx reg = SET_DEST (PATTERN (insn));
  1254. if (INSN_LUID (scan_start) > INSN_LUID (insn))
  1255. return (reg_used_between_p (reg, scan_start, loop_end)
  1256. || reg_used_between_p (reg, loop_start, insn));
  1257. else
  1258. return reg_used_between_p (reg, scan_start, insn);
  1259. }
  1260. /* Return nonzero if X is a division that might trap. */
  1261. static int
  1262. may_trap_p (x)
  1263. rtx x;
  1264. {
  1265. switch (GET_CODE (x))
  1266. {
  1267. case DIV:
  1268. case MOD:
  1269. case UDIV:
  1270. case UMOD:
  1271. if (! CONSTANT_P (XEXP (x, 1))
  1272. && GET_CODE (XEXP (x, 1)) != CONST_DOUBLE)
  1273. return 1;
  1274. }
  1275. return 0;
  1276. }