cfganal.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /* Control flow graph analysis code for GNU compiler.
  2. Copyright (C) 1987-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. /* This file contains various simple utilities to analyze the CFG. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "predict.h"
  20. #include "vec.h"
  21. #include "hashtab.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "tm.h"
  25. #include "hard-reg-set.h"
  26. #include "input.h"
  27. #include "function.h"
  28. #include "dominance.h"
  29. #include "cfg.h"
  30. #include "cfganal.h"
  31. #include "basic-block.h"
  32. #include "bitmap.h"
  33. #include "sbitmap.h"
  34. #include "timevar.h"
  35. /* Store the data structures necessary for depth-first search. */
  36. struct depth_first_search_dsS {
  37. /* stack for backtracking during the algorithm */
  38. basic_block *stack;
  39. /* number of edges in the stack. That is, positions 0, ..., sp-1
  40. have edges. */
  41. unsigned int sp;
  42. /* record of basic blocks already seen by depth-first search */
  43. sbitmap visited_blocks;
  44. };
  45. typedef struct depth_first_search_dsS *depth_first_search_ds;
  46. static void flow_dfs_compute_reverse_init (depth_first_search_ds);
  47. static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds,
  48. basic_block);
  49. static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds,
  50. basic_block);
  51. static void flow_dfs_compute_reverse_finish (depth_first_search_ds);
  52. /* Mark the back edges in DFS traversal.
  53. Return nonzero if a loop (natural or otherwise) is present.
  54. Inspired by Depth_First_Search_PP described in:
  55. Advanced Compiler Design and Implementation
  56. Steven Muchnick
  57. Morgan Kaufmann, 1997
  58. and heavily borrowed from pre_and_rev_post_order_compute. */
  59. bool
  60. mark_dfs_back_edges (void)
  61. {
  62. edge_iterator *stack;
  63. int *pre;
  64. int *post;
  65. int sp;
  66. int prenum = 1;
  67. int postnum = 1;
  68. sbitmap visited;
  69. bool found = false;
  70. /* Allocate the preorder and postorder number arrays. */
  71. pre = XCNEWVEC (int, last_basic_block_for_fn (cfun));
  72. post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
  73. /* Allocate stack for back-tracking up CFG. */
  74. stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
  75. sp = 0;
  76. /* Allocate bitmap to track nodes that have been visited. */
  77. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  78. /* None of the nodes in the CFG have been visited yet. */
  79. bitmap_clear (visited);
  80. /* Push the first edge on to the stack. */
  81. stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
  82. while (sp)
  83. {
  84. edge_iterator ei;
  85. basic_block src;
  86. basic_block dest;
  87. /* Look at the edge on the top of the stack. */
  88. ei = stack[sp - 1];
  89. src = ei_edge (ei)->src;
  90. dest = ei_edge (ei)->dest;
  91. ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
  92. /* Check if the edge destination has been visited yet. */
  93. if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && ! bitmap_bit_p (visited,
  94. dest->index))
  95. {
  96. /* Mark that we have visited the destination. */
  97. bitmap_set_bit (visited, dest->index);
  98. pre[dest->index] = prenum++;
  99. if (EDGE_COUNT (dest->succs) > 0)
  100. {
  101. /* Since the DEST node has been visited for the first
  102. time, check its successors. */
  103. stack[sp++] = ei_start (dest->succs);
  104. }
  105. else
  106. post[dest->index] = postnum++;
  107. }
  108. else
  109. {
  110. if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  111. && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  112. && pre[src->index] >= pre[dest->index]
  113. && post[dest->index] == 0)
  114. ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
  115. if (ei_one_before_end_p (ei)
  116. && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  117. post[src->index] = postnum++;
  118. if (!ei_one_before_end_p (ei))
  119. ei_next (&stack[sp - 1]);
  120. else
  121. sp--;
  122. }
  123. }
  124. free (pre);
  125. free (post);
  126. free (stack);
  127. sbitmap_free (visited);
  128. return found;
  129. }
  130. /* Find unreachable blocks. An unreachable block will have 0 in
  131. the reachable bit in block->flags. A nonzero value indicates the
  132. block is reachable. */
  133. void
  134. find_unreachable_blocks (void)
  135. {
  136. edge e;
  137. edge_iterator ei;
  138. basic_block *tos, *worklist, bb;
  139. tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  140. /* Clear all the reachability flags. */
  141. FOR_EACH_BB_FN (bb, cfun)
  142. bb->flags &= ~BB_REACHABLE;
  143. /* Add our starting points to the worklist. Almost always there will
  144. be only one. It isn't inconceivable that we might one day directly
  145. support Fortran alternate entry points. */
  146. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  147. {
  148. *tos++ = e->dest;
  149. /* Mark the block reachable. */
  150. e->dest->flags |= BB_REACHABLE;
  151. }
  152. /* Iterate: find everything reachable from what we've already seen. */
  153. while (tos != worklist)
  154. {
  155. basic_block b = *--tos;
  156. FOR_EACH_EDGE (e, ei, b->succs)
  157. {
  158. basic_block dest = e->dest;
  159. if (!(dest->flags & BB_REACHABLE))
  160. {
  161. *tos++ = dest;
  162. dest->flags |= BB_REACHABLE;
  163. }
  164. }
  165. }
  166. free (worklist);
  167. }
  168. /* Functions to access an edge list with a vector representation.
  169. Enough data is kept such that given an index number, the
  170. pred and succ that edge represents can be determined, or
  171. given a pred and a succ, its index number can be returned.
  172. This allows algorithms which consume a lot of memory to
  173. represent the normally full matrix of edge (pred,succ) with a
  174. single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
  175. wasted space in the client code due to sparse flow graphs. */
  176. /* This functions initializes the edge list. Basically the entire
  177. flowgraph is processed, and all edges are assigned a number,
  178. and the data structure is filled in. */
  179. struct edge_list *
  180. create_edge_list (void)
  181. {
  182. struct edge_list *elist;
  183. edge e;
  184. int num_edges;
  185. basic_block bb;
  186. edge_iterator ei;
  187. /* Determine the number of edges in the flow graph by counting successor
  188. edges on each basic block. */
  189. num_edges = 0;
  190. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  191. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  192. {
  193. num_edges += EDGE_COUNT (bb->succs);
  194. }
  195. elist = XNEW (struct edge_list);
  196. elist->num_edges = num_edges;
  197. elist->index_to_edge = XNEWVEC (edge, num_edges);
  198. num_edges = 0;
  199. /* Follow successors of blocks, and register these edges. */
  200. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  201. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  202. FOR_EACH_EDGE (e, ei, bb->succs)
  203. elist->index_to_edge[num_edges++] = e;
  204. return elist;
  205. }
  206. /* This function free's memory associated with an edge list. */
  207. void
  208. free_edge_list (struct edge_list *elist)
  209. {
  210. if (elist)
  211. {
  212. free (elist->index_to_edge);
  213. free (elist);
  214. }
  215. }
  216. /* This function provides debug output showing an edge list. */
  217. DEBUG_FUNCTION void
  218. print_edge_list (FILE *f, struct edge_list *elist)
  219. {
  220. int x;
  221. fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
  222. n_basic_blocks_for_fn (cfun), elist->num_edges);
  223. for (x = 0; x < elist->num_edges; x++)
  224. {
  225. fprintf (f, " %-4d - edge(", x);
  226. if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  227. fprintf (f, "entry,");
  228. else
  229. fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
  230. if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR_FOR_FN (cfun))
  231. fprintf (f, "exit)\n");
  232. else
  233. fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
  234. }
  235. }
  236. /* This function provides an internal consistency check of an edge list,
  237. verifying that all edges are present, and that there are no
  238. extra edges. */
  239. DEBUG_FUNCTION void
  240. verify_edge_list (FILE *f, struct edge_list *elist)
  241. {
  242. int pred, succ, index;
  243. edge e;
  244. basic_block bb, p, s;
  245. edge_iterator ei;
  246. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  247. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  248. {
  249. FOR_EACH_EDGE (e, ei, bb->succs)
  250. {
  251. pred = e->src->index;
  252. succ = e->dest->index;
  253. index = EDGE_INDEX (elist, e->src, e->dest);
  254. if (index == EDGE_INDEX_NO_EDGE)
  255. {
  256. fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
  257. continue;
  258. }
  259. if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
  260. fprintf (f, "*p* Pred for index %d should be %d not %d\n",
  261. index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
  262. if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
  263. fprintf (f, "*p* Succ for index %d should be %d not %d\n",
  264. index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
  265. }
  266. }
  267. /* We've verified that all the edges are in the list, now lets make sure
  268. there are no spurious edges in the list. This is an expensive check! */
  269. FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  270. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  271. FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
  272. {
  273. int found_edge = 0;
  274. FOR_EACH_EDGE (e, ei, p->succs)
  275. if (e->dest == s)
  276. {
  277. found_edge = 1;
  278. break;
  279. }
  280. FOR_EACH_EDGE (e, ei, s->preds)
  281. if (e->src == p)
  282. {
  283. found_edge = 1;
  284. break;
  285. }
  286. if (EDGE_INDEX (elist, p, s)
  287. == EDGE_INDEX_NO_EDGE && found_edge != 0)
  288. fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
  289. p->index, s->index);
  290. if (EDGE_INDEX (elist, p, s)
  291. != EDGE_INDEX_NO_EDGE && found_edge == 0)
  292. fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
  293. p->index, s->index, EDGE_INDEX (elist, p, s));
  294. }
  295. }
  296. /* Functions to compute control dependences. */
  297. /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
  298. void
  299. control_dependences::set_control_dependence_map_bit (basic_block bb,
  300. int edge_index)
  301. {
  302. if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  303. return;
  304. gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  305. bitmap_set_bit (control_dependence_map[bb->index], edge_index);
  306. }
  307. /* Clear all control dependences for block BB. */
  308. void
  309. control_dependences::clear_control_dependence_bitmap (basic_block bb)
  310. {
  311. bitmap_clear (control_dependence_map[bb->index]);
  312. }
  313. /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
  314. This function is necessary because some blocks have negative numbers. */
  315. static inline basic_block
  316. find_pdom (basic_block block)
  317. {
  318. gcc_assert (block != ENTRY_BLOCK_PTR_FOR_FN (cfun));
  319. if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
  320. return EXIT_BLOCK_PTR_FOR_FN (cfun);
  321. else
  322. {
  323. basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
  324. if (! bb)
  325. return EXIT_BLOCK_PTR_FOR_FN (cfun);
  326. return bb;
  327. }
  328. }
  329. /* Determine all blocks' control dependences on the given edge with edge_list
  330. EL index EDGE_INDEX, ala Morgan, Section 3.6. */
  331. void
  332. control_dependences::find_control_dependence (int edge_index)
  333. {
  334. basic_block current_block;
  335. basic_block ending_block;
  336. gcc_assert (INDEX_EDGE_PRED_BB (m_el, edge_index)
  337. != EXIT_BLOCK_PTR_FOR_FN (cfun));
  338. if (INDEX_EDGE_PRED_BB (m_el, edge_index) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  339. ending_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  340. else
  341. ending_block = find_pdom (INDEX_EDGE_PRED_BB (m_el, edge_index));
  342. for (current_block = INDEX_EDGE_SUCC_BB (m_el, edge_index);
  343. current_block != ending_block
  344. && current_block != EXIT_BLOCK_PTR_FOR_FN (cfun);
  345. current_block = find_pdom (current_block))
  346. {
  347. edge e = INDEX_EDGE (m_el, edge_index);
  348. /* For abnormal edges, we don't make current_block control
  349. dependent because instructions that throw are always necessary
  350. anyway. */
  351. if (e->flags & EDGE_ABNORMAL)
  352. continue;
  353. set_control_dependence_map_bit (current_block, edge_index);
  354. }
  355. }
  356. /* Record all blocks' control dependences on all edges in the edge
  357. list EL, ala Morgan, Section 3.6. */
  358. control_dependences::control_dependences (struct edge_list *edges)
  359. : m_el (edges)
  360. {
  361. timevar_push (TV_CONTROL_DEPENDENCES);
  362. control_dependence_map.create (last_basic_block_for_fn (cfun));
  363. for (int i = 0; i < last_basic_block_for_fn (cfun); ++i)
  364. control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
  365. for (int i = 0; i < NUM_EDGES (m_el); ++i)
  366. find_control_dependence (i);
  367. timevar_pop (TV_CONTROL_DEPENDENCES);
  368. }
  369. /* Free control dependences and the associated edge list. */
  370. control_dependences::~control_dependences ()
  371. {
  372. for (unsigned i = 0; i < control_dependence_map.length (); ++i)
  373. BITMAP_FREE (control_dependence_map[i]);
  374. control_dependence_map.release ();
  375. free_edge_list (m_el);
  376. }
  377. /* Returns the bitmap of edges the basic-block I is dependent on. */
  378. bitmap
  379. control_dependences::get_edges_dependent_on (int i)
  380. {
  381. return control_dependence_map[i];
  382. }
  383. /* Returns the edge with index I from the edge list. */
  384. edge
  385. control_dependences::get_edge (int i)
  386. {
  387. return INDEX_EDGE (m_el, i);
  388. }
  389. /* Given PRED and SUCC blocks, return the edge which connects the blocks.
  390. If no such edge exists, return NULL. */
  391. edge
  392. find_edge (basic_block pred, basic_block succ)
  393. {
  394. edge e;
  395. edge_iterator ei;
  396. if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
  397. {
  398. FOR_EACH_EDGE (e, ei, pred->succs)
  399. if (e->dest == succ)
  400. return e;
  401. }
  402. else
  403. {
  404. FOR_EACH_EDGE (e, ei, succ->preds)
  405. if (e->src == pred)
  406. return e;
  407. }
  408. return NULL;
  409. }
  410. /* This routine will determine what, if any, edge there is between
  411. a specified predecessor and successor. */
  412. int
  413. find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
  414. {
  415. int x;
  416. for (x = 0; x < NUM_EDGES (edge_list); x++)
  417. if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
  418. && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
  419. return x;
  420. return (EDGE_INDEX_NO_EDGE);
  421. }
  422. /* This routine will remove any fake predecessor edges for a basic block.
  423. When the edge is removed, it is also removed from whatever successor
  424. list it is in. */
  425. static void
  426. remove_fake_predecessors (basic_block bb)
  427. {
  428. edge e;
  429. edge_iterator ei;
  430. for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
  431. {
  432. if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
  433. remove_edge (e);
  434. else
  435. ei_next (&ei);
  436. }
  437. }
  438. /* This routine will remove all fake edges from the flow graph. If
  439. we remove all fake successors, it will automatically remove all
  440. fake predecessors. */
  441. void
  442. remove_fake_edges (void)
  443. {
  444. basic_block bb;
  445. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
  446. remove_fake_predecessors (bb);
  447. }
  448. /* This routine will remove all fake edges to the EXIT_BLOCK. */
  449. void
  450. remove_fake_exit_edges (void)
  451. {
  452. remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun));
  453. }
  454. /* This function will add a fake edge between any block which has no
  455. successors, and the exit block. Some data flow equations require these
  456. edges to exist. */
  457. void
  458. add_noreturn_fake_exit_edges (void)
  459. {
  460. basic_block bb;
  461. FOR_EACH_BB_FN (bb, cfun)
  462. if (EDGE_COUNT (bb->succs) == 0)
  463. make_single_succ_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
  464. }
  465. /* This function adds a fake edge between any infinite loops to the
  466. exit block. Some optimizations require a path from each node to
  467. the exit node.
  468. See also Morgan, Figure 3.10, pp. 82-83.
  469. The current implementation is ugly, not attempting to minimize the
  470. number of inserted fake edges. To reduce the number of fake edges
  471. to insert, add fake edges from _innermost_ loops containing only
  472. nodes not reachable from the exit block. */
  473. void
  474. connect_infinite_loops_to_exit (void)
  475. {
  476. basic_block unvisited_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
  477. basic_block deadend_block;
  478. struct depth_first_search_dsS dfs_ds;
  479. /* Perform depth-first search in the reverse graph to find nodes
  480. reachable from the exit block. */
  481. flow_dfs_compute_reverse_init (&dfs_ds);
  482. flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR_FOR_FN (cfun));
  483. /* Repeatedly add fake edges, updating the unreachable nodes. */
  484. while (1)
  485. {
  486. unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
  487. unvisited_block);
  488. if (!unvisited_block)
  489. break;
  490. deadend_block = dfs_find_deadend (unvisited_block);
  491. make_edge (deadend_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
  492. flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
  493. }
  494. flow_dfs_compute_reverse_finish (&dfs_ds);
  495. return;
  496. }
  497. /* Compute reverse top sort order. This is computing a post order
  498. numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
  499. ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
  500. true, unreachable blocks are deleted. */
  501. int
  502. post_order_compute (int *post_order, bool include_entry_exit,
  503. bool delete_unreachable)
  504. {
  505. edge_iterator *stack;
  506. int sp;
  507. int post_order_num = 0;
  508. sbitmap visited;
  509. int count;
  510. if (include_entry_exit)
  511. post_order[post_order_num++] = EXIT_BLOCK;
  512. /* Allocate stack for back-tracking up CFG. */
  513. stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
  514. sp = 0;
  515. /* Allocate bitmap to track nodes that have been visited. */
  516. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  517. /* None of the nodes in the CFG have been visited yet. */
  518. bitmap_clear (visited);
  519. /* Push the first edge on to the stack. */
  520. stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
  521. while (sp)
  522. {
  523. edge_iterator ei;
  524. basic_block src;
  525. basic_block dest;
  526. /* Look at the edge on the top of the stack. */
  527. ei = stack[sp - 1];
  528. src = ei_edge (ei)->src;
  529. dest = ei_edge (ei)->dest;
  530. /* Check if the edge destination has been visited yet. */
  531. if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  532. && ! bitmap_bit_p (visited, dest->index))
  533. {
  534. /* Mark that we have visited the destination. */
  535. bitmap_set_bit (visited, dest->index);
  536. if (EDGE_COUNT (dest->succs) > 0)
  537. /* Since the DEST node has been visited for the first
  538. time, check its successors. */
  539. stack[sp++] = ei_start (dest->succs);
  540. else
  541. post_order[post_order_num++] = dest->index;
  542. }
  543. else
  544. {
  545. if (ei_one_before_end_p (ei)
  546. && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  547. post_order[post_order_num++] = src->index;
  548. if (!ei_one_before_end_p (ei))
  549. ei_next (&stack[sp - 1]);
  550. else
  551. sp--;
  552. }
  553. }
  554. if (include_entry_exit)
  555. {
  556. post_order[post_order_num++] = ENTRY_BLOCK;
  557. count = post_order_num;
  558. }
  559. else
  560. count = post_order_num + 2;
  561. /* Delete the unreachable blocks if some were found and we are
  562. supposed to do it. */
  563. if (delete_unreachable && (count != n_basic_blocks_for_fn (cfun)))
  564. {
  565. basic_block b;
  566. basic_block next_bb;
  567. for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
  568. != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
  569. {
  570. next_bb = b->next_bb;
  571. if (!(bitmap_bit_p (visited, b->index)))
  572. delete_basic_block (b);
  573. }
  574. tidy_fallthru_edges ();
  575. }
  576. free (stack);
  577. sbitmap_free (visited);
  578. return post_order_num;
  579. }
  580. /* Helper routine for inverted_post_order_compute
  581. flow_dfs_compute_reverse_execute, and the reverse-CFG
  582. deapth first search in dominance.c.
  583. BB has to belong to a region of CFG
  584. unreachable by inverted traversal from the exit.
  585. i.e. there's no control flow path from ENTRY to EXIT
  586. that contains this BB.
  587. This can happen in two cases - if there's an infinite loop
  588. or if there's a block that has no successor
  589. (call to a function with no return).
  590. Some RTL passes deal with this condition by
  591. calling connect_infinite_loops_to_exit () and/or
  592. add_noreturn_fake_exit_edges ().
  593. However, those methods involve modifying the CFG itself
  594. which may not be desirable.
  595. Hence, we deal with the infinite loop/no return cases
  596. by identifying a unique basic block that can reach all blocks
  597. in such a region by inverted traversal.
  598. This function returns a basic block that guarantees
  599. that all blocks in the region are reachable
  600. by starting an inverted traversal from the returned block. */
  601. basic_block
  602. dfs_find_deadend (basic_block bb)
  603. {
  604. bitmap visited = BITMAP_ALLOC (NULL);
  605. for (;;)
  606. {
  607. if (EDGE_COUNT (bb->succs) == 0
  608. || ! bitmap_set_bit (visited, bb->index))
  609. {
  610. BITMAP_FREE (visited);
  611. return bb;
  612. }
  613. bb = EDGE_SUCC (bb, 0)->dest;
  614. }
  615. gcc_unreachable ();
  616. }
  617. /* Compute the reverse top sort order of the inverted CFG
  618. i.e. starting from the exit block and following the edges backward
  619. (from successors to predecessors).
  620. This ordering can be used for forward dataflow problems among others.
  621. This function assumes that all blocks in the CFG are reachable
  622. from the ENTRY (but not necessarily from EXIT).
  623. If there's an infinite loop,
  624. a simple inverted traversal starting from the blocks
  625. with no successors can't visit all blocks.
  626. To solve this problem, we first do inverted traversal
  627. starting from the blocks with no successor.
  628. And if there's any block left that's not visited by the regular
  629. inverted traversal from EXIT,
  630. those blocks are in such problematic region.
  631. Among those, we find one block that has
  632. any visited predecessor (which is an entry into such a region),
  633. and start looking for a "dead end" from that block
  634. and do another inverted traversal from that block. */
  635. int
  636. inverted_post_order_compute (int *post_order)
  637. {
  638. basic_block bb;
  639. edge_iterator *stack;
  640. int sp;
  641. int post_order_num = 0;
  642. sbitmap visited;
  643. /* Allocate stack for back-tracking up CFG. */
  644. stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
  645. sp = 0;
  646. /* Allocate bitmap to track nodes that have been visited. */
  647. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  648. /* None of the nodes in the CFG have been visited yet. */
  649. bitmap_clear (visited);
  650. /* Put all blocks that have no successor into the initial work list. */
  651. FOR_ALL_BB_FN (bb, cfun)
  652. if (EDGE_COUNT (bb->succs) == 0)
  653. {
  654. /* Push the initial edge on to the stack. */
  655. if (EDGE_COUNT (bb->preds) > 0)
  656. {
  657. stack[sp++] = ei_start (bb->preds);
  658. bitmap_set_bit (visited, bb->index);
  659. }
  660. }
  661. do
  662. {
  663. bool has_unvisited_bb = false;
  664. /* The inverted traversal loop. */
  665. while (sp)
  666. {
  667. edge_iterator ei;
  668. basic_block pred;
  669. /* Look at the edge on the top of the stack. */
  670. ei = stack[sp - 1];
  671. bb = ei_edge (ei)->dest;
  672. pred = ei_edge (ei)->src;
  673. /* Check if the predecessor has been visited yet. */
  674. if (! bitmap_bit_p (visited, pred->index))
  675. {
  676. /* Mark that we have visited the destination. */
  677. bitmap_set_bit (visited, pred->index);
  678. if (EDGE_COUNT (pred->preds) > 0)
  679. /* Since the predecessor node has been visited for the first
  680. time, check its predecessors. */
  681. stack[sp++] = ei_start (pred->preds);
  682. else
  683. post_order[post_order_num++] = pred->index;
  684. }
  685. else
  686. {
  687. if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
  688. && ei_one_before_end_p (ei))
  689. post_order[post_order_num++] = bb->index;
  690. if (!ei_one_before_end_p (ei))
  691. ei_next (&stack[sp - 1]);
  692. else
  693. sp--;
  694. }
  695. }
  696. /* Detect any infinite loop and activate the kludge.
  697. Note that this doesn't check EXIT_BLOCK itself
  698. since EXIT_BLOCK is always added after the outer do-while loop. */
  699. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  700. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  701. if (!bitmap_bit_p (visited, bb->index))
  702. {
  703. has_unvisited_bb = true;
  704. if (EDGE_COUNT (bb->preds) > 0)
  705. {
  706. edge_iterator ei;
  707. edge e;
  708. basic_block visited_pred = NULL;
  709. /* Find an already visited predecessor. */
  710. FOR_EACH_EDGE (e, ei, bb->preds)
  711. {
  712. if (bitmap_bit_p (visited, e->src->index))
  713. visited_pred = e->src;
  714. }
  715. if (visited_pred)
  716. {
  717. basic_block be = dfs_find_deadend (bb);
  718. gcc_assert (be != NULL);
  719. bitmap_set_bit (visited, be->index);
  720. stack[sp++] = ei_start (be->preds);
  721. break;
  722. }
  723. }
  724. }
  725. if (has_unvisited_bb && sp == 0)
  726. {
  727. /* No blocks are reachable from EXIT at all.
  728. Find a dead-end from the ENTRY, and restart the iteration. */
  729. basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  730. gcc_assert (be != NULL);
  731. bitmap_set_bit (visited, be->index);
  732. stack[sp++] = ei_start (be->preds);
  733. }
  734. /* The only case the below while fires is
  735. when there's an infinite loop. */
  736. }
  737. while (sp);
  738. /* EXIT_BLOCK is always included. */
  739. post_order[post_order_num++] = EXIT_BLOCK;
  740. free (stack);
  741. sbitmap_free (visited);
  742. return post_order_num;
  743. }
  744. /* Compute the depth first search order of FN and store in the array
  745. PRE_ORDER if nonzero. If REV_POST_ORDER is nonzero, return the
  746. reverse completion number for each node. Returns the number of nodes
  747. visited. A depth first search tries to get as far away from the starting
  748. point as quickly as possible.
  749. In case the function has unreachable blocks the number of nodes
  750. visited does not include them.
  751. pre_order is a really a preorder numbering of the graph.
  752. rev_post_order is really a reverse postorder numbering of the graph. */
  753. int
  754. pre_and_rev_post_order_compute_fn (struct function *fn,
  755. int *pre_order, int *rev_post_order,
  756. bool include_entry_exit)
  757. {
  758. edge_iterator *stack;
  759. int sp;
  760. int pre_order_num = 0;
  761. int rev_post_order_num = n_basic_blocks_for_fn (cfun) - 1;
  762. sbitmap visited;
  763. /* Allocate stack for back-tracking up CFG. */
  764. stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
  765. sp = 0;
  766. if (include_entry_exit)
  767. {
  768. if (pre_order)
  769. pre_order[pre_order_num] = ENTRY_BLOCK;
  770. pre_order_num++;
  771. if (rev_post_order)
  772. rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
  773. }
  774. else
  775. rev_post_order_num -= NUM_FIXED_BLOCKS;
  776. /* Allocate bitmap to track nodes that have been visited. */
  777. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  778. /* None of the nodes in the CFG have been visited yet. */
  779. bitmap_clear (visited);
  780. /* Push the first edge on to the stack. */
  781. stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs);
  782. while (sp)
  783. {
  784. edge_iterator ei;
  785. basic_block src;
  786. basic_block dest;
  787. /* Look at the edge on the top of the stack. */
  788. ei = stack[sp - 1];
  789. src = ei_edge (ei)->src;
  790. dest = ei_edge (ei)->dest;
  791. /* Check if the edge destination has been visited yet. */
  792. if (dest != EXIT_BLOCK_PTR_FOR_FN (fn)
  793. && ! bitmap_bit_p (visited, dest->index))
  794. {
  795. /* Mark that we have visited the destination. */
  796. bitmap_set_bit (visited, dest->index);
  797. if (pre_order)
  798. pre_order[pre_order_num] = dest->index;
  799. pre_order_num++;
  800. if (EDGE_COUNT (dest->succs) > 0)
  801. /* Since the DEST node has been visited for the first
  802. time, check its successors. */
  803. stack[sp++] = ei_start (dest->succs);
  804. else if (rev_post_order)
  805. /* There are no successors for the DEST node so assign
  806. its reverse completion number. */
  807. rev_post_order[rev_post_order_num--] = dest->index;
  808. }
  809. else
  810. {
  811. if (ei_one_before_end_p (ei)
  812. && src != ENTRY_BLOCK_PTR_FOR_FN (fn)
  813. && rev_post_order)
  814. /* There are no more successors for the SRC node
  815. so assign its reverse completion number. */
  816. rev_post_order[rev_post_order_num--] = src->index;
  817. if (!ei_one_before_end_p (ei))
  818. ei_next (&stack[sp - 1]);
  819. else
  820. sp--;
  821. }
  822. }
  823. free (stack);
  824. sbitmap_free (visited);
  825. if (include_entry_exit)
  826. {
  827. if (pre_order)
  828. pre_order[pre_order_num] = EXIT_BLOCK;
  829. pre_order_num++;
  830. if (rev_post_order)
  831. rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
  832. }
  833. return pre_order_num;
  834. }
  835. /* Like pre_and_rev_post_order_compute_fn but operating on the
  836. current function and asserting that all nodes were visited. */
  837. int
  838. pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
  839. bool include_entry_exit)
  840. {
  841. int pre_order_num
  842. = pre_and_rev_post_order_compute_fn (cfun, pre_order, rev_post_order,
  843. include_entry_exit);
  844. if (include_entry_exit)
  845. /* The number of nodes visited should be the number of blocks. */
  846. gcc_assert (pre_order_num == n_basic_blocks_for_fn (cfun));
  847. else
  848. /* The number of nodes visited should be the number of blocks minus
  849. the entry and exit blocks which are not visited here. */
  850. gcc_assert (pre_order_num
  851. == (n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS));
  852. return pre_order_num;
  853. }
  854. /* Compute the depth first search order on the _reverse_ graph and
  855. store in the array DFS_ORDER, marking the nodes visited in VISITED.
  856. Returns the number of nodes visited.
  857. The computation is split into three pieces:
  858. flow_dfs_compute_reverse_init () creates the necessary data
  859. structures.
  860. flow_dfs_compute_reverse_add_bb () adds a basic block to the data
  861. structures. The block will start the search.
  862. flow_dfs_compute_reverse_execute () continues (or starts) the
  863. search using the block on the top of the stack, stopping when the
  864. stack is empty.
  865. flow_dfs_compute_reverse_finish () destroys the necessary data
  866. structures.
  867. Thus, the user will probably call ..._init(), call ..._add_bb() to
  868. add a beginning basic block to the stack, call ..._execute(),
  869. possibly add another bb to the stack and again call ..._execute(),
  870. ..., and finally call _finish(). */
  871. /* Initialize the data structures used for depth-first search on the
  872. reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
  873. added to the basic block stack. DATA is the current depth-first
  874. search context. If INITIALIZE_STACK is nonzero, there is an
  875. element on the stack. */
  876. static void
  877. flow_dfs_compute_reverse_init (depth_first_search_ds data)
  878. {
  879. /* Allocate stack for back-tracking up CFG. */
  880. data->stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  881. data->sp = 0;
  882. /* Allocate bitmap to track nodes that have been visited. */
  883. data->visited_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
  884. /* None of the nodes in the CFG have been visited yet. */
  885. bitmap_clear (data->visited_blocks);
  886. return;
  887. }
  888. /* Add the specified basic block to the top of the dfs data
  889. structures. When the search continues, it will start at the
  890. block. */
  891. static void
  892. flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
  893. {
  894. data->stack[data->sp++] = bb;
  895. bitmap_set_bit (data->visited_blocks, bb->index);
  896. }
  897. /* Continue the depth-first search through the reverse graph starting with the
  898. block at the stack's top and ending when the stack is empty. Visited nodes
  899. are marked. Returns an unvisited basic block, or NULL if there is none
  900. available. */
  901. static basic_block
  902. flow_dfs_compute_reverse_execute (depth_first_search_ds data,
  903. basic_block last_unvisited)
  904. {
  905. basic_block bb;
  906. edge e;
  907. edge_iterator ei;
  908. while (data->sp > 0)
  909. {
  910. bb = data->stack[--data->sp];
  911. /* Perform depth-first search on adjacent vertices. */
  912. FOR_EACH_EDGE (e, ei, bb->preds)
  913. if (!bitmap_bit_p (data->visited_blocks, e->src->index))
  914. flow_dfs_compute_reverse_add_bb (data, e->src);
  915. }
  916. /* Determine if there are unvisited basic blocks. */
  917. FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
  918. if (!bitmap_bit_p (data->visited_blocks, bb->index))
  919. return bb;
  920. return NULL;
  921. }
  922. /* Destroy the data structures needed for depth-first search on the
  923. reverse graph. */
  924. static void
  925. flow_dfs_compute_reverse_finish (depth_first_search_ds data)
  926. {
  927. free (data->stack);
  928. sbitmap_free (data->visited_blocks);
  929. }
  930. /* Performs dfs search from BB over vertices satisfying PREDICATE;
  931. if REVERSE, go against direction of edges. Returns number of blocks
  932. found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
  933. int
  934. dfs_enumerate_from (basic_block bb, int reverse,
  935. bool (*predicate) (const_basic_block, const void *),
  936. basic_block *rslt, int rslt_max, const void *data)
  937. {
  938. basic_block *st, lbb;
  939. int sp = 0, tv = 0;
  940. unsigned size;
  941. /* A bitmap to keep track of visited blocks. Allocating it each time
  942. this function is called is not possible, since dfs_enumerate_from
  943. is often used on small (almost) disjoint parts of cfg (bodies of
  944. loops), and allocating a large sbitmap would lead to quadratic
  945. behavior. */
  946. static sbitmap visited;
  947. static unsigned v_size;
  948. #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
  949. #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
  950. #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
  951. /* Resize the VISITED sbitmap if necessary. */
  952. size = last_basic_block_for_fn (cfun);
  953. if (size < 10)
  954. size = 10;
  955. if (!visited)
  956. {
  957. visited = sbitmap_alloc (size);
  958. bitmap_clear (visited);
  959. v_size = size;
  960. }
  961. else if (v_size < size)
  962. {
  963. /* Ensure that we increase the size of the sbitmap exponentially. */
  964. if (2 * v_size > size)
  965. size = 2 * v_size;
  966. visited = sbitmap_resize (visited, size, 0);
  967. v_size = size;
  968. }
  969. st = XNEWVEC (basic_block, rslt_max);
  970. rslt[tv++] = st[sp++] = bb;
  971. MARK_VISITED (bb);
  972. while (sp)
  973. {
  974. edge e;
  975. edge_iterator ei;
  976. lbb = st[--sp];
  977. if (reverse)
  978. {
  979. FOR_EACH_EDGE (e, ei, lbb->preds)
  980. if (!VISITED_P (e->src) && predicate (e->src, data))
  981. {
  982. gcc_assert (tv != rslt_max);
  983. rslt[tv++] = st[sp++] = e->src;
  984. MARK_VISITED (e->src);
  985. }
  986. }
  987. else
  988. {
  989. FOR_EACH_EDGE (e, ei, lbb->succs)
  990. if (!VISITED_P (e->dest) && predicate (e->dest, data))
  991. {
  992. gcc_assert (tv != rslt_max);
  993. rslt[tv++] = st[sp++] = e->dest;
  994. MARK_VISITED (e->dest);
  995. }
  996. }
  997. }
  998. free (st);
  999. for (sp = 0; sp < tv; sp++)
  1000. UNMARK_VISITED (rslt[sp]);
  1001. return tv;
  1002. #undef MARK_VISITED
  1003. #undef UNMARK_VISITED
  1004. #undef VISITED_P
  1005. }
  1006. /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
  1007. This algorithm can be found in Timothy Harvey's PhD thesis, at
  1008. http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
  1009. dominance algorithms.
  1010. First, we identify each join point, j (any node with more than one
  1011. incoming edge is a join point).
  1012. We then examine each predecessor, p, of j and walk up the dominator tree
  1013. starting at p.
  1014. We stop the walk when we reach j's immediate dominator - j is in the
  1015. dominance frontier of each of the nodes in the walk, except for j's
  1016. immediate dominator. Intuitively, all of the rest of j's dominators are
  1017. shared by j's predecessors as well.
  1018. Since they dominate j, they will not have j in their dominance frontiers.
  1019. The number of nodes touched by this algorithm is equal to the size
  1020. of the dominance frontiers, no more, no less.
  1021. */
  1022. static void
  1023. compute_dominance_frontiers_1 (bitmap_head *frontiers)
  1024. {
  1025. edge p;
  1026. edge_iterator ei;
  1027. basic_block b;
  1028. FOR_EACH_BB_FN (b, cfun)
  1029. {
  1030. if (EDGE_COUNT (b->preds) >= 2)
  1031. {
  1032. FOR_EACH_EDGE (p, ei, b->preds)
  1033. {
  1034. basic_block runner = p->src;
  1035. basic_block domsb;
  1036. if (runner == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1037. continue;
  1038. domsb = get_immediate_dominator (CDI_DOMINATORS, b);
  1039. while (runner != domsb)
  1040. {
  1041. if (!bitmap_set_bit (&frontiers[runner->index],
  1042. b->index))
  1043. break;
  1044. runner = get_immediate_dominator (CDI_DOMINATORS,
  1045. runner);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }
  1051. void
  1052. compute_dominance_frontiers (bitmap_head *frontiers)
  1053. {
  1054. timevar_push (TV_DOM_FRONTIERS);
  1055. compute_dominance_frontiers_1 (frontiers);
  1056. timevar_pop (TV_DOM_FRONTIERS);
  1057. }
  1058. /* Given a set of blocks with variable definitions (DEF_BLOCKS),
  1059. return a bitmap with all the blocks in the iterated dominance
  1060. frontier of the blocks in DEF_BLOCKS. DFS contains dominance
  1061. frontier information as returned by compute_dominance_frontiers.
  1062. The resulting set of blocks are the potential sites where PHI nodes
  1063. are needed. The caller is responsible for freeing the memory
  1064. allocated for the return value. */
  1065. bitmap
  1066. compute_idf (bitmap def_blocks, bitmap_head *dfs)
  1067. {
  1068. bitmap_iterator bi;
  1069. unsigned bb_index, i;
  1070. bitmap phi_insertion_points;
  1071. /* Each block can appear at most twice on the work-stack. */
  1072. auto_vec<int> work_stack (2 * n_basic_blocks_for_fn (cfun));
  1073. phi_insertion_points = BITMAP_ALLOC (NULL);
  1074. /* Seed the work list with all the blocks in DEF_BLOCKS. We use
  1075. vec::quick_push here for speed. This is safe because we know that
  1076. the number of definition blocks is no greater than the number of
  1077. basic blocks, which is the initial capacity of WORK_STACK. */
  1078. EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
  1079. work_stack.quick_push (bb_index);
  1080. /* Pop a block off the worklist, add every block that appears in
  1081. the original block's DF that we have not already processed to
  1082. the worklist. Iterate until the worklist is empty. Blocks
  1083. which are added to the worklist are potential sites for
  1084. PHI nodes. */
  1085. while (work_stack.length () > 0)
  1086. {
  1087. bb_index = work_stack.pop ();
  1088. /* Since the registration of NEW -> OLD name mappings is done
  1089. separately from the call to update_ssa, when updating the SSA
  1090. form, the basic blocks where new and/or old names are defined
  1091. may have disappeared by CFG cleanup calls. In this case,
  1092. we may pull a non-existing block from the work stack. */
  1093. gcc_checking_assert (bb_index
  1094. < (unsigned) last_basic_block_for_fn (cfun));
  1095. EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
  1096. 0, i, bi)
  1097. {
  1098. work_stack.quick_push (i);
  1099. bitmap_set_bit (phi_insertion_points, i);
  1100. }
  1101. }
  1102. return phi_insertion_points;
  1103. }
  1104. /* Intersection and union of preds/succs for sbitmap based data flow
  1105. solvers. All four functions defined below take the same arguments:
  1106. B is the basic block to perform the operation for. DST is the
  1107. target sbitmap, i.e. the result. SRC is an sbitmap vector of size
  1108. last_basic_block so that it can be indexed with basic block indices.
  1109. DST may be (but does not have to be) SRC[B->index]. */
  1110. /* Set the bitmap DST to the intersection of SRC of successors of
  1111. basic block B. */
  1112. void
  1113. bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
  1114. {
  1115. unsigned int set_size = dst->size;
  1116. edge e;
  1117. unsigned ix;
  1118. gcc_assert (!dst->popcount);
  1119. for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
  1120. {
  1121. e = EDGE_SUCC (b, ix);
  1122. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1123. continue;
  1124. bitmap_copy (dst, src[e->dest->index]);
  1125. break;
  1126. }
  1127. if (e == 0)
  1128. bitmap_ones (dst);
  1129. else
  1130. for (++ix; ix < EDGE_COUNT (b->succs); ix++)
  1131. {
  1132. unsigned int i;
  1133. SBITMAP_ELT_TYPE *p, *r;
  1134. e = EDGE_SUCC (b, ix);
  1135. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1136. continue;
  1137. p = src[e->dest->index]->elms;
  1138. r = dst->elms;
  1139. for (i = 0; i < set_size; i++)
  1140. *r++ &= *p++;
  1141. }
  1142. }
  1143. /* Set the bitmap DST to the intersection of SRC of predecessors of
  1144. basic block B. */
  1145. void
  1146. bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
  1147. {
  1148. unsigned int set_size = dst->size;
  1149. edge e;
  1150. unsigned ix;
  1151. gcc_assert (!dst->popcount);
  1152. for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
  1153. {
  1154. e = EDGE_PRED (b, ix);
  1155. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1156. continue;
  1157. bitmap_copy (dst, src[e->src->index]);
  1158. break;
  1159. }
  1160. if (e == 0)
  1161. bitmap_ones (dst);
  1162. else
  1163. for (++ix; ix < EDGE_COUNT (b->preds); ix++)
  1164. {
  1165. unsigned int i;
  1166. SBITMAP_ELT_TYPE *p, *r;
  1167. e = EDGE_PRED (b, ix);
  1168. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1169. continue;
  1170. p = src[e->src->index]->elms;
  1171. r = dst->elms;
  1172. for (i = 0; i < set_size; i++)
  1173. *r++ &= *p++;
  1174. }
  1175. }
  1176. /* Set the bitmap DST to the union of SRC of successors of
  1177. basic block B. */
  1178. void
  1179. bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
  1180. {
  1181. unsigned int set_size = dst->size;
  1182. edge e;
  1183. unsigned ix;
  1184. gcc_assert (!dst->popcount);
  1185. for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
  1186. {
  1187. e = EDGE_SUCC (b, ix);
  1188. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1189. continue;
  1190. bitmap_copy (dst, src[e->dest->index]);
  1191. break;
  1192. }
  1193. if (ix == EDGE_COUNT (b->succs))
  1194. bitmap_clear (dst);
  1195. else
  1196. for (ix++; ix < EDGE_COUNT (b->succs); ix++)
  1197. {
  1198. unsigned int i;
  1199. SBITMAP_ELT_TYPE *p, *r;
  1200. e = EDGE_SUCC (b, ix);
  1201. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1202. continue;
  1203. p = src[e->dest->index]->elms;
  1204. r = dst->elms;
  1205. for (i = 0; i < set_size; i++)
  1206. *r++ |= *p++;
  1207. }
  1208. }
  1209. /* Set the bitmap DST to the union of SRC of predecessors of
  1210. basic block B. */
  1211. void
  1212. bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
  1213. {
  1214. unsigned int set_size = dst->size;
  1215. edge e;
  1216. unsigned ix;
  1217. gcc_assert (!dst->popcount);
  1218. for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
  1219. {
  1220. e = EDGE_PRED (b, ix);
  1221. if (e->src== ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1222. continue;
  1223. bitmap_copy (dst, src[e->src->index]);
  1224. break;
  1225. }
  1226. if (ix == EDGE_COUNT (b->preds))
  1227. bitmap_clear (dst);
  1228. else
  1229. for (ix++; ix < EDGE_COUNT (b->preds); ix++)
  1230. {
  1231. unsigned int i;
  1232. SBITMAP_ELT_TYPE *p, *r;
  1233. e = EDGE_PRED (b, ix);
  1234. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1235. continue;
  1236. p = src[e->src->index]->elms;
  1237. r = dst->elms;
  1238. for (i = 0; i < set_size; i++)
  1239. *r++ |= *p++;
  1240. }
  1241. }
  1242. /* Returns the list of basic blocks in the function in an order that guarantees
  1243. that if a block X has just a single predecessor Y, then Y is after X in the
  1244. ordering. */
  1245. basic_block *
  1246. single_pred_before_succ_order (void)
  1247. {
  1248. basic_block x, y;
  1249. basic_block *order = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  1250. unsigned n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
  1251. unsigned np, i;
  1252. sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  1253. #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
  1254. #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
  1255. bitmap_clear (visited);
  1256. MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  1257. FOR_EACH_BB_FN (x, cfun)
  1258. {
  1259. if (VISITED_P (x))
  1260. continue;
  1261. /* Walk the predecessors of x as long as they have precisely one
  1262. predecessor and add them to the list, so that they get stored
  1263. after x. */
  1264. for (y = x, np = 1;
  1265. single_pred_p (y) && !VISITED_P (single_pred (y));
  1266. y = single_pred (y))
  1267. np++;
  1268. for (y = x, i = n - np;
  1269. single_pred_p (y) && !VISITED_P (single_pred (y));
  1270. y = single_pred (y), i++)
  1271. {
  1272. order[i] = y;
  1273. MARK_VISITED (y);
  1274. }
  1275. order[i] = y;
  1276. MARK_VISITED (y);
  1277. gcc_assert (i == n - 1);
  1278. n -= np;
  1279. }
  1280. sbitmap_free (visited);
  1281. gcc_assert (n == 0);
  1282. return order;
  1283. #undef MARK_VISITED
  1284. #undef VISITED_P
  1285. }