cfgloop.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. /* Natural loop discovery code for GNU compiler.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "hashtab.h"
  21. #include "hash-set.h"
  22. #include "vec.h"
  23. #include "symtab.h"
  24. #include "inchash.h"
  25. #include "machmode.h"
  26. #include "hard-reg-set.h"
  27. #include "input.h"
  28. #include "function.h"
  29. #include "predict.h"
  30. #include "dominance.h"
  31. #include "cfg.h"
  32. #include "cfganal.h"
  33. #include "basic-block.h"
  34. #include "cfgloop.h"
  35. #include "diagnostic-core.h"
  36. #include "flags.h"
  37. #include "tree.h"
  38. #include "fold-const.h"
  39. #include "tree-ssa-alias.h"
  40. #include "internal-fn.h"
  41. #include "gimple-expr.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "gimple-iterator.h"
  45. #include "gimple-ssa.h"
  46. #include "dumpfile.h"
  47. static void flow_loops_cfg_dump (FILE *);
  48. /* Dump loop related CFG information. */
  49. static void
  50. flow_loops_cfg_dump (FILE *file)
  51. {
  52. basic_block bb;
  53. if (!file)
  54. return;
  55. FOR_EACH_BB_FN (bb, cfun)
  56. {
  57. edge succ;
  58. edge_iterator ei;
  59. fprintf (file, ";; %d succs { ", bb->index);
  60. FOR_EACH_EDGE (succ, ei, bb->succs)
  61. fprintf (file, "%d ", succ->dest->index);
  62. fprintf (file, "}\n");
  63. }
  64. }
  65. /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
  66. bool
  67. flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
  68. {
  69. unsigned odepth = loop_depth (outer);
  70. return (loop_depth (loop) > odepth
  71. && (*loop->superloops)[odepth] == outer);
  72. }
  73. /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
  74. loops within LOOP. */
  75. struct loop *
  76. superloop_at_depth (struct loop *loop, unsigned depth)
  77. {
  78. unsigned ldepth = loop_depth (loop);
  79. gcc_assert (depth <= ldepth);
  80. if (depth == ldepth)
  81. return loop;
  82. return (*loop->superloops)[depth];
  83. }
  84. /* Returns the list of the latch edges of LOOP. */
  85. static vec<edge>
  86. get_loop_latch_edges (const struct loop *loop)
  87. {
  88. edge_iterator ei;
  89. edge e;
  90. vec<edge> ret = vNULL;
  91. FOR_EACH_EDGE (e, ei, loop->header->preds)
  92. {
  93. if (dominated_by_p (CDI_DOMINATORS, e->src, loop->header))
  94. ret.safe_push (e);
  95. }
  96. return ret;
  97. }
  98. /* Dump the loop information specified by LOOP to the stream FILE
  99. using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
  100. void
  101. flow_loop_dump (const struct loop *loop, FILE *file,
  102. void (*loop_dump_aux) (const struct loop *, FILE *, int),
  103. int verbose)
  104. {
  105. basic_block *bbs;
  106. unsigned i;
  107. vec<edge> latches;
  108. edge e;
  109. if (! loop || ! loop->header)
  110. return;
  111. fprintf (file, ";;\n;; Loop %d\n", loop->num);
  112. fprintf (file, ";; header %d, ", loop->header->index);
  113. if (loop->latch)
  114. fprintf (file, "latch %d\n", loop->latch->index);
  115. else
  116. {
  117. fprintf (file, "multiple latches:");
  118. latches = get_loop_latch_edges (loop);
  119. FOR_EACH_VEC_ELT (latches, i, e)
  120. fprintf (file, " %d", e->src->index);
  121. latches.release ();
  122. fprintf (file, "\n");
  123. }
  124. fprintf (file, ";; depth %d, outer %ld\n",
  125. loop_depth (loop), (long) (loop_outer (loop)
  126. ? loop_outer (loop)->num : -1));
  127. fprintf (file, ";; nodes:");
  128. bbs = get_loop_body (loop);
  129. for (i = 0; i < loop->num_nodes; i++)
  130. fprintf (file, " %d", bbs[i]->index);
  131. free (bbs);
  132. fprintf (file, "\n");
  133. if (loop_dump_aux)
  134. loop_dump_aux (loop, file, verbose);
  135. }
  136. /* Dump the loop information about loops to the stream FILE,
  137. using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
  138. void
  139. flow_loops_dump (FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
  140. {
  141. struct loop *loop;
  142. if (!current_loops || ! file)
  143. return;
  144. fprintf (file, ";; %d loops found\n", number_of_loops (cfun));
  145. FOR_EACH_LOOP (loop, LI_INCLUDE_ROOT)
  146. {
  147. flow_loop_dump (loop, file, loop_dump_aux, verbose);
  148. }
  149. if (verbose)
  150. flow_loops_cfg_dump (file);
  151. }
  152. /* Free data allocated for LOOP. */
  153. void
  154. flow_loop_free (struct loop *loop)
  155. {
  156. struct loop_exit *exit, *next;
  157. vec_free (loop->superloops);
  158. /* Break the list of the loop exit records. They will be freed when the
  159. corresponding edge is rescanned or removed, and this avoids
  160. accessing the (already released) head of the list stored in the
  161. loop structure. */
  162. for (exit = loop->exits->next; exit != loop->exits; exit = next)
  163. {
  164. next = exit->next;
  165. exit->next = exit;
  166. exit->prev = exit;
  167. }
  168. ggc_free (loop->exits);
  169. ggc_free (loop);
  170. }
  171. /* Free all the memory allocated for LOOPS. */
  172. void
  173. flow_loops_free (struct loops *loops)
  174. {
  175. if (loops->larray)
  176. {
  177. unsigned i;
  178. loop_p loop;
  179. /* Free the loop descriptors. */
  180. FOR_EACH_VEC_SAFE_ELT (loops->larray, i, loop)
  181. {
  182. if (!loop)
  183. continue;
  184. flow_loop_free (loop);
  185. }
  186. vec_free (loops->larray);
  187. }
  188. }
  189. /* Find the nodes contained within the LOOP with header HEADER.
  190. Return the number of nodes within the loop. */
  191. int
  192. flow_loop_nodes_find (basic_block header, struct loop *loop)
  193. {
  194. vec<basic_block> stack = vNULL;
  195. int num_nodes = 1;
  196. edge latch;
  197. edge_iterator latch_ei;
  198. header->loop_father = loop;
  199. FOR_EACH_EDGE (latch, latch_ei, loop->header->preds)
  200. {
  201. if (latch->src->loop_father == loop
  202. || !dominated_by_p (CDI_DOMINATORS, latch->src, loop->header))
  203. continue;
  204. num_nodes++;
  205. stack.safe_push (latch->src);
  206. latch->src->loop_father = loop;
  207. while (!stack.is_empty ())
  208. {
  209. basic_block node;
  210. edge e;
  211. edge_iterator ei;
  212. node = stack.pop ();
  213. FOR_EACH_EDGE (e, ei, node->preds)
  214. {
  215. basic_block ancestor = e->src;
  216. if (ancestor->loop_father != loop)
  217. {
  218. ancestor->loop_father = loop;
  219. num_nodes++;
  220. stack.safe_push (ancestor);
  221. }
  222. }
  223. }
  224. }
  225. stack.release ();
  226. return num_nodes;
  227. }
  228. /* Records the vector of superloops of the loop LOOP, whose immediate
  229. superloop is FATHER. */
  230. static void
  231. establish_preds (struct loop *loop, struct loop *father)
  232. {
  233. loop_p ploop;
  234. unsigned depth = loop_depth (father) + 1;
  235. unsigned i;
  236. loop->superloops = 0;
  237. vec_alloc (loop->superloops, depth);
  238. FOR_EACH_VEC_SAFE_ELT (father->superloops, i, ploop)
  239. loop->superloops->quick_push (ploop);
  240. loop->superloops->quick_push (father);
  241. for (ploop = loop->inner; ploop; ploop = ploop->next)
  242. establish_preds (ploop, loop);
  243. }
  244. /* Add LOOP to the loop hierarchy tree where FATHER is father of the
  245. added loop. If LOOP has some children, take care of that their
  246. pred field will be initialized correctly. */
  247. void
  248. flow_loop_tree_node_add (struct loop *father, struct loop *loop)
  249. {
  250. loop->next = father->inner;
  251. father->inner = loop;
  252. establish_preds (loop, father);
  253. }
  254. /* Remove LOOP from the loop hierarchy tree. */
  255. void
  256. flow_loop_tree_node_remove (struct loop *loop)
  257. {
  258. struct loop *prev, *father;
  259. father = loop_outer (loop);
  260. /* Remove loop from the list of sons. */
  261. if (father->inner == loop)
  262. father->inner = loop->next;
  263. else
  264. {
  265. for (prev = father->inner; prev->next != loop; prev = prev->next)
  266. continue;
  267. prev->next = loop->next;
  268. }
  269. loop->superloops = NULL;
  270. }
  271. /* Allocates and returns new loop structure. */
  272. struct loop *
  273. alloc_loop (void)
  274. {
  275. struct loop *loop = ggc_cleared_alloc<struct loop> ();
  276. loop->exits = ggc_cleared_alloc<loop_exit> ();
  277. loop->exits->next = loop->exits->prev = loop->exits;
  278. loop->can_be_parallel = false;
  279. loop->nb_iterations_upper_bound = 0;
  280. loop->nb_iterations_estimate = 0;
  281. return loop;
  282. }
  283. /* Initializes loops structure LOOPS, reserving place for NUM_LOOPS loops
  284. (including the root of the loop tree). */
  285. void
  286. init_loops_structure (struct function *fn,
  287. struct loops *loops, unsigned num_loops)
  288. {
  289. struct loop *root;
  290. memset (loops, 0, sizeof *loops);
  291. vec_alloc (loops->larray, num_loops);
  292. /* Dummy loop containing whole function. */
  293. root = alloc_loop ();
  294. root->num_nodes = n_basic_blocks_for_fn (fn);
  295. root->latch = EXIT_BLOCK_PTR_FOR_FN (fn);
  296. root->header = ENTRY_BLOCK_PTR_FOR_FN (fn);
  297. ENTRY_BLOCK_PTR_FOR_FN (fn)->loop_father = root;
  298. EXIT_BLOCK_PTR_FOR_FN (fn)->loop_father = root;
  299. loops->larray->quick_push (root);
  300. loops->tree_root = root;
  301. }
  302. /* Returns whether HEADER is a loop header. */
  303. bool
  304. bb_loop_header_p (basic_block header)
  305. {
  306. edge_iterator ei;
  307. edge e;
  308. /* If we have an abnormal predecessor, do not consider the
  309. loop (not worth the problems). */
  310. if (bb_has_abnormal_pred (header))
  311. return false;
  312. /* Look for back edges where a predecessor is dominated
  313. by this block. A natural loop has a single entry
  314. node (header) that dominates all the nodes in the
  315. loop. It also has single back edge to the header
  316. from a latch node. */
  317. FOR_EACH_EDGE (e, ei, header->preds)
  318. {
  319. basic_block latch = e->src;
  320. if (latch != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  321. && dominated_by_p (CDI_DOMINATORS, latch, header))
  322. return true;
  323. }
  324. return false;
  325. }
  326. /* Find all the natural loops in the function and save in LOOPS structure and
  327. recalculate loop_father information in basic block structures.
  328. If LOOPS is non-NULL then the loop structures for already recorded loops
  329. will be re-used and their number will not change. We assume that no
  330. stale loops exist in LOOPS.
  331. When LOOPS is NULL it is allocated and re-built from scratch.
  332. Return the built LOOPS structure. */
  333. struct loops *
  334. flow_loops_find (struct loops *loops)
  335. {
  336. bool from_scratch = (loops == NULL);
  337. int *rc_order;
  338. int b;
  339. unsigned i;
  340. /* Ensure that the dominators are computed. */
  341. calculate_dominance_info (CDI_DOMINATORS);
  342. if (!loops)
  343. {
  344. loops = ggc_cleared_alloc<struct loops> ();
  345. init_loops_structure (cfun, loops, 1);
  346. }
  347. /* Ensure that loop exits were released. */
  348. gcc_assert (loops->exits == NULL);
  349. /* Taking care of this degenerate case makes the rest of
  350. this code simpler. */
  351. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  352. return loops;
  353. /* The root loop node contains all basic-blocks. */
  354. loops->tree_root->num_nodes = n_basic_blocks_for_fn (cfun);
  355. /* Compute depth first search order of the CFG so that outer
  356. natural loops will be found before inner natural loops. */
  357. rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  358. pre_and_rev_post_order_compute (NULL, rc_order, false);
  359. /* Gather all loop headers in reverse completion order and allocate
  360. loop structures for loops that are not already present. */
  361. auto_vec<loop_p> larray (loops->larray->length ());
  362. for (b = 0; b < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; b++)
  363. {
  364. basic_block header = BASIC_BLOCK_FOR_FN (cfun, rc_order[b]);
  365. if (bb_loop_header_p (header))
  366. {
  367. struct loop *loop;
  368. /* The current active loop tree has valid loop-fathers for
  369. header blocks. */
  370. if (!from_scratch
  371. && header->loop_father->header == header)
  372. {
  373. loop = header->loop_father;
  374. /* If we found an existing loop remove it from the
  375. loop tree. It is going to be inserted again
  376. below. */
  377. flow_loop_tree_node_remove (loop);
  378. }
  379. else
  380. {
  381. /* Otherwise allocate a new loop structure for the loop. */
  382. loop = alloc_loop ();
  383. /* ??? We could re-use unused loop slots here. */
  384. loop->num = loops->larray->length ();
  385. vec_safe_push (loops->larray, loop);
  386. loop->header = header;
  387. if (!from_scratch
  388. && dump_file && (dump_flags & TDF_DETAILS))
  389. fprintf (dump_file, "flow_loops_find: discovered new "
  390. "loop %d with header %d\n",
  391. loop->num, header->index);
  392. }
  393. /* Reset latch, we recompute it below. */
  394. loop->latch = NULL;
  395. larray.safe_push (loop);
  396. }
  397. /* Make blocks part of the loop root node at start. */
  398. header->loop_father = loops->tree_root;
  399. }
  400. free (rc_order);
  401. /* Now iterate over the loops found, insert them into the loop tree
  402. and assign basic-block ownership. */
  403. for (i = 0; i < larray.length (); ++i)
  404. {
  405. struct loop *loop = larray[i];
  406. basic_block header = loop->header;
  407. edge_iterator ei;
  408. edge e;
  409. flow_loop_tree_node_add (header->loop_father, loop);
  410. loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
  411. /* Look for the latch for this header block, if it has just a
  412. single one. */
  413. FOR_EACH_EDGE (e, ei, header->preds)
  414. {
  415. basic_block latch = e->src;
  416. if (flow_bb_inside_loop_p (loop, latch))
  417. {
  418. if (loop->latch != NULL)
  419. {
  420. /* More than one latch edge. */
  421. loop->latch = NULL;
  422. break;
  423. }
  424. loop->latch = latch;
  425. }
  426. }
  427. }
  428. return loops;
  429. }
  430. /* Ratio of frequencies of edges so that one of more latch edges is
  431. considered to belong to inner loop with same header. */
  432. #define HEAVY_EDGE_RATIO 8
  433. /* Minimum number of samples for that we apply
  434. find_subloop_latch_edge_by_profile heuristics. */
  435. #define HEAVY_EDGE_MIN_SAMPLES 10
  436. /* If the profile info is available, finds an edge in LATCHES that much more
  437. frequent than the remaining edges. Returns such an edge, or NULL if we do
  438. not find one.
  439. We do not use guessed profile here, only the measured one. The guessed
  440. profile is usually too flat and unreliable for this (and it is mostly based
  441. on the loop structure of the program, so it does not make much sense to
  442. derive the loop structure from it). */
  443. static edge
  444. find_subloop_latch_edge_by_profile (vec<edge> latches)
  445. {
  446. unsigned i;
  447. edge e, me = NULL;
  448. gcov_type mcount = 0, tcount = 0;
  449. FOR_EACH_VEC_ELT (latches, i, e)
  450. {
  451. if (e->count > mcount)
  452. {
  453. me = e;
  454. mcount = e->count;
  455. }
  456. tcount += e->count;
  457. }
  458. if (tcount < HEAVY_EDGE_MIN_SAMPLES
  459. || (tcount - mcount) * HEAVY_EDGE_RATIO > tcount)
  460. return NULL;
  461. if (dump_file)
  462. fprintf (dump_file,
  463. "Found latch edge %d -> %d using profile information.\n",
  464. me->src->index, me->dest->index);
  465. return me;
  466. }
  467. /* Among LATCHES, guesses a latch edge of LOOP corresponding to subloop, based
  468. on the structure of induction variables. Returns this edge, or NULL if we
  469. do not find any.
  470. We are quite conservative, and look just for an obvious simple innermost
  471. loop (which is the case where we would lose the most performance by not
  472. disambiguating the loop). More precisely, we look for the following
  473. situation: The source of the chosen latch edge dominates sources of all
  474. the other latch edges. Additionally, the header does not contain a phi node
  475. such that the argument from the chosen edge is equal to the argument from
  476. another edge. */
  477. static edge
  478. find_subloop_latch_edge_by_ivs (struct loop *loop ATTRIBUTE_UNUSED, vec<edge> latches)
  479. {
  480. edge e, latch = latches[0];
  481. unsigned i;
  482. gphi *phi;
  483. gphi_iterator psi;
  484. tree lop;
  485. basic_block bb;
  486. /* Find the candidate for the latch edge. */
  487. for (i = 1; latches.iterate (i, &e); i++)
  488. if (dominated_by_p (CDI_DOMINATORS, latch->src, e->src))
  489. latch = e;
  490. /* Verify that it dominates all the latch edges. */
  491. FOR_EACH_VEC_ELT (latches, i, e)
  492. if (!dominated_by_p (CDI_DOMINATORS, e->src, latch->src))
  493. return NULL;
  494. /* Check for a phi node that would deny that this is a latch edge of
  495. a subloop. */
  496. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  497. {
  498. phi = psi.phi ();
  499. lop = PHI_ARG_DEF_FROM_EDGE (phi, latch);
  500. /* Ignore the values that are not changed inside the subloop. */
  501. if (TREE_CODE (lop) != SSA_NAME
  502. || SSA_NAME_DEF_STMT (lop) == phi)
  503. continue;
  504. bb = gimple_bb (SSA_NAME_DEF_STMT (lop));
  505. if (!bb || !flow_bb_inside_loop_p (loop, bb))
  506. continue;
  507. FOR_EACH_VEC_ELT (latches, i, e)
  508. if (e != latch
  509. && PHI_ARG_DEF_FROM_EDGE (phi, e) == lop)
  510. return NULL;
  511. }
  512. if (dump_file)
  513. fprintf (dump_file,
  514. "Found latch edge %d -> %d using iv structure.\n",
  515. latch->src->index, latch->dest->index);
  516. return latch;
  517. }
  518. /* If we can determine that one of the several latch edges of LOOP behaves
  519. as a latch edge of a separate subloop, returns this edge. Otherwise
  520. returns NULL. */
  521. static edge
  522. find_subloop_latch_edge (struct loop *loop)
  523. {
  524. vec<edge> latches = get_loop_latch_edges (loop);
  525. edge latch = NULL;
  526. if (latches.length () > 1)
  527. {
  528. latch = find_subloop_latch_edge_by_profile (latches);
  529. if (!latch
  530. /* We consider ivs to guess the latch edge only in SSA. Perhaps we
  531. should use cfghook for this, but it is hard to imagine it would
  532. be useful elsewhere. */
  533. && current_ir_type () == IR_GIMPLE)
  534. latch = find_subloop_latch_edge_by_ivs (loop, latches);
  535. }
  536. latches.release ();
  537. return latch;
  538. }
  539. /* Callback for make_forwarder_block. Returns true if the edge E is marked
  540. in the set MFB_REIS_SET. */
  541. static hash_set<edge> *mfb_reis_set;
  542. static bool
  543. mfb_redirect_edges_in_set (edge e)
  544. {
  545. return mfb_reis_set->contains (e);
  546. }
  547. /* Creates a subloop of LOOP with latch edge LATCH. */
  548. static void
  549. form_subloop (struct loop *loop, edge latch)
  550. {
  551. edge_iterator ei;
  552. edge e, new_entry;
  553. struct loop *new_loop;
  554. mfb_reis_set = new hash_set<edge>;
  555. FOR_EACH_EDGE (e, ei, loop->header->preds)
  556. {
  557. if (e != latch)
  558. mfb_reis_set->add (e);
  559. }
  560. new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
  561. NULL);
  562. delete mfb_reis_set;
  563. loop->header = new_entry->src;
  564. /* Find the blocks and subloops that belong to the new loop, and add it to
  565. the appropriate place in the loop tree. */
  566. new_loop = alloc_loop ();
  567. new_loop->header = new_entry->dest;
  568. new_loop->latch = latch->src;
  569. add_loop (new_loop, loop);
  570. }
  571. /* Make all the latch edges of LOOP to go to a single forwarder block --
  572. a new latch of LOOP. */
  573. static void
  574. merge_latch_edges (struct loop *loop)
  575. {
  576. vec<edge> latches = get_loop_latch_edges (loop);
  577. edge latch, e;
  578. unsigned i;
  579. gcc_assert (latches.length () > 0);
  580. if (latches.length () == 1)
  581. loop->latch = latches[0]->src;
  582. else
  583. {
  584. if (dump_file)
  585. fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
  586. mfb_reis_set = new hash_set<edge>;
  587. FOR_EACH_VEC_ELT (latches, i, e)
  588. mfb_reis_set->add (e);
  589. latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
  590. NULL);
  591. delete mfb_reis_set;
  592. loop->header = latch->dest;
  593. loop->latch = latch->src;
  594. }
  595. latches.release ();
  596. }
  597. /* LOOP may have several latch edges. Transform it into (possibly several)
  598. loops with single latch edge. */
  599. static void
  600. disambiguate_multiple_latches (struct loop *loop)
  601. {
  602. edge e;
  603. /* We eliminate the multiple latches by splitting the header to the forwarder
  604. block F and the rest R, and redirecting the edges. There are two cases:
  605. 1) If there is a latch edge E that corresponds to a subloop (we guess
  606. that based on profile -- if it is taken much more often than the
  607. remaining edges; and on trees, using the information about induction
  608. variables of the loops), we redirect E to R, all the remaining edges to
  609. F, then rescan the loops and try again for the outer loop.
  610. 2) If there is no such edge, we redirect all latch edges to F, and the
  611. entry edges to R, thus making F the single latch of the loop. */
  612. if (dump_file)
  613. fprintf (dump_file, "Disambiguating loop %d with multiple latches\n",
  614. loop->num);
  615. /* During latch merging, we may need to redirect the entry edges to a new
  616. block. This would cause problems if the entry edge was the one from the
  617. entry block. To avoid having to handle this case specially, split
  618. such entry edge. */
  619. e = find_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), loop->header);
  620. if (e)
  621. split_edge (e);
  622. while (1)
  623. {
  624. e = find_subloop_latch_edge (loop);
  625. if (!e)
  626. break;
  627. form_subloop (loop, e);
  628. }
  629. merge_latch_edges (loop);
  630. }
  631. /* Split loops with multiple latch edges. */
  632. void
  633. disambiguate_loops_with_multiple_latches (void)
  634. {
  635. struct loop *loop;
  636. FOR_EACH_LOOP (loop, 0)
  637. {
  638. if (!loop->latch)
  639. disambiguate_multiple_latches (loop);
  640. }
  641. }
  642. /* Return nonzero if basic block BB belongs to LOOP. */
  643. bool
  644. flow_bb_inside_loop_p (const struct loop *loop, const_basic_block bb)
  645. {
  646. struct loop *source_loop;
  647. if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  648. || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  649. return 0;
  650. source_loop = bb->loop_father;
  651. return loop == source_loop || flow_loop_nested_p (loop, source_loop);
  652. }
  653. /* Enumeration predicate for get_loop_body_with_size. */
  654. static bool
  655. glb_enum_p (const_basic_block bb, const void *glb_loop)
  656. {
  657. const struct loop *const loop = (const struct loop *) glb_loop;
  658. return (bb != loop->header
  659. && dominated_by_p (CDI_DOMINATORS, bb, loop->header));
  660. }
  661. /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
  662. order against direction of edges from latch. Specially, if
  663. header != latch, latch is the 1-st block. LOOP cannot be the fake
  664. loop tree root, and its size must be at most MAX_SIZE. The blocks
  665. in the LOOP body are stored to BODY, and the size of the LOOP is
  666. returned. */
  667. unsigned
  668. get_loop_body_with_size (const struct loop *loop, basic_block *body,
  669. unsigned max_size)
  670. {
  671. return dfs_enumerate_from (loop->header, 1, glb_enum_p,
  672. body, max_size, loop);
  673. }
  674. /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
  675. order against direction of edges from latch. Specially, if
  676. header != latch, latch is the 1-st block. */
  677. basic_block *
  678. get_loop_body (const struct loop *loop)
  679. {
  680. basic_block *body, bb;
  681. unsigned tv = 0;
  682. gcc_assert (loop->num_nodes);
  683. body = XNEWVEC (basic_block, loop->num_nodes);
  684. if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
  685. {
  686. /* There may be blocks unreachable from EXIT_BLOCK, hence we need to
  687. special-case the fake loop that contains the whole function. */
  688. gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks_for_fn (cfun));
  689. body[tv++] = loop->header;
  690. body[tv++] = EXIT_BLOCK_PTR_FOR_FN (cfun);
  691. FOR_EACH_BB_FN (bb, cfun)
  692. body[tv++] = bb;
  693. }
  694. else
  695. tv = get_loop_body_with_size (loop, body, loop->num_nodes);
  696. gcc_assert (tv == loop->num_nodes);
  697. return body;
  698. }
  699. /* Fills dominance descendants inside LOOP of the basic block BB into
  700. array TOVISIT from index *TV. */
  701. static void
  702. fill_sons_in_loop (const struct loop *loop, basic_block bb,
  703. basic_block *tovisit, int *tv)
  704. {
  705. basic_block son, postpone = NULL;
  706. tovisit[(*tv)++] = bb;
  707. for (son = first_dom_son (CDI_DOMINATORS, bb);
  708. son;
  709. son = next_dom_son (CDI_DOMINATORS, son))
  710. {
  711. if (!flow_bb_inside_loop_p (loop, son))
  712. continue;
  713. if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
  714. {
  715. postpone = son;
  716. continue;
  717. }
  718. fill_sons_in_loop (loop, son, tovisit, tv);
  719. }
  720. if (postpone)
  721. fill_sons_in_loop (loop, postpone, tovisit, tv);
  722. }
  723. /* Gets body of a LOOP (that must be different from the outermost loop)
  724. sorted by dominance relation. Additionally, if a basic block s dominates
  725. the latch, then only blocks dominated by s are be after it. */
  726. basic_block *
  727. get_loop_body_in_dom_order (const struct loop *loop)
  728. {
  729. basic_block *tovisit;
  730. int tv;
  731. gcc_assert (loop->num_nodes);
  732. tovisit = XNEWVEC (basic_block, loop->num_nodes);
  733. gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
  734. tv = 0;
  735. fill_sons_in_loop (loop, loop->header, tovisit, &tv);
  736. gcc_assert (tv == (int) loop->num_nodes);
  737. return tovisit;
  738. }
  739. /* Gets body of a LOOP sorted via provided BB_COMPARATOR. */
  740. basic_block *
  741. get_loop_body_in_custom_order (const struct loop *loop,
  742. int (*bb_comparator) (const void *, const void *))
  743. {
  744. basic_block *bbs = get_loop_body (loop);
  745. qsort (bbs, loop->num_nodes, sizeof (basic_block), bb_comparator);
  746. return bbs;
  747. }
  748. /* Get body of a LOOP in breadth first sort order. */
  749. basic_block *
  750. get_loop_body_in_bfs_order (const struct loop *loop)
  751. {
  752. basic_block *blocks;
  753. basic_block bb;
  754. bitmap visited;
  755. unsigned int i = 0;
  756. unsigned int vc = 1;
  757. gcc_assert (loop->num_nodes);
  758. gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
  759. blocks = XNEWVEC (basic_block, loop->num_nodes);
  760. visited = BITMAP_ALLOC (NULL);
  761. bb = loop->header;
  762. while (i < loop->num_nodes)
  763. {
  764. edge e;
  765. edge_iterator ei;
  766. if (bitmap_set_bit (visited, bb->index))
  767. /* This basic block is now visited */
  768. blocks[i++] = bb;
  769. FOR_EACH_EDGE (e, ei, bb->succs)
  770. {
  771. if (flow_bb_inside_loop_p (loop, e->dest))
  772. {
  773. if (bitmap_set_bit (visited, e->dest->index))
  774. blocks[i++] = e->dest;
  775. }
  776. }
  777. gcc_assert (i >= vc);
  778. bb = blocks[vc++];
  779. }
  780. BITMAP_FREE (visited);
  781. return blocks;
  782. }
  783. /* Hash function for struct loop_exit. */
  784. hashval_t
  785. loop_exit_hasher::hash (loop_exit *exit)
  786. {
  787. return htab_hash_pointer (exit->e);
  788. }
  789. /* Equality function for struct loop_exit. Compares with edge. */
  790. bool
  791. loop_exit_hasher::equal (loop_exit *exit, edge e)
  792. {
  793. return exit->e == e;
  794. }
  795. /* Frees the list of loop exit descriptions EX. */
  796. void
  797. loop_exit_hasher::remove (loop_exit *exit)
  798. {
  799. loop_exit *next;
  800. for (; exit; exit = next)
  801. {
  802. next = exit->next_e;
  803. exit->next->prev = exit->prev;
  804. exit->prev->next = exit->next;
  805. ggc_free (exit);
  806. }
  807. }
  808. /* Returns the list of records for E as an exit of a loop. */
  809. static struct loop_exit *
  810. get_exit_descriptions (edge e)
  811. {
  812. return current_loops->exits->find_with_hash (e, htab_hash_pointer (e));
  813. }
  814. /* Updates the lists of loop exits in that E appears.
  815. If REMOVED is true, E is being removed, and we
  816. just remove it from the lists of exits.
  817. If NEW_EDGE is true and E is not a loop exit, we
  818. do not try to remove it from loop exit lists. */
  819. void
  820. rescan_loop_exit (edge e, bool new_edge, bool removed)
  821. {
  822. struct loop_exit *exits = NULL, *exit;
  823. struct loop *aloop, *cloop;
  824. if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  825. return;
  826. if (!removed
  827. && e->src->loop_father != NULL
  828. && e->dest->loop_father != NULL
  829. && !flow_bb_inside_loop_p (e->src->loop_father, e->dest))
  830. {
  831. cloop = find_common_loop (e->src->loop_father, e->dest->loop_father);
  832. for (aloop = e->src->loop_father;
  833. aloop != cloop;
  834. aloop = loop_outer (aloop))
  835. {
  836. exit = ggc_alloc<loop_exit> ();
  837. exit->e = e;
  838. exit->next = aloop->exits->next;
  839. exit->prev = aloop->exits;
  840. exit->next->prev = exit;
  841. exit->prev->next = exit;
  842. exit->next_e = exits;
  843. exits = exit;
  844. }
  845. }
  846. if (!exits && new_edge)
  847. return;
  848. loop_exit **slot
  849. = current_loops->exits->find_slot_with_hash (e, htab_hash_pointer (e),
  850. exits ? INSERT : NO_INSERT);
  851. if (!slot)
  852. return;
  853. if (exits)
  854. {
  855. if (*slot)
  856. loop_exit_hasher::remove (*slot);
  857. *slot = exits;
  858. }
  859. else
  860. current_loops->exits->clear_slot (slot);
  861. }
  862. /* For each loop, record list of exit edges, and start maintaining these
  863. lists. */
  864. void
  865. record_loop_exits (void)
  866. {
  867. basic_block bb;
  868. edge_iterator ei;
  869. edge e;
  870. if (!current_loops)
  871. return;
  872. if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  873. return;
  874. loops_state_set (LOOPS_HAVE_RECORDED_EXITS);
  875. gcc_assert (current_loops->exits == NULL);
  876. current_loops->exits
  877. = hash_table<loop_exit_hasher>::create_ggc (2 * number_of_loops (cfun));
  878. FOR_EACH_BB_FN (bb, cfun)
  879. {
  880. FOR_EACH_EDGE (e, ei, bb->succs)
  881. {
  882. rescan_loop_exit (e, true, false);
  883. }
  884. }
  885. }
  886. /* Dumps information about the exit in *SLOT to FILE.
  887. Callback for htab_traverse. */
  888. int
  889. dump_recorded_exit (loop_exit **slot, FILE *file)
  890. {
  891. struct loop_exit *exit = *slot;
  892. unsigned n = 0;
  893. edge e = exit->e;
  894. for (; exit != NULL; exit = exit->next_e)
  895. n++;
  896. fprintf (file, "Edge %d->%d exits %u loops\n",
  897. e->src->index, e->dest->index, n);
  898. return 1;
  899. }
  900. /* Dumps the recorded exits of loops to FILE. */
  901. extern void dump_recorded_exits (FILE *);
  902. void
  903. dump_recorded_exits (FILE *file)
  904. {
  905. if (!current_loops->exits)
  906. return;
  907. current_loops->exits->traverse<FILE *, dump_recorded_exit> (file);
  908. }
  909. /* Releases lists of loop exits. */
  910. void
  911. release_recorded_exits (void)
  912. {
  913. gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS));
  914. current_loops->exits->empty ();
  915. current_loops->exits = NULL;
  916. loops_state_clear (LOOPS_HAVE_RECORDED_EXITS);
  917. }
  918. /* Returns the list of the exit edges of a LOOP. */
  919. vec<edge>
  920. get_loop_exit_edges (const struct loop *loop)
  921. {
  922. vec<edge> edges = vNULL;
  923. edge e;
  924. unsigned i;
  925. basic_block *body;
  926. edge_iterator ei;
  927. struct loop_exit *exit;
  928. gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
  929. /* If we maintain the lists of exits, use them. Otherwise we must
  930. scan the body of the loop. */
  931. if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  932. {
  933. for (exit = loop->exits->next; exit->e; exit = exit->next)
  934. edges.safe_push (exit->e);
  935. }
  936. else
  937. {
  938. body = get_loop_body (loop);
  939. for (i = 0; i < loop->num_nodes; i++)
  940. FOR_EACH_EDGE (e, ei, body[i]->succs)
  941. {
  942. if (!flow_bb_inside_loop_p (loop, e->dest))
  943. edges.safe_push (e);
  944. }
  945. free (body);
  946. }
  947. return edges;
  948. }
  949. /* Counts the number of conditional branches inside LOOP. */
  950. unsigned
  951. num_loop_branches (const struct loop *loop)
  952. {
  953. unsigned i, n;
  954. basic_block * body;
  955. gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
  956. body = get_loop_body (loop);
  957. n = 0;
  958. for (i = 0; i < loop->num_nodes; i++)
  959. if (EDGE_COUNT (body[i]->succs) >= 2)
  960. n++;
  961. free (body);
  962. return n;
  963. }
  964. /* Adds basic block BB to LOOP. */
  965. void
  966. add_bb_to_loop (basic_block bb, struct loop *loop)
  967. {
  968. unsigned i;
  969. loop_p ploop;
  970. edge_iterator ei;
  971. edge e;
  972. gcc_assert (bb->loop_father == NULL);
  973. bb->loop_father = loop;
  974. loop->num_nodes++;
  975. FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
  976. ploop->num_nodes++;
  977. FOR_EACH_EDGE (e, ei, bb->succs)
  978. {
  979. rescan_loop_exit (e, true, false);
  980. }
  981. FOR_EACH_EDGE (e, ei, bb->preds)
  982. {
  983. rescan_loop_exit (e, true, false);
  984. }
  985. }
  986. /* Remove basic block BB from loops. */
  987. void
  988. remove_bb_from_loops (basic_block bb)
  989. {
  990. unsigned i;
  991. struct loop *loop = bb->loop_father;
  992. loop_p ploop;
  993. edge_iterator ei;
  994. edge e;
  995. gcc_assert (loop != NULL);
  996. loop->num_nodes--;
  997. FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
  998. ploop->num_nodes--;
  999. bb->loop_father = NULL;
  1000. FOR_EACH_EDGE (e, ei, bb->succs)
  1001. {
  1002. rescan_loop_exit (e, false, true);
  1003. }
  1004. FOR_EACH_EDGE (e, ei, bb->preds)
  1005. {
  1006. rescan_loop_exit (e, false, true);
  1007. }
  1008. }
  1009. /* Finds nearest common ancestor in loop tree for given loops. */
  1010. struct loop *
  1011. find_common_loop (struct loop *loop_s, struct loop *loop_d)
  1012. {
  1013. unsigned sdepth, ddepth;
  1014. if (!loop_s) return loop_d;
  1015. if (!loop_d) return loop_s;
  1016. sdepth = loop_depth (loop_s);
  1017. ddepth = loop_depth (loop_d);
  1018. if (sdepth < ddepth)
  1019. loop_d = (*loop_d->superloops)[sdepth];
  1020. else if (sdepth > ddepth)
  1021. loop_s = (*loop_s->superloops)[ddepth];
  1022. while (loop_s != loop_d)
  1023. {
  1024. loop_s = loop_outer (loop_s);
  1025. loop_d = loop_outer (loop_d);
  1026. }
  1027. return loop_s;
  1028. }
  1029. /* Removes LOOP from structures and frees its data. */
  1030. void
  1031. delete_loop (struct loop *loop)
  1032. {
  1033. /* Remove the loop from structure. */
  1034. flow_loop_tree_node_remove (loop);
  1035. /* Remove loop from loops array. */
  1036. (*current_loops->larray)[loop->num] = NULL;
  1037. /* Free loop data. */
  1038. flow_loop_free (loop);
  1039. }
  1040. /* Cancels the LOOP; it must be innermost one. */
  1041. static void
  1042. cancel_loop (struct loop *loop)
  1043. {
  1044. basic_block *bbs;
  1045. unsigned i;
  1046. struct loop *outer = loop_outer (loop);
  1047. gcc_assert (!loop->inner);
  1048. /* Move blocks up one level (they should be removed as soon as possible). */
  1049. bbs = get_loop_body (loop);
  1050. for (i = 0; i < loop->num_nodes; i++)
  1051. bbs[i]->loop_father = outer;
  1052. free (bbs);
  1053. delete_loop (loop);
  1054. }
  1055. /* Cancels LOOP and all its subloops. */
  1056. void
  1057. cancel_loop_tree (struct loop *loop)
  1058. {
  1059. while (loop->inner)
  1060. cancel_loop_tree (loop->inner);
  1061. cancel_loop (loop);
  1062. }
  1063. /* Checks that information about loops is correct
  1064. -- sizes of loops are all right
  1065. -- results of get_loop_body really belong to the loop
  1066. -- loop header have just single entry edge and single latch edge
  1067. -- loop latches have only single successor that is header of their loop
  1068. -- irreducible loops are correctly marked
  1069. -- the cached loop depth and loop father of each bb is correct
  1070. */
  1071. DEBUG_FUNCTION void
  1072. verify_loop_structure (void)
  1073. {
  1074. unsigned *sizes, i, j;
  1075. sbitmap irreds;
  1076. basic_block bb, *bbs;
  1077. struct loop *loop;
  1078. int err = 0;
  1079. edge e;
  1080. unsigned num = number_of_loops (cfun);
  1081. struct loop_exit *exit, *mexit;
  1082. bool dom_available = dom_info_available_p (CDI_DOMINATORS);
  1083. sbitmap visited;
  1084. if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
  1085. {
  1086. error ("loop verification on loop tree that needs fixup");
  1087. err = 1;
  1088. }
  1089. /* We need up-to-date dominators, compute or verify them. */
  1090. if (!dom_available)
  1091. calculate_dominance_info (CDI_DOMINATORS);
  1092. else
  1093. verify_dominators (CDI_DOMINATORS);
  1094. /* Check the headers. */
  1095. FOR_EACH_BB_FN (bb, cfun)
  1096. if (bb_loop_header_p (bb))
  1097. {
  1098. if (bb->loop_father->header == NULL)
  1099. {
  1100. error ("loop with header %d marked for removal", bb->index);
  1101. err = 1;
  1102. }
  1103. else if (bb->loop_father->header != bb)
  1104. {
  1105. error ("loop with header %d not in loop tree", bb->index);
  1106. err = 1;
  1107. }
  1108. }
  1109. else if (bb->loop_father->header == bb)
  1110. {
  1111. error ("non-loop with header %d not marked for removal", bb->index);
  1112. err = 1;
  1113. }
  1114. /* Check the recorded loop father and sizes of loops. */
  1115. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  1116. bitmap_clear (visited);
  1117. bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  1118. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  1119. {
  1120. unsigned n;
  1121. if (loop->header == NULL)
  1122. {
  1123. error ("removed loop %d in loop tree", loop->num);
  1124. err = 1;
  1125. continue;
  1126. }
  1127. n = get_loop_body_with_size (loop, bbs, n_basic_blocks_for_fn (cfun));
  1128. if (loop->num_nodes != n)
  1129. {
  1130. error ("size of loop %d should be %d, not %d",
  1131. loop->num, n, loop->num_nodes);
  1132. err = 1;
  1133. }
  1134. for (j = 0; j < n; j++)
  1135. {
  1136. bb = bbs[j];
  1137. if (!flow_bb_inside_loop_p (loop, bb))
  1138. {
  1139. error ("bb %d does not belong to loop %d",
  1140. bb->index, loop->num);
  1141. err = 1;
  1142. }
  1143. /* Ignore this block if it is in an inner loop. */
  1144. if (bitmap_bit_p (visited, bb->index))
  1145. continue;
  1146. bitmap_set_bit (visited, bb->index);
  1147. if (bb->loop_father != loop)
  1148. {
  1149. error ("bb %d has father loop %d, should be loop %d",
  1150. bb->index, bb->loop_father->num, loop->num);
  1151. err = 1;
  1152. }
  1153. }
  1154. }
  1155. free (bbs);
  1156. sbitmap_free (visited);
  1157. /* Check headers and latches. */
  1158. FOR_EACH_LOOP (loop, 0)
  1159. {
  1160. i = loop->num;
  1161. if (loop->header == NULL)
  1162. continue;
  1163. if (!bb_loop_header_p (loop->header))
  1164. {
  1165. error ("loop %d%'s header is not a loop header", i);
  1166. err = 1;
  1167. }
  1168. if (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS)
  1169. && EDGE_COUNT (loop->header->preds) != 2)
  1170. {
  1171. error ("loop %d%'s header does not have exactly 2 entries", i);
  1172. err = 1;
  1173. }
  1174. if (loop->latch)
  1175. {
  1176. if (!find_edge (loop->latch, loop->header))
  1177. {
  1178. error ("loop %d%'s latch does not have an edge to its header", i);
  1179. err = 1;
  1180. }
  1181. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, loop->header))
  1182. {
  1183. error ("loop %d%'s latch is not dominated by its header", i);
  1184. err = 1;
  1185. }
  1186. }
  1187. if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
  1188. {
  1189. if (!single_succ_p (loop->latch))
  1190. {
  1191. error ("loop %d%'s latch does not have exactly 1 successor", i);
  1192. err = 1;
  1193. }
  1194. if (single_succ (loop->latch) != loop->header)
  1195. {
  1196. error ("loop %d%'s latch does not have header as successor", i);
  1197. err = 1;
  1198. }
  1199. if (loop->latch->loop_father != loop)
  1200. {
  1201. error ("loop %d%'s latch does not belong directly to it", i);
  1202. err = 1;
  1203. }
  1204. }
  1205. if (loop->header->loop_father != loop)
  1206. {
  1207. error ("loop %d%'s header does not belong directly to it", i);
  1208. err = 1;
  1209. }
  1210. if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
  1211. && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
  1212. {
  1213. error ("loop %d%'s latch is marked as part of irreducible region", i);
  1214. err = 1;
  1215. }
  1216. }
  1217. /* Check irreducible loops. */
  1218. if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
  1219. {
  1220. /* Record old info. */
  1221. irreds = sbitmap_alloc (last_basic_block_for_fn (cfun));
  1222. FOR_EACH_BB_FN (bb, cfun)
  1223. {
  1224. edge_iterator ei;
  1225. if (bb->flags & BB_IRREDUCIBLE_LOOP)
  1226. bitmap_set_bit (irreds, bb->index);
  1227. else
  1228. bitmap_clear_bit (irreds, bb->index);
  1229. FOR_EACH_EDGE (e, ei, bb->succs)
  1230. if (e->flags & EDGE_IRREDUCIBLE_LOOP)
  1231. e->flags |= EDGE_ALL_FLAGS + 1;
  1232. }
  1233. /* Recount it. */
  1234. mark_irreducible_loops ();
  1235. /* Compare. */
  1236. FOR_EACH_BB_FN (bb, cfun)
  1237. {
  1238. edge_iterator ei;
  1239. if ((bb->flags & BB_IRREDUCIBLE_LOOP)
  1240. && !bitmap_bit_p (irreds, bb->index))
  1241. {
  1242. error ("basic block %d should be marked irreducible", bb->index);
  1243. err = 1;
  1244. }
  1245. else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
  1246. && bitmap_bit_p (irreds, bb->index))
  1247. {
  1248. error ("basic block %d should not be marked irreducible", bb->index);
  1249. err = 1;
  1250. }
  1251. FOR_EACH_EDGE (e, ei, bb->succs)
  1252. {
  1253. if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
  1254. && !(e->flags & (EDGE_ALL_FLAGS + 1)))
  1255. {
  1256. error ("edge from %d to %d should be marked irreducible",
  1257. e->src->index, e->dest->index);
  1258. err = 1;
  1259. }
  1260. else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
  1261. && (e->flags & (EDGE_ALL_FLAGS + 1)))
  1262. {
  1263. error ("edge from %d to %d should not be marked irreducible",
  1264. e->src->index, e->dest->index);
  1265. err = 1;
  1266. }
  1267. e->flags &= ~(EDGE_ALL_FLAGS + 1);
  1268. }
  1269. }
  1270. free (irreds);
  1271. }
  1272. /* Check the recorded loop exits. */
  1273. FOR_EACH_LOOP (loop, 0)
  1274. {
  1275. if (!loop->exits || loop->exits->e != NULL)
  1276. {
  1277. error ("corrupted head of the exits list of loop %d",
  1278. loop->num);
  1279. err = 1;
  1280. }
  1281. else
  1282. {
  1283. /* Check that the list forms a cycle, and all elements except
  1284. for the head are nonnull. */
  1285. for (mexit = loop->exits, exit = mexit->next, i = 0;
  1286. exit->e && exit != mexit;
  1287. exit = exit->next)
  1288. {
  1289. if (i++ & 1)
  1290. mexit = mexit->next;
  1291. }
  1292. if (exit != loop->exits)
  1293. {
  1294. error ("corrupted exits list of loop %d", loop->num);
  1295. err = 1;
  1296. }
  1297. }
  1298. if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  1299. {
  1300. if (loop->exits->next != loop->exits)
  1301. {
  1302. error ("nonempty exits list of loop %d, but exits are not recorded",
  1303. loop->num);
  1304. err = 1;
  1305. }
  1306. }
  1307. }
  1308. if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  1309. {
  1310. unsigned n_exits = 0, eloops;
  1311. sizes = XCNEWVEC (unsigned, num);
  1312. memset (sizes, 0, sizeof (unsigned) * num);
  1313. FOR_EACH_BB_FN (bb, cfun)
  1314. {
  1315. edge_iterator ei;
  1316. if (bb->loop_father == current_loops->tree_root)
  1317. continue;
  1318. FOR_EACH_EDGE (e, ei, bb->succs)
  1319. {
  1320. if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
  1321. continue;
  1322. n_exits++;
  1323. exit = get_exit_descriptions (e);
  1324. if (!exit)
  1325. {
  1326. error ("exit %d->%d not recorded",
  1327. e->src->index, e->dest->index);
  1328. err = 1;
  1329. }
  1330. eloops = 0;
  1331. for (; exit; exit = exit->next_e)
  1332. eloops++;
  1333. for (loop = bb->loop_father;
  1334. loop != e->dest->loop_father
  1335. /* When a loop exit is also an entry edge which
  1336. can happen when avoiding CFG manipulations
  1337. then the last loop exited is the outer loop
  1338. of the loop entered. */
  1339. && loop != loop_outer (e->dest->loop_father);
  1340. loop = loop_outer (loop))
  1341. {
  1342. eloops--;
  1343. sizes[loop->num]++;
  1344. }
  1345. if (eloops != 0)
  1346. {
  1347. error ("wrong list of exited loops for edge %d->%d",
  1348. e->src->index, e->dest->index);
  1349. err = 1;
  1350. }
  1351. }
  1352. }
  1353. if (n_exits != current_loops->exits->elements ())
  1354. {
  1355. error ("too many loop exits recorded");
  1356. err = 1;
  1357. }
  1358. FOR_EACH_LOOP (loop, 0)
  1359. {
  1360. eloops = 0;
  1361. for (exit = loop->exits->next; exit->e; exit = exit->next)
  1362. eloops++;
  1363. if (eloops != sizes[loop->num])
  1364. {
  1365. error ("%d exits recorded for loop %d (having %d exits)",
  1366. eloops, loop->num, sizes[loop->num]);
  1367. err = 1;
  1368. }
  1369. }
  1370. free (sizes);
  1371. }
  1372. gcc_assert (!err);
  1373. if (!dom_available)
  1374. free_dominance_info (CDI_DOMINATORS);
  1375. }
  1376. /* Returns latch edge of LOOP. */
  1377. edge
  1378. loop_latch_edge (const struct loop *loop)
  1379. {
  1380. return find_edge (loop->latch, loop->header);
  1381. }
  1382. /* Returns preheader edge of LOOP. */
  1383. edge
  1384. loop_preheader_edge (const struct loop *loop)
  1385. {
  1386. edge e;
  1387. edge_iterator ei;
  1388. gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS));
  1389. FOR_EACH_EDGE (e, ei, loop->header->preds)
  1390. if (e->src != loop->latch)
  1391. break;
  1392. return e;
  1393. }
  1394. /* Returns true if E is an exit of LOOP. */
  1395. bool
  1396. loop_exit_edge_p (const struct loop *loop, const_edge e)
  1397. {
  1398. return (flow_bb_inside_loop_p (loop, e->src)
  1399. && !flow_bb_inside_loop_p (loop, e->dest));
  1400. }
  1401. /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
  1402. or more than one exit. If loops do not have the exits recorded, NULL
  1403. is returned always. */
  1404. edge
  1405. single_exit (const struct loop *loop)
  1406. {
  1407. struct loop_exit *exit = loop->exits->next;
  1408. if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  1409. return NULL;
  1410. if (exit->e && exit->next == loop->exits)
  1411. return exit->e;
  1412. else
  1413. return NULL;
  1414. }
  1415. /* Returns true when BB has an incoming edge exiting LOOP. */
  1416. bool
  1417. loop_exits_to_bb_p (struct loop *loop, basic_block bb)
  1418. {
  1419. edge e;
  1420. edge_iterator ei;
  1421. FOR_EACH_EDGE (e, ei, bb->preds)
  1422. if (loop_exit_edge_p (loop, e))
  1423. return true;
  1424. return false;
  1425. }
  1426. /* Returns true when BB has an outgoing edge exiting LOOP. */
  1427. bool
  1428. loop_exits_from_bb_p (struct loop *loop, basic_block bb)
  1429. {
  1430. edge e;
  1431. edge_iterator ei;
  1432. FOR_EACH_EDGE (e, ei, bb->succs)
  1433. if (loop_exit_edge_p (loop, e))
  1434. return true;
  1435. return false;
  1436. }
  1437. /* Return location corresponding to the loop control condition if possible. */
  1438. location_t
  1439. get_loop_location (struct loop *loop)
  1440. {
  1441. rtx_insn *insn = NULL;
  1442. struct niter_desc *desc = NULL;
  1443. edge exit;
  1444. /* For a for or while loop, we would like to return the location
  1445. of the for or while statement, if possible. To do this, look
  1446. for the branch guarding the loop back-edge. */
  1447. /* If this is a simple loop with an in_edge, then the loop control
  1448. branch is typically at the end of its source. */
  1449. desc = get_simple_loop_desc (loop);
  1450. if (desc->in_edge)
  1451. {
  1452. FOR_BB_INSNS_REVERSE (desc->in_edge->src, insn)
  1453. {
  1454. if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
  1455. return INSN_LOCATION (insn);
  1456. }
  1457. }
  1458. /* If loop has a single exit, then the loop control branch
  1459. must be at the end of its source. */
  1460. if ((exit = single_exit (loop)))
  1461. {
  1462. FOR_BB_INSNS_REVERSE (exit->src, insn)
  1463. {
  1464. if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
  1465. return INSN_LOCATION (insn);
  1466. }
  1467. }
  1468. /* Next check the latch, to see if it is non-empty. */
  1469. FOR_BB_INSNS_REVERSE (loop->latch, insn)
  1470. {
  1471. if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
  1472. return INSN_LOCATION (insn);
  1473. }
  1474. /* Finally, if none of the above identifies the loop control branch,
  1475. return the first location in the loop header. */
  1476. FOR_BB_INSNS (loop->header, insn)
  1477. {
  1478. if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
  1479. return INSN_LOCATION (insn);
  1480. }
  1481. /* If all else fails, simply return the current function location. */
  1482. return DECL_SOURCE_LOCATION (current_function_decl);
  1483. }
  1484. /* Records that every statement in LOOP is executed I_BOUND times.
  1485. REALISTIC is true if I_BOUND is expected to be close to the real number
  1486. of iterations. UPPER is true if we are sure the loop iterates at most
  1487. I_BOUND times. */
  1488. void
  1489. record_niter_bound (struct loop *loop, const widest_int &i_bound,
  1490. bool realistic, bool upper)
  1491. {
  1492. /* Update the bounds only when there is no previous estimation, or when the
  1493. current estimation is smaller. */
  1494. if (upper
  1495. && (!loop->any_upper_bound
  1496. || wi::ltu_p (i_bound, loop->nb_iterations_upper_bound)))
  1497. {
  1498. loop->any_upper_bound = true;
  1499. loop->nb_iterations_upper_bound = i_bound;
  1500. }
  1501. if (realistic
  1502. && (!loop->any_estimate
  1503. || wi::ltu_p (i_bound, loop->nb_iterations_estimate)))
  1504. {
  1505. loop->any_estimate = true;
  1506. loop->nb_iterations_estimate = i_bound;
  1507. }
  1508. /* If an upper bound is smaller than the realistic estimate of the
  1509. number of iterations, use the upper bound instead. */
  1510. if (loop->any_upper_bound
  1511. && loop->any_estimate
  1512. && wi::ltu_p (loop->nb_iterations_upper_bound,
  1513. loop->nb_iterations_estimate))
  1514. loop->nb_iterations_estimate = loop->nb_iterations_upper_bound;
  1515. }
  1516. /* Similar to get_estimated_loop_iterations, but returns the estimate only
  1517. if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
  1518. on the number of iterations of LOOP could not be derived, returns -1. */
  1519. HOST_WIDE_INT
  1520. get_estimated_loop_iterations_int (struct loop *loop)
  1521. {
  1522. widest_int nit;
  1523. HOST_WIDE_INT hwi_nit;
  1524. if (!get_estimated_loop_iterations (loop, &nit))
  1525. return -1;
  1526. if (!wi::fits_shwi_p (nit))
  1527. return -1;
  1528. hwi_nit = nit.to_shwi ();
  1529. return hwi_nit < 0 ? -1 : hwi_nit;
  1530. }
  1531. /* Returns an upper bound on the number of executions of statements
  1532. in the LOOP. For statements before the loop exit, this exceeds
  1533. the number of execution of the latch by one. */
  1534. HOST_WIDE_INT
  1535. max_stmt_executions_int (struct loop *loop)
  1536. {
  1537. HOST_WIDE_INT nit = get_max_loop_iterations_int (loop);
  1538. HOST_WIDE_INT snit;
  1539. if (nit == -1)
  1540. return -1;
  1541. snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
  1542. /* If the computation overflows, return -1. */
  1543. return snit < 0 ? -1 : snit;
  1544. }
  1545. /* Sets NIT to the estimated number of executions of the latch of the
  1546. LOOP. If we have no reliable estimate, the function returns false, otherwise
  1547. returns true. */
  1548. bool
  1549. get_estimated_loop_iterations (struct loop *loop, widest_int *nit)
  1550. {
  1551. /* Even if the bound is not recorded, possibly we can derrive one from
  1552. profile. */
  1553. if (!loop->any_estimate)
  1554. {
  1555. if (loop->header->count)
  1556. {
  1557. *nit = gcov_type_to_wide_int
  1558. (expected_loop_iterations_unbounded (loop) + 1);
  1559. return true;
  1560. }
  1561. return false;
  1562. }
  1563. *nit = loop->nb_iterations_estimate;
  1564. return true;
  1565. }
  1566. /* Sets NIT to an upper bound for the maximum number of executions of the
  1567. latch of the LOOP. If we have no reliable estimate, the function returns
  1568. false, otherwise returns true. */
  1569. bool
  1570. get_max_loop_iterations (struct loop *loop, widest_int *nit)
  1571. {
  1572. if (!loop->any_upper_bound)
  1573. return false;
  1574. *nit = loop->nb_iterations_upper_bound;
  1575. return true;
  1576. }
  1577. /* Similar to get_max_loop_iterations, but returns the estimate only
  1578. if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
  1579. on the number of iterations of LOOP could not be derived, returns -1. */
  1580. HOST_WIDE_INT
  1581. get_max_loop_iterations_int (struct loop *loop)
  1582. {
  1583. widest_int nit;
  1584. HOST_WIDE_INT hwi_nit;
  1585. if (!get_max_loop_iterations (loop, &nit))
  1586. return -1;
  1587. if (!wi::fits_shwi_p (nit))
  1588. return -1;
  1589. hwi_nit = nit.to_shwi ();
  1590. return hwi_nit < 0 ? -1 : hwi_nit;
  1591. }
  1592. /* Returns the loop depth of the loop BB belongs to. */
  1593. int
  1594. bb_loop_depth (const_basic_block bb)
  1595. {
  1596. return bb->loop_father ? loop_depth (bb->loop_father) : 0;
  1597. }
  1598. /* Marks LOOP for removal and sets LOOPS_NEED_FIXUP. */
  1599. void
  1600. mark_loop_for_removal (loop_p loop)
  1601. {
  1602. if (loop->header == NULL)
  1603. return;
  1604. loop->former_header = loop->header;
  1605. loop->header = NULL;
  1606. loop->latch = NULL;
  1607. loops_state_set (LOOPS_NEED_FIXUP);
  1608. }