loop.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /* Move constant computations out of loops.
  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 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. /* Vector mapping INSN_UIDs to luids.
  35. The luids are like uids but increase monononically always.
  36. We use them to see whether a jump comes from outside a given loop. */
  37. static short *uid_luid;
  38. /* Get the luid of an insn. */
  39. #define INSN_LUID(INSN) (uid_luid[INSN_UID (INSN)])
  40. /* 1 + largest uid of any insn. */
  41. static int max_uid;
  42. /* 1 + luid of last insn. */
  43. static int max_luid;
  44. /* Nonzero if somewhere in the current loop
  45. there is either a subroutine call,
  46. or a store into a memory address that is not fixed,
  47. or a store in a BLKmode memory operand,
  48. or too many different fixed addresses stored in
  49. to record them all in `loop_store_addrs'.
  50. In any of these cases, no memory location can be regarded
  51. as invariant. */
  52. static int unknown_address_altered;
  53. /* Nonzero if somewhere in the current loop there is a store
  54. into a memory address that is not fixed but is known to be
  55. part of an aggregate.
  56. In this case, no memory reference in an aggregate may be
  57. considered invariant. */
  58. static int unknown_aggregate_altered;
  59. /* Nonzero if somewhere in the current loop there is a store
  60. into a memory address other than a fixed address not in an aggregate.
  61. In this case, no memory reference in an aggregate at a varying address
  62. may be considered invariant. */
  63. static int fixed_aggregate_altered;
  64. /* Nonzero if there is a subroutine call in the current loop.
  65. (unknown_address_altered is also nonzero in this case.) */
  66. static int loop_has_call;
  67. /* Array of fixed memory addresses that are stored in this loop.
  68. If there are too many to fit here,
  69. we just turn on unknown_address_altered. */
  70. #define NUM_STORES 10
  71. static rtx loop_store_addrs[NUM_STORES];
  72. static int loop_store_widths[NUM_STORES];
  73. /* Index of first available slot in above array. */
  74. static int loop_store_addrs_idx;
  75. /* During the analysis of a loop, a chain of `struct movable's
  76. is made to record all the movable insns found.
  77. Then the entire chain can be scanned to decide which to move. */
  78. struct movable
  79. {
  80. rtx insn; /* A movable insn */
  81. int regno; /* The register it sets */
  82. short lifetime; /* lifetime of that register;
  83. may be adjusted when matching movables
  84. that load the same value are found. */
  85. unsigned int cond : 1; /* 1 if only conditionally movable */
  86. unsigned int force : 1; /* 1 means MUST move this insn */
  87. unsigned int global : 1; /* 1 means reg is live outside this loop */
  88. unsigned int dont : 1; /* 1 inhibits further processing of this */
  89. struct movable *match; /* First entry for same value */
  90. struct movable *forces; /* An insn that must be moved if this is */
  91. struct movable *next;
  92. };
  93. static rtx verify_loop ();
  94. static int invariant_p ();
  95. static int can_jump_into_range_p ();
  96. static void count_loop_regs_set ();
  97. static void note_addr_stored ();
  98. static int loop_reg_used_before_p ();
  99. static void constant_high_bytes ();
  100. static void scan_loop ();
  101. static rtx replace_regs ();
  102. /* Entry point of this file. Perform loop optimization
  103. on the current function. F is the first insn of the function
  104. and NREGS is the number of register numbers used. */
  105. int
  106. loop_optimize (f, nregs)
  107. /* f is the first instruction of a chain of insns for one function */
  108. rtx f;
  109. /* nregs is the total number of registers used in it */
  110. int nregs;
  111. {
  112. register rtx insn;
  113. register int i;
  114. rtx end;
  115. rtx last_insn;
  116. init_recog ();
  117. /* First find the last real insn, and count the number of insns,
  118. and assign insns their suids. */
  119. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  120. if (INSN_UID (insn) > i)
  121. i = INSN_UID (insn);
  122. max_uid = i + 1;
  123. uid_luid = (short *) alloca ((i + 1) * sizeof (short));
  124. bzero (uid_luid, (i + 1) * sizeof (short));
  125. /* Compute the mapping from uids to luids.
  126. LUIDs are numbers assigned to insns, like uids,
  127. except that luids increase monotonically through the code. */
  128. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  129. {
  130. last_insn = insn;
  131. INSN_LUID (insn) = ++i;
  132. }
  133. max_luid = i;
  134. /* Don't leave gaps in uid_luid for insns that have been
  135. deleted. It is possible that the first or last insn
  136. using some register has been deleted by cross-jumping.
  137. Make sure that uid_luid for that former insn's uid
  138. points to the general area where that insn used to be. */
  139. for (i = 0; i < max_uid; i++)
  140. if (uid_luid[i] == 0)
  141. uid_luid[i] = uid_luid[i - 1];
  142. /* Find and process each loop.
  143. We scan from the end, and process each loop when its start is seen,
  144. so we process innermost loops first. */
  145. for (insn = last_insn; insn; insn = PREV_INSN (insn))
  146. if (GET_CODE (insn) == NOTE
  147. && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  148. /* Make sure it really is a loop -- no jumps in from outside. */
  149. && (end = verify_loop (f, insn)))
  150. scan_loop (insn, end, nregs);
  151. }
  152. /* Optimize one loop whose start is LOOP_START and end is END.
  153. LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
  154. NOTE_INSN_LOOP_END. */
  155. static void
  156. scan_loop (loop_start, end, nregs)
  157. rtx loop_start, end;
  158. int nregs;
  159. {
  160. register int i;
  161. register rtx p = NEXT_INSN (loop_start);
  162. /* 1 if we are scanning insns that could be executed zero times. */
  163. int maybe_never = 0;
  164. /* For a rotated loop that is entered near the bottom,
  165. this is the label at the top. Otherwise it is zero. */
  166. rtx loop_top = 0;
  167. /* Jump insn that enters the loop, or 0 if control drops in. */
  168. rtx loop_entry_jump = 0;
  169. /* Place in the loop where control enters. */
  170. rtx scan_start;
  171. /* Number of insns in the loop. */
  172. int insn_count;
  173. int tem;
  174. /* Indexed by register number, contains the number of times the reg
  175. is set during the loop being scanned, or -1 if the insns that set it
  176. have all been scanned as candidates for being moved out of the loop.
  177. 0 indicates an invariant register; -1 a conditionally invariant one. */
  178. short *n_times_set;
  179. /* Indexed by register number, contains the number of times the reg
  180. was set during the loop being scanned, not counting changes due
  181. to moving these insns out of the loop. */
  182. short *n_times_used;
  183. /* Indexed by register number, contains 1 for a register whose
  184. assignments may not be moved out of the loop. */
  185. char *may_not_move;
  186. /* Chain describing insns movable in current loop. */
  187. struct movable *movables = 0;
  188. /* Last element in `movables' -- so we can add elements at the end. */
  189. struct movable *last_movable = 0;
  190. /* Ratio of extra register life span we can justify
  191. for saving an instruction. More if loop doesn't call subroutines
  192. since in that case saving an insn makes more difference
  193. and more registers are available. */
  194. int threshold = loop_has_call ? 15 : 30;
  195. n_times_set = (short *) alloca (nregs * sizeof (short));
  196. n_times_used = (short *) alloca (nregs * sizeof (short));
  197. may_not_move = (char *) alloca (nregs);
  198. /* Determine whether this loop starts with a jump down
  199. to a test at the end. */
  200. while (p != end
  201. && GET_CODE (p) != CODE_LABEL && GET_CODE (p) != JUMP_INSN)
  202. p = NEXT_INSN (p);
  203. /* "Loop" contains neither jumps nor labels;
  204. it must have been a dummy. Think no more about it. */
  205. if (p == end)
  206. return;
  207. /* If loop has a jump before the first label,
  208. the true entry is the target of that jump.
  209. Start scan from there.
  210. But record in LOOP_TOP the place where the end-test jumps
  211. back to so we can scan that after the end of the loop. */
  212. if (GET_CODE (p) == JUMP_INSN)
  213. {
  214. loop_entry_jump = p;
  215. loop_top = NEXT_INSN (p);
  216. /* Loop entry will never be a conditional jump.
  217. If we see one, this must not be a real loop. */
  218. if (GET_CODE (loop_top) != BARRIER)
  219. return;
  220. while (GET_CODE (loop_top) != CODE_LABEL)
  221. loop_top = NEXT_INSN (loop_top);
  222. p = JUMP_LABEL (p);
  223. /* Check to see whether the jump actually
  224. jumps out of the loop (meaning it's no loop).
  225. This case can happen for things like
  226. do {..} while (0). */
  227. if (INSN_LUID (p) < INSN_LUID (loop_start)
  228. || INSN_LUID (p) >= INSN_LUID (end))
  229. return;
  230. }
  231. /* Count number of times each reg is set during this loop.
  232. Set MAY_NOT_MOVE[I] if it is not safe to move out
  233. the setting of register I. */
  234. bzero (n_times_set, nregs * sizeof (short));
  235. bzero (may_not_move, nregs);
  236. count_loop_regs_set (loop_start, end, n_times_set, may_not_move,
  237. &insn_count, nregs);
  238. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  239. may_not_move[i] = 1, n_times_set[i] = 1;
  240. bcopy (n_times_set, n_times_used, nregs * sizeof (short));
  241. /* Scan through the loop finding insns that are safe to move.
  242. In each such insn, store QImode as the mode, to mark it.
  243. Then set n_times_set to -1 for the reg being set, so that
  244. this reg will be considered invariant for subsequent insns.
  245. We consider whether subsequent insns use the reg
  246. in deciding whether it is worth actually moving.
  247. MAYBE_NEVER is nonzero if we have passed a conditional jump insn
  248. and therefore it is possible that the insns we are scanning
  249. would never be executed. At such times, we must make sure
  250. that it is safe to execute the insn once instead of zero times.
  251. When MAYBE_NEVER is 0, all insns will be executed at least once
  252. so that is not a problem. */
  253. scan_start = p;
  254. while (1)
  255. {
  256. p = NEXT_INSN (p);
  257. /* At end of a straight-in loop, we are done.
  258. At end of a loop entered at the bottom, scan the top. */
  259. if (p == scan_start)
  260. break;
  261. if (p == end)
  262. {
  263. if (loop_top != 0)
  264. p = NEXT_INSN (loop_top);
  265. else
  266. break;
  267. if (p == scan_start)
  268. break;
  269. }
  270. if (GET_CODE (p) == INSN
  271. && GET_CODE (PATTERN (p)) == SET
  272. && GET_CODE (SET_DEST (PATTERN (p))) == REG
  273. && ! may_not_move[REGNO (SET_DEST (PATTERN (p)))])
  274. {
  275. /* If this register is used or set outside the loop,
  276. then we can move it only if we know this insn is
  277. executed exactly once per iteration,
  278. and we can check all the insns executed before it
  279. to make sure none of them used the value that
  280. was lying around at entry to the loop. */
  281. if ((uid_luid[regno_last_uid[REGNO (SET_DEST (PATTERN (p)))]] > INSN_LUID (end)
  282. || uid_luid[regno_first_uid[REGNO (SET_DEST (PATTERN (p)))]] < INSN_LUID (loop_start))
  283. && (maybe_never
  284. || loop_reg_used_before_p (p, loop_start, scan_start, end)))
  285. ;
  286. else if ((tem = invariant_p (SET_SRC (PATTERN (p)), n_times_set))
  287. && (n_times_set[REGNO (SET_DEST (PATTERN (p)))] == 1
  288. || all_sets_invariant_p (SET_DEST (PATTERN (p)),
  289. p, n_times_set)))
  290. {
  291. register struct movable *m;
  292. register int regno = REGNO (SET_DEST (PATTERN (p)));
  293. m = (struct movable *) alloca (sizeof (struct movable));
  294. m->next = 0;
  295. m->insn = p;
  296. m->force = 0;
  297. m->dont = 0;
  298. m->forces = 0;
  299. m->regno = regno;
  300. m->cond = (tem > 1);
  301. m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
  302. || uid_luid[regno_first_uid[regno]] < INSN_LUID (loop_start));
  303. m->match = 0;
  304. m->lifetime = (uid_luid[regno_last_uid[regno]]
  305. - uid_luid[regno_first_uid[regno]]);
  306. n_times_set[regno] = -1;
  307. /* Add M to the end of the chain MOVABLES. */
  308. if (movables == 0)
  309. movables = m;
  310. else
  311. last_movable->next = m;
  312. last_movable = m;
  313. }
  314. #ifdef SLOW_ZERO_EXTEND
  315. /* If this register is set only once, and by zero-extending,
  316. that means its high bytes are constant.
  317. So clear them outside the loop and within the loop
  318. just load the low bytes.
  319. We must check that the machine has an instruction to do so.
  320. Also, if the value loaded into the register
  321. depends on the same register, this cannot be done. */
  322. else if (GET_CODE (SET_SRC (PATTERN (p))) == ZERO_EXTEND
  323. && !reg_mentioned_p (SET_DEST (PATTERN (p)),
  324. SET_SRC (PATTERN (p))))
  325. {
  326. register int regno = REGNO (SET_DEST (PATTERN (p)));
  327. if ((threshold
  328. * (uid_luid[regno_last_uid[regno]]
  329. - uid_luid[regno_first_uid[regno]]))
  330. >= insn_count)
  331. constant_high_bytes (p, loop_start);
  332. }
  333. #endif /* SLOW_ZERO_EXTEND */
  334. }
  335. /* Past a label or a jump, we get to insns for which we
  336. can't count on whether or how many times they will be
  337. executed during each iteration. Therefore, we can
  338. only move out sets of trivial variables
  339. (those not used after the loop). */
  340. else if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
  341. maybe_never = 1;
  342. }
  343. /* For each movable insn, see if the reg that it loads
  344. leads when it dies right into another conditionally movable insn. */
  345. {
  346. register struct movable *m, *m1;
  347. for (m1 = movables; m1; m1 = m1->next)
  348. {
  349. int regno = m1->regno;
  350. for (m = m1->next; m; m = m->next)
  351. if (INSN_UID (m->insn) == regno_last_uid[regno])
  352. break;
  353. if (m != 0 && SET_SRC (PATTERN (m->insn)) == SET_DEST (PATTERN (m1->insn)))
  354. m = 0;
  355. m1->forces = m;
  356. }
  357. }
  358. /* See if there are multiple movable insns that load the same value.
  359. If there are, make all but the first point at the first one
  360. through the `match' field, and add the priorities of them
  361. all together as the priority of the first. */
  362. {
  363. register struct movable *m;
  364. rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
  365. char *matched_regs = (char *) alloca (nregs);
  366. bzero (reg_map, nregs * sizeof (rtx));
  367. for (m = movables; m; m = m->next)
  368. if (m->match == 0 && n_times_used[m->regno] == 1 && !m->global
  369. && (! movables->cond
  370. || 1 == invariant_p (SET_SRC (PATTERN (movables->insn)), n_times_set)))
  371. {
  372. register struct movable *m1;
  373. int matched = 0;
  374. int lifetime = m->lifetime;
  375. int times_used = n_times_used[m->regno];
  376. if (m->forces != 0) times_used++;
  377. bzero (matched_regs, nregs);
  378. matched_regs[m->regno] = 1;
  379. for (m1 = m->next; m1; m1 = m1->next)
  380. {
  381. if (m1->match == 0 && n_times_used[m1->regno] == 1
  382. && !m1->global
  383. /* Perform already-scheduled replacements
  384. on M1's insn before comparing them! */
  385. && (replace_regs (PATTERN (m1->insn), reg_map),
  386. ((GET_CODE (SET_SRC (PATTERN (m1->insn))) == REG
  387. && matched_regs[REGNO (SET_SRC (PATTERN (m1->insn)))])
  388. || rtx_equal_p (SET_SRC (PATTERN (m->insn)),
  389. SET_SRC (PATTERN (m1->insn))))))
  390. {
  391. lifetime += m1->lifetime;
  392. times_used += n_times_used[m1->regno];
  393. if (m1->forces != 0) times_used++;
  394. m1->match = m;
  395. matched_regs[m1->regno] = 1;
  396. matched = 1;
  397. }
  398. }
  399. /* If the movable insn M has others that match it
  400. and all together they merit being moved,
  401. go through the others now and replace their registers
  402. with M's register. Mark the others with the `dont' field
  403. and do all processing on them now. */
  404. if (matched
  405. && ((threshold * times_used * lifetime) >= insn_count
  406. || m->force
  407. || n_times_set[m->regno] == 0))
  408. {
  409. m->force = 1;
  410. m->lifetime = lifetime;
  411. for (m1 = m->next; m1; m1 = m1->next)
  412. if (m1->match == m)
  413. {
  414. /* Schedule the reg loaded by M1
  415. for replacement so that shares the reg of M. */
  416. reg_map[m1->regno] = SET_DEST (PATTERN (m->insn));
  417. /* Get rid of the replaced reg
  418. and prevent further processing of it. */
  419. m1->dont = 1;
  420. delete_insn (m1->insn);
  421. }
  422. }
  423. }
  424. /* Go through all the instructions in the loop, making
  425. all the register substitutions scheduled above. */
  426. for (p = loop_start; p != end; p = NEXT_INSN (p))
  427. if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
  428. || GET_CODE (p) == CALL_INSN)
  429. replace_regs (PATTERN (p), reg_map);
  430. }
  431. /* Now consider each movable insn to decide whether it is worth moving. */
  432. {
  433. register struct movable *m;
  434. for (m = movables; m; m = m->next)
  435. if (!m->dont
  436. && (! m->cond
  437. || 1 == invariant_p (SET_SRC (PATTERN (m->insn)), n_times_set)))
  438. /* We have an insn that is safe to move. */
  439. {
  440. register int regno;
  441. p = m->insn;
  442. regno = m->regno;
  443. if (m->forces != 0)
  444. n_times_used[regno]++;
  445. /* Don't move something out of the loop
  446. if that would cause it to be live over a range
  447. THRESHOLD times as long. That means the loop is so big
  448. that moving won't speed things up much,
  449. and it is liable to make register usage worse. */
  450. if (m->force
  451. || (threshold * n_times_used[regno] * m->lifetime) >= insn_count
  452. || n_times_set[regno] == 0)
  453. {
  454. rtx i1 = emit_insn_before (PATTERN (p), loop_start);
  455. if (CONSTANT_ADDRESS_P (SET_SRC (PATTERN (p))))
  456. REG_NOTES (i1)
  457. = gen_rtx (EXPR_LIST, REG_CONST,
  458. SET_DEST (PATTERN (p)), REG_NOTES (i1));
  459. delete_insn (p);
  460. n_times_set[regno] = 0;
  461. /* Signal any other movables that match this one
  462. that they should be moved too. */
  463. m->force = 1;
  464. /* Signal any conditionally movable computation
  465. that uses this one that it should be moved too. */
  466. if (m->forces != 0)
  467. m->forces->force = 1;
  468. }
  469. }
  470. }
  471. /* Now maybe duplicate the end-test before the loop. */
  472. if (loop_entry_jump != 0)
  473. {
  474. rtx endtestjump;
  475. p = scan_start;
  476. while (GET_CODE (p) != INSN && GET_CODE (p) != JUMP_INSN
  477. && GET_CODE (p) != CALL_INSN)
  478. p = NEXT_INSN (p);
  479. endtestjump = next_real_insn (p);
  480. /* Check that we (1) enter at a compare insn and (2)
  481. the insn (presumably a jump) following that compare
  482. is the last in the loop and jumps back to the loop beginning. */
  483. if (GET_CODE (PATTERN (p)) == SET
  484. && SET_DEST (PATTERN (p)) == cc0_rtx
  485. && endtestjump == prev_real_insn (end)
  486. && prev_real_insn (JUMP_LABEL (endtestjump)) == loop_entry_jump)
  487. {
  488. rtx newlab;
  489. /* Ok, duplicate that test before loop entry. */
  490. emit_insn_before (copy_rtx (PATTERN (p)), loop_entry_jump);
  491. /* Make a new entry-jump (before the original)
  492. that uses the opposite condition and jumps in
  493. after this conitional jump. */
  494. newlab = gen_label_rtx ();
  495. emit_label_after (newlab, endtestjump);
  496. emit_jump_insn_before (copy_rtx (PATTERN (endtestjump)), loop_entry_jump);
  497. JUMP_LABEL (PREV_INSN (loop_entry_jump)) = JUMP_LABEL (endtestjump);
  498. LABEL_NUSES (JUMP_LABEL (endtestjump))++;
  499. invert_jump (PREV_INSN (loop_entry_jump), newlab);
  500. /* Delete the original entry-jump. */
  501. delete_insn (loop_entry_jump);
  502. }
  503. }
  504. }
  505. /* Throughout the rtx X, replace many registers according to REG_MAP.
  506. Return the replacement for X (which may be X with altered contents).
  507. REG_MAP[R] is the replacement for register R, or 0 for don't replace. */
  508. static rtx
  509. replace_regs (x, reg_map)
  510. rtx x;
  511. rtx *reg_map;
  512. {
  513. register RTX_CODE code = GET_CODE (x);
  514. register int i;
  515. register char *fmt;
  516. switch (code)
  517. {
  518. case PC:
  519. case CC0:
  520. case CONST_INT:
  521. case CONST_DOUBLE:
  522. case CONST:
  523. case SYMBOL_REF:
  524. case LABEL_REF:
  525. return x;
  526. case REG:
  527. if (reg_map[REGNO (x)] != 0)
  528. return reg_map[REGNO (x)];
  529. return x;
  530. }
  531. fmt = GET_RTX_FORMAT (code);
  532. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  533. {
  534. if (fmt[i] == 'e')
  535. XEXP (x, i) = replace_regs (XEXP (x, i), reg_map);
  536. if (fmt[i] == 'E')
  537. {
  538. register int j;
  539. for (j = 0; j < XVECLEN (x, i); j++)
  540. XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map);
  541. }
  542. }
  543. return x;
  544. }
  545. /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
  546. Replace it with an instruction to load just the low bytes
  547. if the machine supports such an instruction,
  548. and insert above LOOP_START an instruction to clear the register. */
  549. static void
  550. constant_high_bytes (p, loop_start)
  551. rtx p, loop_start;
  552. {
  553. register rtx new;
  554. register int insn_code_number;
  555. /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
  556. to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
  557. new = gen_rtx (SET, VOIDmode,
  558. gen_rtx (STRICT_LOW_PART, VOIDmode,
  559. gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
  560. SET_DEST (PATTERN (p)),
  561. 0)),
  562. XEXP (SET_SRC (PATTERN (p)), 0));
  563. insn_code_number = recog (new, p);
  564. if (insn_code_number)
  565. {
  566. register int i;
  567. /* Clear destination register before the loop. */
  568. emit_insn_before (gen_rtx (SET, VOIDmode,
  569. SET_DEST (PATTERN (p)),
  570. const0_rtx),
  571. loop_start);
  572. /* Inside the loop, just load the low part. */
  573. PATTERN (p) = new;
  574. }
  575. }
  576. /* Verify that the ostensible loop starting at START
  577. really is a loop: nothing jumps into it from outside.
  578. Return the marker for the end of the loop, or zero if not a real loop.
  579. Also set the variables `unknown_*_altered' and `loop_has_call',
  580. and fill in the array `loop_store_addrs'. */
  581. static rtx
  582. verify_loop (f, start)
  583. rtx f, start;
  584. {
  585. register int level = 1;
  586. register rtx insn, end;
  587. /* First find the LOOP_END that matches.
  588. Also check each insn for storing in memory and record where. */
  589. unknown_address_altered = 0;
  590. unknown_aggregate_altered = 0;
  591. fixed_aggregate_altered = 0;
  592. loop_has_call = 0;
  593. loop_store_addrs_idx = 0;
  594. for (insn = NEXT_INSN (start); level > 0; insn = NEXT_INSN (insn))
  595. {
  596. if (insn == 0)
  597. abort ();
  598. if (GET_CODE (insn) == NOTE)
  599. {
  600. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  601. ++level;
  602. else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  603. --level;
  604. }
  605. else if (GET_CODE (insn) == CALL_INSN)
  606. {
  607. unknown_address_altered = 1;
  608. loop_has_call = 1;
  609. }
  610. else if (! unknown_address_altered)
  611. {
  612. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  613. note_stores (PATTERN (insn), note_addr_stored);
  614. }
  615. }
  616. end = insn;
  617. /* Now scan all jumps in the function and see if any of them can
  618. reach a label within the range of the loop. */
  619. for (insn = f; insn; insn = NEXT_INSN (insn))
  620. if (GET_CODE (insn) == JUMP_INSN
  621. /* Don't get fooled by jumps inserted by loop-optimize.
  622. They don't have valid LUIDs, and they never jump into loops. */
  623. && INSN_UID (insn) < max_uid
  624. && (INSN_LUID (insn) < INSN_LUID (start)
  625. || INSN_LUID (insn) > INSN_LUID (end))
  626. /* We have a jump that is outside the loop.
  627. Does it jump into the loop? */
  628. && can_jump_into_range_p (PATTERN (insn),
  629. INSN_LUID (start), INSN_LUID (end)))
  630. return 0;
  631. #if 0
  632. /* Now scan all labels between them and check for any jumps from outside.
  633. This uses the ref-chains set up by find_basic_blocks.
  634. This code is not used because it's more convenient for other reasons
  635. to do the loop optimization before find_basic_blocks. */
  636. for (insn = start; insn != end; insn = NEXT_INSN (insn))
  637. if (GET_CODE (insn) == CODE_LABEL)
  638. {
  639. register rtx y;
  640. for (y = LABEL_REFS (insn); y != insn; y = LABEL_NEXTREF (y))
  641. if (INSN_LUID (CONTAINING_INSN (y)) < INSN_LUID (start)
  642. || INSN_LUID (CONTAINING_INSN (y)) > INSN_LUID (end))
  643. return 0;
  644. }
  645. #endif
  646. return end;
  647. }
  648. /* Return 1 if somewhere in X is a LABEL_REF to a label
  649. located between BEG and END. */
  650. static int
  651. can_jump_into_range_p (x, beg, end)
  652. rtx x;
  653. int beg, end;
  654. {
  655. register RTX_CODE code = GET_CODE (x);
  656. register int i;
  657. register char *fmt;
  658. if (code == LABEL_REF)
  659. {
  660. register int luid = INSN_LUID (XEXP (x, 0));
  661. return luid > beg && luid < end;
  662. }
  663. fmt = GET_RTX_FORMAT (code);
  664. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  665. {
  666. if (fmt[i] == 'e')
  667. {
  668. if (can_jump_into_range_p (XEXP (x, i), beg, end))
  669. return 1;
  670. }
  671. else if (fmt[i] == 'E')
  672. {
  673. register int j;
  674. for (j = 0; j < XVECLEN (x, i); j++)
  675. if (can_jump_into_range_p (XVECEXP (x, i, j), beg, end))
  676. return 1;
  677. }
  678. }
  679. return 0;
  680. }
  681. /* Record that a memory reference X is being set. */
  682. static void
  683. note_addr_stored (x)
  684. rtx x;
  685. {
  686. rtx addr;
  687. if (x == 0 || GET_CODE (x) != MEM)
  688. return;
  689. if (rtx_addr_varies_p (x))
  690. {
  691. if (GET_CODE (XEXP (x, 0)) == PLUS)
  692. unknown_aggregate_altered = 1;
  693. else
  694. unknown_address_altered = 1;
  695. }
  696. else if (GET_MODE (x) == BLKmode)
  697. unknown_address_altered = 1;
  698. else
  699. {
  700. register int i;
  701. register rtx addr = XEXP (x, 0);
  702. if (x->in_struct)
  703. fixed_aggregate_altered = 1;
  704. for (i = 0; i < loop_store_addrs_idx; i++)
  705. if (rtx_equal_p (loop_store_addrs[i], addr))
  706. {
  707. if (loop_store_widths[i] < GET_MODE_SIZE (GET_MODE (x)))
  708. loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
  709. break;
  710. }
  711. if (i == NUM_STORES)
  712. unknown_address_altered = 1;
  713. else if (i == loop_store_addrs_idx)
  714. {
  715. loop_store_widths[i] == GET_MODE_SIZE (GET_MODE (x));
  716. loop_store_addrs[loop_store_addrs_idx++] = addr;
  717. }
  718. }
  719. }
  720. /* Return nonzero if the rtx X is invariant over the current loop.
  721. N_TIMES_SET is a vector whose element I is nonzero if register I
  722. is set during the loop.
  723. The value is 2 if we refer to something only conditionally invariant.
  724. If `unknown_address_altered' is nonzero, no memory ref is invariant.
  725. Otherwise if `unknown_aggregate_altered' is nonzero,
  726. a memory ref is invariant if it is not part of an aggregate
  727. and its address is fixed and not in `loop_store_addrs'.
  728. Otherwise if `fixed_aggregate_altered' is nonzero,
  729. a memory ref is invariant
  730. if its address is fixed and not in `loop_store_addrs'.
  731. Otherwise, a memory ref is invariant if its address is fixed and not in
  732. `loop_store_addrs' or if it is not an aggregate. */
  733. static int
  734. invariant_p (x, n_times_set)
  735. register rtx x;
  736. short *n_times_set;
  737. {
  738. register int i;
  739. register RTX_CODE code = GET_CODE (x);
  740. register char *fmt;
  741. int conditional = 0;
  742. switch (code)
  743. {
  744. case CONST_INT:
  745. case CONST_DOUBLE:
  746. case SYMBOL_REF:
  747. case LABEL_REF:
  748. case CONST:
  749. case UNCHANGING:
  750. return 1;
  751. case PC:
  752. case VOLATILE:
  753. case CC0:
  754. return 0;
  755. case REG:
  756. if (n_times_set[REGNO (x)] == -1)
  757. return 2;
  758. return n_times_set[REGNO (x)] == 0;
  759. case MEM:
  760. /* A store in a varying-address scalar (or a subroutine call)
  761. could clobber anything in memory. */
  762. if (unknown_address_altered)
  763. return 0;
  764. /* A store in a varying-address aggregate component
  765. could clobber anything except a scalar with a fixed address. */
  766. if (unknown_aggregate_altered
  767. && ((x->in_struct || GET_CODE (XEXP (x, 0)) == PLUS)
  768. || rtx_addr_varies_p (x)))
  769. return 0;
  770. /* A store in a fixed-address aggregate component
  771. could clobber anything whose address is not fixed,
  772. even an aggregate component. */
  773. if (fixed_aggregate_altered
  774. && rtx_addr_varies_p (x))
  775. return 0;
  776. /* Any store could clobber a varying-address scalar. */
  777. if (loop_store_addrs_idx
  778. && !(x->in_struct || GET_CODE (XEXP (x, 0)) == PLUS)
  779. && rtx_addr_varies_p (x))
  780. return 0;
  781. /* A store in a fixed address clobbers overlapping references. */
  782. for (i = loop_store_addrs_idx - 1; i >= 0; i--)
  783. if (addr_overlap_p (XEXP (x, 0), loop_store_addrs[i],
  784. loop_store_widths[i]))
  785. return 0;
  786. /* It's not invalidated by a store in memory
  787. but we must still verify the address is invariant. */
  788. }
  789. fmt = GET_RTX_FORMAT (code);
  790. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  791. {
  792. if (fmt[i] == 'E')
  793. abort ();
  794. if (fmt[i] == 'e')
  795. {
  796. int tem = invariant_p (XEXP (x, i), n_times_set);
  797. if (tem == 0)
  798. return 0;
  799. if (tem == 2)
  800. conditional = 1;
  801. }
  802. }
  803. return 1 + conditional;
  804. }
  805. /* Return 1 if OTHER (a mem ref) overlaps the area of memory
  806. which is SIZE bytes starting at BASE. */
  807. int
  808. addr_overlap_p (other, base, size)
  809. rtx other;
  810. rtx base;
  811. int size;
  812. {
  813. int start = 0, end;
  814. if (GET_CODE (base) == CONST)
  815. base = XEXP (base, 0);
  816. if (GET_CODE (base) == PLUS
  817. && GET_CODE (XEXP (base, 1)) == CONST_INT)
  818. {
  819. start = INTVAL (XEXP (base, 1));
  820. base = XEXP (base, 0);
  821. }
  822. end = start + size;
  823. return refers_to_mem_p (other, base, start, end);
  824. }
  825. /* Return 1 if all insns in the basic block of INSN and following INSN
  826. that set REG are invariant according to TABLE. */
  827. static int
  828. all_sets_invariant_p (reg, insn, table)
  829. rtx reg, insn;
  830. char *table;
  831. {
  832. register rtx p = insn;
  833. register int regno = REGNO (reg);
  834. while (1)
  835. {
  836. register enum rtx_code code;
  837. p = NEXT_INSN (p);
  838. code = GET_CODE (p);
  839. if (code == CODE_LABEL || code == JUMP_INSN)
  840. return 1;
  841. if (code == INSN && GET_CODE (PATTERN (p)) == SET
  842. && GET_CODE (SET_DEST (PATTERN (p))) == REG
  843. && REGNO (SET_DEST (PATTERN (p))) == regno)
  844. {
  845. if (!invariant_p (SET_SRC (PATTERN (p)), table))
  846. return 0;
  847. }
  848. }
  849. }
  850. /* Increment N_TIMES_SET at the index of each register
  851. that is modified by an insn between FROM and TO.
  852. If the value of an element of N_TIMES_SET becomes 2 or more,
  853. do not keep incrementing it; all values >= 2 would be
  854. equivalent anyway, and this way we avoid danger of overflow.
  855. Store in *COUNT_PTR the number of actual instruction
  856. in the loop. We use this to decide what is worth moving out. */
  857. /* last_set[n] is nonzero iff reg n has been set in the current basic block.
  858. In that case, it is the insn that last set reg n. */
  859. static void
  860. count_loop_regs_set (from, to, n_times_set, may_not_move, count_ptr, nregs)
  861. register rtx from, to;
  862. short *n_times_set;
  863. char *may_not_move;
  864. int *count_ptr;
  865. int nregs;
  866. {
  867. register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
  868. register rtx insn;
  869. register int count = 0;
  870. register rtx dest;
  871. bzero (last_set, nregs * sizeof (rtx));
  872. for (insn = from; insn != to; insn = NEXT_INSN (insn))
  873. {
  874. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  875. || GET_CODE (insn) == CALL_INSN)
  876. {
  877. ++count;
  878. if (GET_CODE (PATTERN (insn)) == SET)
  879. {
  880. dest = SET_DEST (PATTERN (insn));
  881. while (GET_CODE (dest) == SUBREG
  882. || GET_CODE (dest) == ZERO_EXTRACT
  883. || GET_CODE (dest) == SIGN_EXTRACT
  884. || GET_CODE (dest) == STRICT_LOW_PART)
  885. dest = XEXP (dest, 0);
  886. if (GET_CODE (dest) == REG)
  887. {
  888. register int regno = REGNO (dest);
  889. /* If this is the first setting of this reg
  890. in current basic block, and it was set before,
  891. it must be set in two basic blocks, so it cannot
  892. be moved out of the loop. */
  893. if (n_times_set[regno] > 0 && last_set[regno] == 0)
  894. may_not_move[regno] = 1;
  895. /* If this is not first setting in current basic block,
  896. see if reg was used in between previous one and this.
  897. If so, neither one can be moved. */
  898. if (last_set[regno] != 0
  899. && reg_used_between_p (dest, last_set[regno], insn))
  900. may_not_move[regno] = 1;
  901. ++n_times_set[regno];
  902. last_set[regno] = insn;
  903. }
  904. }
  905. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  906. {
  907. register int i;
  908. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  909. {
  910. register rtx x = XVECEXP (PATTERN (insn), 0, i);
  911. if (GET_CODE (x) == SET)
  912. {
  913. dest = SET_DEST (x);
  914. while (GET_CODE (dest) == SUBREG
  915. || GET_CODE (dest) == ZERO_EXTRACT
  916. || GET_CODE (dest) == SIGN_EXTRACT
  917. || GET_CODE (dest) == STRICT_LOW_PART)
  918. dest = XEXP (dest, 0);
  919. if (GET_CODE (dest) == REG)
  920. {
  921. register int regno = REGNO (dest);
  922. ++n_times_set[regno];
  923. may_not_move[regno] = 1;
  924. last_set[regno] = insn;
  925. }
  926. }
  927. }
  928. }
  929. /* If a register is used as a subroutine address,
  930. don't allow this register's setting to be moved out of the loop.
  931. This condition is not at all logically correct
  932. but it averts a very common lossage pattern
  933. and creates lossage much less often. */
  934. else if (GET_CODE (PATTERN (insn)) == CALL
  935. && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM
  936. && GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == REG)
  937. {
  938. register int regno
  939. = REGNO (XEXP (XEXP (PATTERN (insn), 0), 0));
  940. may_not_move[regno] = 1;
  941. }
  942. }
  943. if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
  944. bzero (last_set, nregs * sizeof (rtx));
  945. }
  946. *count_ptr = count;
  947. }
  948. /* Given a loop that is bounded by LOOP_START and LOOP_END
  949. and that is entered at SCAN_START,
  950. return 1 if the register set by insn INSN is used by
  951. any insn that precedes INSN in cyclic order starting
  952. from the loop entry point. */
  953. static int
  954. loop_reg_used_before_p (insn, loop_start, scan_start, loop_end)
  955. rtx insn, loop_start, scan_start, loop_end;
  956. {
  957. rtx reg = SET_DEST (PATTERN (insn));
  958. if (INSN_LUID (scan_start) > INSN_LUID (insn))
  959. return (reg_used_between_p (reg, scan_start, loop_end)
  960. || reg_used_between_p (reg, loop_start, insn));
  961. else
  962. return reg_used_between_p (reg, scan_start, insn);
  963. }