cfg.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /* Control flow graph manipulation 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 low level functions to manipulate the CFG and
  16. analyze it. All other modules should not transform the data structure
  17. directly and use abstraction instead. The file is supposed to be
  18. ordered bottom-up and should not contain any code dependent on a
  19. particular intermediate language (RTL or trees).
  20. Available functionality:
  21. - Initialization/deallocation
  22. init_flow, clear_edges
  23. - Low level basic block manipulation
  24. alloc_block, expunge_block
  25. - Edge manipulation
  26. make_edge, make_single_succ_edge, cached_make_edge, remove_edge
  27. - Low level edge redirection (without updating instruction chain)
  28. redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
  29. - Dumping and debugging
  30. dump_flow_info, debug_flow_info, dump_edge_info
  31. - Allocation of AUX fields for basic blocks
  32. alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
  33. - clear_bb_flags
  34. - Consistency checking
  35. verify_flow_info
  36. - Dumping and debugging
  37. print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
  38. TODO: Document these "Available functionality" functions in the files
  39. that implement them.
  40. */
  41. #include "config.h"
  42. #include "system.h"
  43. #include "coretypes.h"
  44. #include "obstack.h"
  45. #include "ggc.h"
  46. #include "hash-table.h"
  47. #include "alloc-pool.h"
  48. #include "hash-set.h"
  49. #include "machmode.h"
  50. #include "vec.h"
  51. #include "double-int.h"
  52. #include "input.h"
  53. #include "alias.h"
  54. #include "symtab.h"
  55. #include "options.h"
  56. #include "wide-int.h"
  57. #include "inchash.h"
  58. #include "tree.h"
  59. #include "predict.h"
  60. #include "vec.h"
  61. #include "hashtab.h"
  62. #include "hash-set.h"
  63. #include "machmode.h"
  64. #include "tm.h"
  65. #include "hard-reg-set.h"
  66. #include "input.h"
  67. #include "function.h"
  68. #include "dominance.h"
  69. #include "cfg.h"
  70. #include "cfganal.h"
  71. #include "basic-block.h"
  72. #include "df.h"
  73. #include "cfgloop.h" /* FIXME: For struct loop. */
  74. #include "dumpfile.h"
  75. #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
  76. /* Called once at initialization time. */
  77. void
  78. init_flow (struct function *the_fun)
  79. {
  80. if (!the_fun->cfg)
  81. the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
  82. n_edges_for_fn (the_fun) = 0;
  83. ENTRY_BLOCK_PTR_FOR_FN (the_fun)
  84. = ggc_cleared_alloc<basic_block_def> ();
  85. ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
  86. EXIT_BLOCK_PTR_FOR_FN (the_fun)
  87. = ggc_cleared_alloc<basic_block_def> ();
  88. EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
  89. ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
  90. = EXIT_BLOCK_PTR_FOR_FN (the_fun);
  91. EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
  92. = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
  93. }
  94. /* Helper function for remove_edge and clear_edges. Frees edge structure
  95. without actually removing it from the pred/succ arrays. */
  96. static void
  97. free_edge (edge e)
  98. {
  99. n_edges_for_fn (cfun)--;
  100. ggc_free (e);
  101. }
  102. /* Free the memory associated with the edge structures. */
  103. void
  104. clear_edges (void)
  105. {
  106. basic_block bb;
  107. edge e;
  108. edge_iterator ei;
  109. FOR_EACH_BB_FN (bb, cfun)
  110. {
  111. FOR_EACH_EDGE (e, ei, bb->succs)
  112. free_edge (e);
  113. vec_safe_truncate (bb->succs, 0);
  114. vec_safe_truncate (bb->preds, 0);
  115. }
  116. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  117. free_edge (e);
  118. vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, 0);
  119. vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs, 0);
  120. gcc_assert (!n_edges_for_fn (cfun));
  121. }
  122. /* Allocate memory for basic_block. */
  123. basic_block
  124. alloc_block (void)
  125. {
  126. basic_block bb;
  127. bb = ggc_cleared_alloc<basic_block_def> ();
  128. return bb;
  129. }
  130. /* Link block B to chain after AFTER. */
  131. void
  132. link_block (basic_block b, basic_block after)
  133. {
  134. b->next_bb = after->next_bb;
  135. b->prev_bb = after;
  136. after->next_bb = b;
  137. b->next_bb->prev_bb = b;
  138. }
  139. /* Unlink block B from chain. */
  140. void
  141. unlink_block (basic_block b)
  142. {
  143. b->next_bb->prev_bb = b->prev_bb;
  144. b->prev_bb->next_bb = b->next_bb;
  145. b->prev_bb = NULL;
  146. b->next_bb = NULL;
  147. }
  148. /* Sequentially order blocks and compact the arrays. */
  149. void
  150. compact_blocks (void)
  151. {
  152. int i;
  153. SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
  154. SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
  155. if (df)
  156. df_compact_blocks ();
  157. else
  158. {
  159. basic_block bb;
  160. i = NUM_FIXED_BLOCKS;
  161. FOR_EACH_BB_FN (bb, cfun)
  162. {
  163. SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
  164. bb->index = i;
  165. i++;
  166. }
  167. gcc_assert (i == n_basic_blocks_for_fn (cfun));
  168. for (; i < last_basic_block_for_fn (cfun); i++)
  169. SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
  170. }
  171. last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
  172. }
  173. /* Remove block B from the basic block array. */
  174. void
  175. expunge_block (basic_block b)
  176. {
  177. unlink_block (b);
  178. SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
  179. n_basic_blocks_for_fn (cfun)--;
  180. /* We should be able to ggc_free here, but we are not.
  181. The dead SSA_NAMES are left pointing to dead statements that are pointing
  182. to dead basic blocks making garbage collector to die.
  183. We should be able to release all dead SSA_NAMES and at the same time we should
  184. clear out BB pointer of dead statements consistently. */
  185. }
  186. /* Connect E to E->src. */
  187. static inline void
  188. connect_src (edge e)
  189. {
  190. vec_safe_push (e->src->succs, e);
  191. df_mark_solutions_dirty ();
  192. }
  193. /* Connect E to E->dest. */
  194. static inline void
  195. connect_dest (edge e)
  196. {
  197. basic_block dest = e->dest;
  198. vec_safe_push (dest->preds, e);
  199. e->dest_idx = EDGE_COUNT (dest->preds) - 1;
  200. df_mark_solutions_dirty ();
  201. }
  202. /* Disconnect edge E from E->src. */
  203. static inline void
  204. disconnect_src (edge e)
  205. {
  206. basic_block src = e->src;
  207. edge_iterator ei;
  208. edge tmp;
  209. for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
  210. {
  211. if (tmp == e)
  212. {
  213. src->succs->unordered_remove (ei.index);
  214. df_mark_solutions_dirty ();
  215. return;
  216. }
  217. else
  218. ei_next (&ei);
  219. }
  220. gcc_unreachable ();
  221. }
  222. /* Disconnect edge E from E->dest. */
  223. static inline void
  224. disconnect_dest (edge e)
  225. {
  226. basic_block dest = e->dest;
  227. unsigned int dest_idx = e->dest_idx;
  228. dest->preds->unordered_remove (dest_idx);
  229. /* If we removed an edge in the middle of the edge vector, we need
  230. to update dest_idx of the edge that moved into the "hole". */
  231. if (dest_idx < EDGE_COUNT (dest->preds))
  232. EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
  233. df_mark_solutions_dirty ();
  234. }
  235. /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
  236. created edge. Use this only if you are sure that this edge can't
  237. possibly already exist. */
  238. edge
  239. unchecked_make_edge (basic_block src, basic_block dst, int flags)
  240. {
  241. edge e;
  242. e = ggc_cleared_alloc<edge_def> ();
  243. n_edges_for_fn (cfun)++;
  244. e->src = src;
  245. e->dest = dst;
  246. e->flags = flags;
  247. connect_src (e);
  248. connect_dest (e);
  249. execute_on_growing_pred (e);
  250. return e;
  251. }
  252. /* Create an edge connecting SRC and DST with FLAGS optionally using
  253. edge cache CACHE. Return the new edge, NULL if already exist. */
  254. edge
  255. cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
  256. {
  257. if (edge_cache == NULL
  258. || src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  259. || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
  260. return make_edge (src, dst, flags);
  261. /* Does the requested edge already exist? */
  262. if (! bitmap_bit_p (edge_cache, dst->index))
  263. {
  264. /* The edge does not exist. Create one and update the
  265. cache. */
  266. bitmap_set_bit (edge_cache, dst->index);
  267. return unchecked_make_edge (src, dst, flags);
  268. }
  269. /* At this point, we know that the requested edge exists. Adjust
  270. flags if necessary. */
  271. if (flags)
  272. {
  273. edge e = find_edge (src, dst);
  274. e->flags |= flags;
  275. }
  276. return NULL;
  277. }
  278. /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
  279. created edge or NULL if already exist. */
  280. edge
  281. make_edge (basic_block src, basic_block dest, int flags)
  282. {
  283. edge e = find_edge (src, dest);
  284. /* Make sure we don't add duplicate edges. */
  285. if (e)
  286. {
  287. e->flags |= flags;
  288. return NULL;
  289. }
  290. return unchecked_make_edge (src, dest, flags);
  291. }
  292. /* Create an edge connecting SRC to DEST and set probability by knowing
  293. that it is the single edge leaving SRC. */
  294. edge
  295. make_single_succ_edge (basic_block src, basic_block dest, int flags)
  296. {
  297. edge e = make_edge (src, dest, flags);
  298. e->probability = REG_BR_PROB_BASE;
  299. e->count = src->count;
  300. return e;
  301. }
  302. /* This function will remove an edge from the flow graph. */
  303. void
  304. remove_edge_raw (edge e)
  305. {
  306. remove_predictions_associated_with_edge (e);
  307. execute_on_shrinking_pred (e);
  308. disconnect_src (e);
  309. disconnect_dest (e);
  310. free_edge (e);
  311. }
  312. /* Redirect an edge's successor from one block to another. */
  313. void
  314. redirect_edge_succ (edge e, basic_block new_succ)
  315. {
  316. execute_on_shrinking_pred (e);
  317. disconnect_dest (e);
  318. e->dest = new_succ;
  319. /* Reconnect the edge to the new successor block. */
  320. connect_dest (e);
  321. execute_on_growing_pred (e);
  322. }
  323. /* Redirect an edge's predecessor from one block to another. */
  324. void
  325. redirect_edge_pred (edge e, basic_block new_pred)
  326. {
  327. disconnect_src (e);
  328. e->src = new_pred;
  329. /* Reconnect the edge to the new predecessor block. */
  330. connect_src (e);
  331. }
  332. /* Clear all basic block flags that do not have to be preserved. */
  333. void
  334. clear_bb_flags (void)
  335. {
  336. basic_block bb;
  337. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
  338. bb->flags &= BB_FLAGS_TO_PRESERVE;
  339. }
  340. /* Check the consistency of profile information. We can't do that
  341. in verify_flow_info, as the counts may get invalid for incompletely
  342. solved graphs, later eliminating of conditionals or roundoff errors.
  343. It is still practical to have them reported for debugging of simple
  344. testcases. */
  345. static void
  346. check_bb_profile (basic_block bb, FILE * file, int indent, int flags)
  347. {
  348. edge e;
  349. int sum = 0;
  350. gcov_type lsum;
  351. edge_iterator ei;
  352. struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
  353. char *s_indent = (char *) alloca ((size_t) indent + 1);
  354. memset ((void *) s_indent, ' ', (size_t) indent);
  355. s_indent[indent] = '\0';
  356. if (profile_status_for_fn (fun) == PROFILE_ABSENT)
  357. return;
  358. if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
  359. {
  360. FOR_EACH_EDGE (e, ei, bb->succs)
  361. sum += e->probability;
  362. if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
  363. fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
  364. (flags & TDF_COMMENT) ? ";; " : "", s_indent,
  365. sum * 100.0 / REG_BR_PROB_BASE);
  366. lsum = 0;
  367. FOR_EACH_EDGE (e, ei, bb->succs)
  368. lsum += e->count;
  369. if (EDGE_COUNT (bb->succs)
  370. && (lsum - bb->count > 100 || lsum - bb->count < -100))
  371. fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
  372. (flags & TDF_COMMENT) ? ";; " : "", s_indent,
  373. (int) lsum, (int) bb->count);
  374. }
  375. if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
  376. {
  377. sum = 0;
  378. FOR_EACH_EDGE (e, ei, bb->preds)
  379. sum += EDGE_FREQUENCY (e);
  380. if (abs (sum - bb->frequency) > 100)
  381. fprintf (file,
  382. "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
  383. (flags & TDF_COMMENT) ? ";; " : "", s_indent,
  384. sum, bb->frequency);
  385. lsum = 0;
  386. FOR_EACH_EDGE (e, ei, bb->preds)
  387. lsum += e->count;
  388. if (lsum - bb->count > 100 || lsum - bb->count < -100)
  389. fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n",
  390. (flags & TDF_COMMENT) ? ";; " : "", s_indent,
  391. (int) lsum, (int) bb->count);
  392. }
  393. if (BB_PARTITION (bb) == BB_COLD_PARTITION)
  394. {
  395. /* Warn about inconsistencies in the partitioning that are
  396. currently caused by profile insanities created via optimization. */
  397. if (!probably_never_executed_bb_p (fun, bb))
  398. fprintf (file, "%s%sBlock in cold partition with hot count\n",
  399. (flags & TDF_COMMENT) ? ";; " : "", s_indent);
  400. FOR_EACH_EDGE (e, ei, bb->preds)
  401. {
  402. if (!probably_never_executed_edge_p (fun, e))
  403. fprintf (file,
  404. "%s%sBlock in cold partition with incoming hot edge\n",
  405. (flags & TDF_COMMENT) ? ";; " : "", s_indent);
  406. }
  407. }
  408. }
  409. void
  410. dump_edge_info (FILE *file, edge e, int flags, int do_succ)
  411. {
  412. basic_block side = (do_succ ? e->dest : e->src);
  413. bool do_details = false;
  414. if ((flags & TDF_DETAILS) != 0
  415. && (flags & TDF_SLIM) == 0)
  416. do_details = true;
  417. if (side->index == ENTRY_BLOCK)
  418. fputs (" ENTRY", file);
  419. else if (side->index == EXIT_BLOCK)
  420. fputs (" EXIT", file);
  421. else
  422. fprintf (file, " %d", side->index);
  423. if (e->probability && do_details)
  424. fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
  425. if (e->count && do_details)
  426. {
  427. fputs (" count:", file);
  428. fprintf (file, "%"PRId64, e->count);
  429. }
  430. if (e->flags && do_details)
  431. {
  432. static const char * const bitnames[] =
  433. {
  434. #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
  435. #include "cfg-flags.def"
  436. NULL
  437. #undef DEF_EDGE_FLAG
  438. };
  439. bool comma = false;
  440. int i, flags = e->flags;
  441. gcc_assert (e->flags <= EDGE_ALL_FLAGS);
  442. fputs (" (", file);
  443. for (i = 0; flags; i++)
  444. if (flags & (1 << i))
  445. {
  446. flags &= ~(1 << i);
  447. if (comma)
  448. fputc (',', file);
  449. fputs (bitnames[i], file);
  450. comma = true;
  451. }
  452. fputc (')', file);
  453. }
  454. }
  455. DEBUG_FUNCTION void
  456. debug (edge_def &ref)
  457. {
  458. /* FIXME (crowl): Is this desireable? */
  459. dump_edge_info (stderr, &ref, 0, false);
  460. dump_edge_info (stderr, &ref, 0, true);
  461. }
  462. DEBUG_FUNCTION void
  463. debug (edge_def *ptr)
  464. {
  465. if (ptr)
  466. debug (*ptr);
  467. else
  468. fprintf (stderr, "<nil>\n");
  469. }
  470. /* Simple routines to easily allocate AUX fields of basic blocks. */
  471. static struct obstack block_aux_obstack;
  472. static void *first_block_aux_obj = 0;
  473. static struct obstack edge_aux_obstack;
  474. static void *first_edge_aux_obj = 0;
  475. /* Allocate a memory block of SIZE as BB->aux. The obstack must
  476. be first initialized by alloc_aux_for_blocks. */
  477. static void
  478. alloc_aux_for_block (basic_block bb, int size)
  479. {
  480. /* Verify that aux field is clear. */
  481. gcc_assert (!bb->aux && first_block_aux_obj);
  482. bb->aux = obstack_alloc (&block_aux_obstack, size);
  483. memset (bb->aux, 0, size);
  484. }
  485. /* Initialize the block_aux_obstack and if SIZE is nonzero, call
  486. alloc_aux_for_block for each basic block. */
  487. void
  488. alloc_aux_for_blocks (int size)
  489. {
  490. static int initialized;
  491. if (!initialized)
  492. {
  493. gcc_obstack_init (&block_aux_obstack);
  494. initialized = 1;
  495. }
  496. else
  497. /* Check whether AUX data are still allocated. */
  498. gcc_assert (!first_block_aux_obj);
  499. first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
  500. if (size)
  501. {
  502. basic_block bb;
  503. FOR_ALL_BB_FN (bb, cfun)
  504. alloc_aux_for_block (bb, size);
  505. }
  506. }
  507. /* Clear AUX pointers of all blocks. */
  508. void
  509. clear_aux_for_blocks (void)
  510. {
  511. basic_block bb;
  512. FOR_ALL_BB_FN (bb, cfun)
  513. bb->aux = NULL;
  514. }
  515. /* Free data allocated in block_aux_obstack and clear AUX pointers
  516. of all blocks. */
  517. void
  518. free_aux_for_blocks (void)
  519. {
  520. gcc_assert (first_block_aux_obj);
  521. obstack_free (&block_aux_obstack, first_block_aux_obj);
  522. first_block_aux_obj = NULL;
  523. clear_aux_for_blocks ();
  524. }
  525. /* Allocate a memory edge of SIZE as E->aux. The obstack must
  526. be first initialized by alloc_aux_for_edges. */
  527. void
  528. alloc_aux_for_edge (edge e, int size)
  529. {
  530. /* Verify that aux field is clear. */
  531. gcc_assert (!e->aux && first_edge_aux_obj);
  532. e->aux = obstack_alloc (&edge_aux_obstack, size);
  533. memset (e->aux, 0, size);
  534. }
  535. /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
  536. alloc_aux_for_edge for each basic edge. */
  537. void
  538. alloc_aux_for_edges (int size)
  539. {
  540. static int initialized;
  541. if (!initialized)
  542. {
  543. gcc_obstack_init (&edge_aux_obstack);
  544. initialized = 1;
  545. }
  546. else
  547. /* Check whether AUX data are still allocated. */
  548. gcc_assert (!first_edge_aux_obj);
  549. first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
  550. if (size)
  551. {
  552. basic_block bb;
  553. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  554. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  555. {
  556. edge e;
  557. edge_iterator ei;
  558. FOR_EACH_EDGE (e, ei, bb->succs)
  559. alloc_aux_for_edge (e, size);
  560. }
  561. }
  562. }
  563. /* Clear AUX pointers of all edges. */
  564. void
  565. clear_aux_for_edges (void)
  566. {
  567. basic_block bb;
  568. edge e;
  569. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  570. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  571. {
  572. edge_iterator ei;
  573. FOR_EACH_EDGE (e, ei, bb->succs)
  574. e->aux = NULL;
  575. }
  576. }
  577. /* Free data allocated in edge_aux_obstack and clear AUX pointers
  578. of all edges. */
  579. void
  580. free_aux_for_edges (void)
  581. {
  582. gcc_assert (first_edge_aux_obj);
  583. obstack_free (&edge_aux_obstack, first_edge_aux_obj);
  584. first_edge_aux_obj = NULL;
  585. clear_aux_for_edges ();
  586. }
  587. DEBUG_FUNCTION void
  588. debug_bb (basic_block bb)
  589. {
  590. dump_bb (stderr, bb, 0, dump_flags);
  591. }
  592. DEBUG_FUNCTION basic_block
  593. debug_bb_n (int n)
  594. {
  595. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
  596. debug_bb (bb);
  597. return bb;
  598. }
  599. /* Dumps cfg related information about basic block BB to OUTF.
  600. If HEADER is true, dump things that appear before the instructions
  601. contained in BB. If FOOTER is true, dump things that appear after.
  602. Flags are the TDF_* masks as documented in dumpfile.h.
  603. NB: With TDF_DETAILS, it is assumed that cfun is available, so
  604. that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
  605. void
  606. dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
  607. bool do_header, bool do_footer)
  608. {
  609. edge_iterator ei;
  610. edge e;
  611. static const char * const bb_bitnames[] =
  612. {
  613. #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
  614. #include "cfg-flags.def"
  615. NULL
  616. #undef DEF_BASIC_BLOCK_FLAG
  617. };
  618. const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
  619. bool first;
  620. char *s_indent = (char *) alloca ((size_t) indent + 1);
  621. memset ((void *) s_indent, ' ', (size_t) indent);
  622. s_indent[indent] = '\0';
  623. gcc_assert (bb->flags <= BB_ALL_FLAGS);
  624. if (do_header)
  625. {
  626. unsigned i;
  627. if (flags & TDF_COMMENT)
  628. fputs (";; ", outf);
  629. fprintf (outf, "%sbasic block %d, loop depth %d",
  630. s_indent, bb->index, bb_loop_depth (bb));
  631. if (flags & TDF_DETAILS)
  632. {
  633. struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
  634. fprintf (outf, ", count " "%"PRId64,
  635. (int64_t) bb->count);
  636. fprintf (outf, ", freq %i", bb->frequency);
  637. if (maybe_hot_bb_p (fun, bb))
  638. fputs (", maybe hot", outf);
  639. if (probably_never_executed_bb_p (fun, bb))
  640. fputs (", probably never executed", outf);
  641. }
  642. fputc ('\n', outf);
  643. if (flags & TDF_DETAILS)
  644. {
  645. check_bb_profile (bb, outf, indent, flags);
  646. if (flags & TDF_COMMENT)
  647. fputs (";; ", outf);
  648. fprintf (outf, "%s prev block ", s_indent);
  649. if (bb->prev_bb)
  650. fprintf (outf, "%d", bb->prev_bb->index);
  651. else
  652. fprintf (outf, "(nil)");
  653. fprintf (outf, ", next block ");
  654. if (bb->next_bb)
  655. fprintf (outf, "%d", bb->next_bb->index);
  656. else
  657. fprintf (outf, "(nil)");
  658. fputs (", flags:", outf);
  659. first = true;
  660. for (i = 0; i < n_bitnames; i++)
  661. if (bb->flags & (1 << i))
  662. {
  663. if (first)
  664. fputs (" (", outf);
  665. else
  666. fputs (", ", outf);
  667. first = false;
  668. fputs (bb_bitnames[i], outf);
  669. }
  670. if (!first)
  671. fputc (')', outf);
  672. fputc ('\n', outf);
  673. }
  674. if (flags & TDF_COMMENT)
  675. fputs (";; ", outf);
  676. fprintf (outf, "%s pred: ", s_indent);
  677. first = true;
  678. FOR_EACH_EDGE (e, ei, bb->preds)
  679. {
  680. if (! first)
  681. {
  682. if (flags & TDF_COMMENT)
  683. fputs (";; ", outf);
  684. fprintf (outf, "%s ", s_indent);
  685. }
  686. first = false;
  687. dump_edge_info (outf, e, flags, 0);
  688. fputc ('\n', outf);
  689. }
  690. if (first)
  691. fputc ('\n', outf);
  692. }
  693. if (do_footer)
  694. {
  695. if (flags & TDF_COMMENT)
  696. fputs (";; ", outf);
  697. fprintf (outf, "%s succ: ", s_indent);
  698. first = true;
  699. FOR_EACH_EDGE (e, ei, bb->succs)
  700. {
  701. if (! first)
  702. {
  703. if (flags & TDF_COMMENT)
  704. fputs (";; ", outf);
  705. fprintf (outf, "%s ", s_indent);
  706. }
  707. first = false;
  708. dump_edge_info (outf, e, flags, 1);
  709. fputc ('\n', outf);
  710. }
  711. if (first)
  712. fputc ('\n', outf);
  713. }
  714. }
  715. /* Dumps a brief description of cfg to FILE. */
  716. void
  717. brief_dump_cfg (FILE *file, int flags)
  718. {
  719. basic_block bb;
  720. FOR_EACH_BB_FN (bb, cfun)
  721. {
  722. dump_bb_info (file, bb, 0,
  723. flags & (TDF_COMMENT | TDF_DETAILS),
  724. true, true);
  725. }
  726. }
  727. /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
  728. leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
  729. redirected to destination of TAKEN_EDGE.
  730. This function may leave the profile inconsistent in the case TAKEN_EDGE
  731. frequency or count is believed to be lower than FREQUENCY or COUNT
  732. respectively. */
  733. void
  734. update_bb_profile_for_threading (basic_block bb, int edge_frequency,
  735. gcov_type count, edge taken_edge)
  736. {
  737. edge c;
  738. int prob;
  739. edge_iterator ei;
  740. bb->count -= count;
  741. if (bb->count < 0)
  742. {
  743. if (dump_file)
  744. fprintf (dump_file, "bb %i count became negative after threading",
  745. bb->index);
  746. bb->count = 0;
  747. }
  748. /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
  749. Watch for overflows. */
  750. if (bb->frequency)
  751. prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
  752. else
  753. prob = 0;
  754. if (prob > taken_edge->probability)
  755. {
  756. if (dump_file)
  757. fprintf (dump_file, "Jump threading proved probability of edge "
  758. "%i->%i too small (it is %i, should be %i).\n",
  759. taken_edge->src->index, taken_edge->dest->index,
  760. taken_edge->probability, prob);
  761. prob = taken_edge->probability;
  762. }
  763. /* Now rescale the probabilities. */
  764. taken_edge->probability -= prob;
  765. prob = REG_BR_PROB_BASE - prob;
  766. bb->frequency -= edge_frequency;
  767. if (bb->frequency < 0)
  768. bb->frequency = 0;
  769. if (prob <= 0)
  770. {
  771. if (dump_file)
  772. fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
  773. "frequency of block should end up being 0, it is %i\n",
  774. bb->index, bb->frequency);
  775. EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
  776. ei = ei_start (bb->succs);
  777. ei_next (&ei);
  778. for (; (c = ei_safe_edge (ei)); ei_next (&ei))
  779. c->probability = 0;
  780. }
  781. else if (prob != REG_BR_PROB_BASE)
  782. {
  783. int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
  784. FOR_EACH_EDGE (c, ei, bb->succs)
  785. {
  786. /* Protect from overflow due to additional scaling. */
  787. if (c->probability > prob)
  788. c->probability = REG_BR_PROB_BASE;
  789. else
  790. {
  791. c->probability = RDIV (c->probability * scale, 65536);
  792. if (c->probability > REG_BR_PROB_BASE)
  793. c->probability = REG_BR_PROB_BASE;
  794. }
  795. }
  796. }
  797. gcc_assert (bb == taken_edge->src);
  798. taken_edge->count -= count;
  799. if (taken_edge->count < 0)
  800. {
  801. if (dump_file)
  802. fprintf (dump_file, "edge %i->%i count became negative after threading",
  803. taken_edge->src->index, taken_edge->dest->index);
  804. taken_edge->count = 0;
  805. }
  806. }
  807. /* Multiply all frequencies of basic blocks in array BBS of length NBBS
  808. by NUM/DEN, in int arithmetic. May lose some accuracy. */
  809. void
  810. scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
  811. {
  812. int i;
  813. edge e;
  814. if (num < 0)
  815. num = 0;
  816. /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
  817. 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
  818. and still safely fit in int during calculations. */
  819. if (den > 1000)
  820. {
  821. if (num > 1000000)
  822. return;
  823. num = RDIV (1000 * num, den);
  824. den = 1000;
  825. }
  826. if (num > 100 * den)
  827. return;
  828. for (i = 0; i < nbbs; i++)
  829. {
  830. edge_iterator ei;
  831. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  832. /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
  833. if (bbs[i]->frequency > BB_FREQ_MAX)
  834. bbs[i]->frequency = BB_FREQ_MAX;
  835. bbs[i]->count = RDIV (bbs[i]->count * num, den);
  836. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  837. e->count = RDIV (e->count * num, den);
  838. }
  839. }
  840. /* numbers smaller than this value are safe to multiply without getting
  841. 64bit overflow. */
  842. #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
  843. /* Multiply all frequencies of basic blocks in array BBS of length NBBS
  844. by NUM/DEN, in gcov_type arithmetic. More accurate than previous
  845. function but considerably slower. */
  846. void
  847. scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
  848. gcov_type den)
  849. {
  850. int i;
  851. edge e;
  852. gcov_type fraction = RDIV (num * 65536, den);
  853. gcc_assert (fraction >= 0);
  854. if (num < MAX_SAFE_MULTIPLIER)
  855. for (i = 0; i < nbbs; i++)
  856. {
  857. edge_iterator ei;
  858. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  859. if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
  860. bbs[i]->count = RDIV (bbs[i]->count * num, den);
  861. else
  862. bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
  863. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  864. if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
  865. e->count = RDIV (e->count * num, den);
  866. else
  867. e->count = RDIV (e->count * fraction, 65536);
  868. }
  869. else
  870. for (i = 0; i < nbbs; i++)
  871. {
  872. edge_iterator ei;
  873. if (sizeof (gcov_type) > sizeof (int))
  874. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  875. else
  876. bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
  877. bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
  878. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  879. e->count = RDIV (e->count * fraction, 65536);
  880. }
  881. }
  882. /* Helper types for hash tables. */
  883. struct htab_bb_copy_original_entry
  884. {
  885. /* Block we are attaching info to. */
  886. int index1;
  887. /* Index of original or copy (depending on the hashtable) */
  888. int index2;
  889. };
  890. struct bb_copy_hasher : typed_noop_remove <htab_bb_copy_original_entry>
  891. {
  892. typedef htab_bb_copy_original_entry value_type;
  893. typedef htab_bb_copy_original_entry compare_type;
  894. static inline hashval_t hash (const value_type *);
  895. static inline bool equal (const value_type *existing,
  896. const compare_type * candidate);
  897. };
  898. inline hashval_t
  899. bb_copy_hasher::hash (const value_type *data)
  900. {
  901. return data->index1;
  902. }
  903. inline bool
  904. bb_copy_hasher::equal (const value_type *data, const compare_type *data2)
  905. {
  906. return data->index1 == data2->index1;
  907. }
  908. /* Data structures used to maintain mapping between basic blocks and
  909. copies. */
  910. static hash_table<bb_copy_hasher> *bb_original;
  911. static hash_table<bb_copy_hasher> *bb_copy;
  912. /* And between loops and copies. */
  913. static hash_table<bb_copy_hasher> *loop_copy;
  914. static alloc_pool original_copy_bb_pool;
  915. /* Initialize the data structures to maintain mapping between blocks
  916. and its copies. */
  917. void
  918. initialize_original_copy_tables (void)
  919. {
  920. gcc_assert (!original_copy_bb_pool);
  921. original_copy_bb_pool
  922. = create_alloc_pool ("original_copy",
  923. sizeof (struct htab_bb_copy_original_entry), 10);
  924. bb_original = new hash_table<bb_copy_hasher> (10);
  925. bb_copy = new hash_table<bb_copy_hasher> (10);
  926. loop_copy = new hash_table<bb_copy_hasher> (10);
  927. }
  928. /* Free the data structures to maintain mapping between blocks and
  929. its copies. */
  930. void
  931. free_original_copy_tables (void)
  932. {
  933. gcc_assert (original_copy_bb_pool);
  934. delete bb_copy;
  935. bb_copy = NULL;
  936. delete bb_original;
  937. bb_copy = NULL;
  938. delete loop_copy;
  939. loop_copy = NULL;
  940. free_alloc_pool (original_copy_bb_pool);
  941. original_copy_bb_pool = NULL;
  942. }
  943. /* Removes the value associated with OBJ from table TAB. */
  944. static void
  945. copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
  946. {
  947. htab_bb_copy_original_entry **slot;
  948. struct htab_bb_copy_original_entry key, *elt;
  949. if (!original_copy_bb_pool)
  950. return;
  951. key.index1 = obj;
  952. slot = tab->find_slot (&key, NO_INSERT);
  953. if (!slot)
  954. return;
  955. elt = *slot;
  956. tab->clear_slot (slot);
  957. pool_free (original_copy_bb_pool, elt);
  958. }
  959. /* Sets the value associated with OBJ in table TAB to VAL.
  960. Do nothing when data structures are not initialized. */
  961. static void
  962. copy_original_table_set (hash_table<bb_copy_hasher> *tab,
  963. unsigned obj, unsigned val)
  964. {
  965. struct htab_bb_copy_original_entry **slot;
  966. struct htab_bb_copy_original_entry key;
  967. if (!original_copy_bb_pool)
  968. return;
  969. key.index1 = obj;
  970. slot = tab->find_slot (&key, INSERT);
  971. if (!*slot)
  972. {
  973. *slot = (struct htab_bb_copy_original_entry *)
  974. pool_alloc (original_copy_bb_pool);
  975. (*slot)->index1 = obj;
  976. }
  977. (*slot)->index2 = val;
  978. }
  979. /* Set original for basic block. Do nothing when data structures are not
  980. initialized so passes not needing this don't need to care. */
  981. void
  982. set_bb_original (basic_block bb, basic_block original)
  983. {
  984. copy_original_table_set (bb_original, bb->index, original->index);
  985. }
  986. /* Get the original basic block. */
  987. basic_block
  988. get_bb_original (basic_block bb)
  989. {
  990. struct htab_bb_copy_original_entry *entry;
  991. struct htab_bb_copy_original_entry key;
  992. gcc_assert (original_copy_bb_pool);
  993. key.index1 = bb->index;
  994. entry = bb_original->find (&key);
  995. if (entry)
  996. return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
  997. else
  998. return NULL;
  999. }
  1000. /* Set copy for basic block. Do nothing when data structures are not
  1001. initialized so passes not needing this don't need to care. */
  1002. void
  1003. set_bb_copy (basic_block bb, basic_block copy)
  1004. {
  1005. copy_original_table_set (bb_copy, bb->index, copy->index);
  1006. }
  1007. /* Get the copy of basic block. */
  1008. basic_block
  1009. get_bb_copy (basic_block bb)
  1010. {
  1011. struct htab_bb_copy_original_entry *entry;
  1012. struct htab_bb_copy_original_entry key;
  1013. gcc_assert (original_copy_bb_pool);
  1014. key.index1 = bb->index;
  1015. entry = bb_copy->find (&key);
  1016. if (entry)
  1017. return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
  1018. else
  1019. return NULL;
  1020. }
  1021. /* Set copy for LOOP to COPY. Do nothing when data structures are not
  1022. initialized so passes not needing this don't need to care. */
  1023. void
  1024. set_loop_copy (struct loop *loop, struct loop *copy)
  1025. {
  1026. if (!copy)
  1027. copy_original_table_clear (loop_copy, loop->num);
  1028. else
  1029. copy_original_table_set (loop_copy, loop->num, copy->num);
  1030. }
  1031. /* Get the copy of LOOP. */
  1032. struct loop *
  1033. get_loop_copy (struct loop *loop)
  1034. {
  1035. struct htab_bb_copy_original_entry *entry;
  1036. struct htab_bb_copy_original_entry key;
  1037. gcc_assert (original_copy_bb_pool);
  1038. key.index1 = loop->num;
  1039. entry = loop_copy->find (&key);
  1040. if (entry)
  1041. return get_loop (cfun, entry->index2);
  1042. else
  1043. return NULL;
  1044. }