PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/freebsd/contrib/gcc/cfg.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 1156 lines | 840 code | 162 blank | 154 comment | 136 complexity | 8b99e643c97d503b35736c06e08e8df2 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* Control flow graph manipulation code for GNU compiler.
  2. Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  3. 1999, 2000, 2001, 2002, 2003, 2004, 2005
  4. Free Software Foundation, Inc.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING. If not, write to the Free
  16. Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This file contains low level functions to manipulate the CFG and
  19. analyze it. All other modules should not transform the data structure
  20. directly and use abstraction instead. The file is supposed to be
  21. ordered bottom-up and should not contain any code dependent on a
  22. particular intermediate language (RTL or trees).
  23. Available functionality:
  24. - Initialization/deallocation
  25. init_flow, clear_edges
  26. - Low level basic block manipulation
  27. alloc_block, expunge_block
  28. - Edge manipulation
  29. make_edge, make_single_succ_edge, cached_make_edge, remove_edge
  30. - Low level edge redirection (without updating instruction chain)
  31. redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
  32. - Dumping and debugging
  33. dump_flow_info, debug_flow_info, dump_edge_info
  34. - Allocation of AUX fields for basic blocks
  35. alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
  36. - clear_bb_flags
  37. - Consistency checking
  38. verify_flow_info
  39. - Dumping and debugging
  40. print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
  41. */
  42. #include "config.h"
  43. #include "system.h"
  44. #include "coretypes.h"
  45. #include "tm.h"
  46. #include "tree.h"
  47. #include "rtl.h"
  48. #include "hard-reg-set.h"
  49. #include "regs.h"
  50. #include "flags.h"
  51. #include "output.h"
  52. #include "function.h"
  53. #include "except.h"
  54. #include "toplev.h"
  55. #include "tm_p.h"
  56. #include "obstack.h"
  57. #include "timevar.h"
  58. #include "tree-pass.h"
  59. #include "ggc.h"
  60. #include "hashtab.h"
  61. #include "alloc-pool.h"
  62. /* The obstack on which the flow graph components are allocated. */
  63. struct bitmap_obstack reg_obstack;
  64. void debug_flow_info (void);
  65. static void free_edge (edge);
  66. #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
  67. /* Called once at initialization time. */
  68. void
  69. init_flow (void)
  70. {
  71. if (!cfun->cfg)
  72. cfun->cfg = ggc_alloc_cleared (sizeof (struct control_flow_graph));
  73. n_edges = 0;
  74. ENTRY_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
  75. ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
  76. EXIT_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
  77. EXIT_BLOCK_PTR->index = EXIT_BLOCK;
  78. ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
  79. EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
  80. }
  81. /* Helper function for remove_edge and clear_edges. Frees edge structure
  82. without actually unlinking it from the pred/succ lists. */
  83. static void
  84. free_edge (edge e ATTRIBUTE_UNUSED)
  85. {
  86. n_edges--;
  87. ggc_free (e);
  88. }
  89. /* Free the memory associated with the edge structures. */
  90. void
  91. clear_edges (void)
  92. {
  93. basic_block bb;
  94. edge e;
  95. edge_iterator ei;
  96. FOR_EACH_BB (bb)
  97. {
  98. FOR_EACH_EDGE (e, ei, bb->succs)
  99. free_edge (e);
  100. VEC_truncate (edge, bb->succs, 0);
  101. VEC_truncate (edge, bb->preds, 0);
  102. }
  103. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
  104. free_edge (e);
  105. VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
  106. VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
  107. gcc_assert (!n_edges);
  108. }
  109. /* Allocate memory for basic_block. */
  110. basic_block
  111. alloc_block (void)
  112. {
  113. basic_block bb;
  114. bb = ggc_alloc_cleared (sizeof (*bb));
  115. return bb;
  116. }
  117. /* Link block B to chain after AFTER. */
  118. void
  119. link_block (basic_block b, basic_block after)
  120. {
  121. b->next_bb = after->next_bb;
  122. b->prev_bb = after;
  123. after->next_bb = b;
  124. b->next_bb->prev_bb = b;
  125. }
  126. /* Unlink block B from chain. */
  127. void
  128. unlink_block (basic_block b)
  129. {
  130. b->next_bb->prev_bb = b->prev_bb;
  131. b->prev_bb->next_bb = b->next_bb;
  132. b->prev_bb = NULL;
  133. b->next_bb = NULL;
  134. }
  135. /* Sequentially order blocks and compact the arrays. */
  136. void
  137. compact_blocks (void)
  138. {
  139. int i;
  140. basic_block bb;
  141. SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR);
  142. SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR);
  143. i = NUM_FIXED_BLOCKS;
  144. FOR_EACH_BB (bb)
  145. {
  146. SET_BASIC_BLOCK (i, bb);
  147. bb->index = i;
  148. i++;
  149. }
  150. gcc_assert (i == n_basic_blocks);
  151. for (; i < last_basic_block; i++)
  152. SET_BASIC_BLOCK (i, NULL);
  153. last_basic_block = n_basic_blocks;
  154. }
  155. /* Remove block B from the basic block array. */
  156. void
  157. expunge_block (basic_block b)
  158. {
  159. unlink_block (b);
  160. SET_BASIC_BLOCK (b->index, NULL);
  161. n_basic_blocks--;
  162. /* We should be able to ggc_free here, but we are not.
  163. The dead SSA_NAMES are left pointing to dead statements that are pointing
  164. to dead basic blocks making garbage collector to die.
  165. We should be able to release all dead SSA_NAMES and at the same time we should
  166. clear out BB pointer of dead statements consistently. */
  167. }
  168. /* Connect E to E->src. */
  169. static inline void
  170. connect_src (edge e)
  171. {
  172. VEC_safe_push (edge, gc, e->src->succs, e);
  173. }
  174. /* Connect E to E->dest. */
  175. static inline void
  176. connect_dest (edge e)
  177. {
  178. basic_block dest = e->dest;
  179. VEC_safe_push (edge, gc, dest->preds, e);
  180. e->dest_idx = EDGE_COUNT (dest->preds) - 1;
  181. }
  182. /* Disconnect edge E from E->src. */
  183. static inline void
  184. disconnect_src (edge e)
  185. {
  186. basic_block src = e->src;
  187. edge_iterator ei;
  188. edge tmp;
  189. for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
  190. {
  191. if (tmp == e)
  192. {
  193. VEC_unordered_remove (edge, src->succs, ei.index);
  194. return;
  195. }
  196. else
  197. ei_next (&ei);
  198. }
  199. gcc_unreachable ();
  200. }
  201. /* Disconnect edge E from E->dest. */
  202. static inline void
  203. disconnect_dest (edge e)
  204. {
  205. basic_block dest = e->dest;
  206. unsigned int dest_idx = e->dest_idx;
  207. VEC_unordered_remove (edge, dest->preds, dest_idx);
  208. /* If we removed an edge in the middle of the edge vector, we need
  209. to update dest_idx of the edge that moved into the "hole". */
  210. if (dest_idx < EDGE_COUNT (dest->preds))
  211. EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
  212. }
  213. /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
  214. created edge. Use this only if you are sure that this edge can't
  215. possibly already exist. */
  216. edge
  217. unchecked_make_edge (basic_block src, basic_block dst, int flags)
  218. {
  219. edge e;
  220. e = ggc_alloc_cleared (sizeof (*e));
  221. n_edges++;
  222. e->src = src;
  223. e->dest = dst;
  224. e->flags = flags;
  225. connect_src (e);
  226. connect_dest (e);
  227. execute_on_growing_pred (e);
  228. return e;
  229. }
  230. /* Create an edge connecting SRC and DST with FLAGS optionally using
  231. edge cache CACHE. Return the new edge, NULL if already exist. */
  232. edge
  233. cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
  234. {
  235. if (edge_cache == NULL
  236. || src == ENTRY_BLOCK_PTR
  237. || dst == EXIT_BLOCK_PTR)
  238. return make_edge (src, dst, flags);
  239. /* Does the requested edge already exist? */
  240. if (! TEST_BIT (edge_cache, dst->index))
  241. {
  242. /* The edge does not exist. Create one and update the
  243. cache. */
  244. SET_BIT (edge_cache, dst->index);
  245. return unchecked_make_edge (src, dst, flags);
  246. }
  247. /* At this point, we know that the requested edge exists. Adjust
  248. flags if necessary. */
  249. if (flags)
  250. {
  251. edge e = find_edge (src, dst);
  252. e->flags |= flags;
  253. }
  254. return NULL;
  255. }
  256. /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
  257. created edge or NULL if already exist. */
  258. edge
  259. make_edge (basic_block src, basic_block dest, int flags)
  260. {
  261. edge e = find_edge (src, dest);
  262. /* Make sure we don't add duplicate edges. */
  263. if (e)
  264. {
  265. e->flags |= flags;
  266. return NULL;
  267. }
  268. return unchecked_make_edge (src, dest, flags);
  269. }
  270. /* Create an edge connecting SRC to DEST and set probability by knowing
  271. that it is the single edge leaving SRC. */
  272. edge
  273. make_single_succ_edge (basic_block src, basic_block dest, int flags)
  274. {
  275. edge e = make_edge (src, dest, flags);
  276. e->probability = REG_BR_PROB_BASE;
  277. e->count = src->count;
  278. return e;
  279. }
  280. /* This function will remove an edge from the flow graph. */
  281. void
  282. remove_edge (edge e)
  283. {
  284. remove_predictions_associated_with_edge (e);
  285. execute_on_shrinking_pred (e);
  286. disconnect_src (e);
  287. disconnect_dest (e);
  288. free_edge (e);
  289. }
  290. /* Redirect an edge's successor from one block to another. */
  291. void
  292. redirect_edge_succ (edge e, basic_block new_succ)
  293. {
  294. execute_on_shrinking_pred (e);
  295. disconnect_dest (e);
  296. e->dest = new_succ;
  297. /* Reconnect the edge to the new successor block. */
  298. connect_dest (e);
  299. execute_on_growing_pred (e);
  300. }
  301. /* Like previous but avoid possible duplicate edge. */
  302. edge
  303. redirect_edge_succ_nodup (edge e, basic_block new_succ)
  304. {
  305. edge s;
  306. s = find_edge (e->src, new_succ);
  307. if (s && s != e)
  308. {
  309. s->flags |= e->flags;
  310. s->probability += e->probability;
  311. if (s->probability > REG_BR_PROB_BASE)
  312. s->probability = REG_BR_PROB_BASE;
  313. s->count += e->count;
  314. remove_edge (e);
  315. e = s;
  316. }
  317. else
  318. redirect_edge_succ (e, new_succ);
  319. return e;
  320. }
  321. /* Redirect an edge's predecessor from one block to another. */
  322. void
  323. redirect_edge_pred (edge e, basic_block new_pred)
  324. {
  325. disconnect_src (e);
  326. e->src = new_pred;
  327. /* Reconnect the edge to the new predecessor block. */
  328. connect_src (e);
  329. }
  330. /* Clear all basic block flags, with the exception of partitioning. */
  331. void
  332. clear_bb_flags (void)
  333. {
  334. basic_block bb;
  335. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
  336. bb->flags = (BB_PARTITION (bb) | (bb->flags & BB_DISABLE_SCHEDULE)
  337. | (bb->flags & BB_RTL));
  338. }
  339. /* Check the consistency of profile information. We can't do that
  340. in verify_flow_info, as the counts may get invalid for incompletely
  341. solved graphs, later eliminating of conditionals or roundoff errors.
  342. It is still practical to have them reported for debugging of simple
  343. testcases. */
  344. void
  345. check_bb_profile (basic_block bb, FILE * file)
  346. {
  347. edge e;
  348. int sum = 0;
  349. gcov_type lsum;
  350. edge_iterator ei;
  351. if (profile_status == PROFILE_ABSENT)
  352. return;
  353. if (bb != EXIT_BLOCK_PTR)
  354. {
  355. FOR_EACH_EDGE (e, ei, bb->succs)
  356. sum += e->probability;
  357. if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
  358. fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
  359. sum * 100.0 / REG_BR_PROB_BASE);
  360. lsum = 0;
  361. FOR_EACH_EDGE (e, ei, bb->succs)
  362. lsum += e->count;
  363. if (EDGE_COUNT (bb->succs)
  364. && (lsum - bb->count > 100 || lsum - bb->count < -100))
  365. fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
  366. (int) lsum, (int) bb->count);
  367. }
  368. if (bb != ENTRY_BLOCK_PTR)
  369. {
  370. sum = 0;
  371. FOR_EACH_EDGE (e, ei, bb->preds)
  372. sum += EDGE_FREQUENCY (e);
  373. if (abs (sum - bb->frequency) > 100)
  374. fprintf (file,
  375. "Invalid sum of incoming frequencies %i, should be %i\n",
  376. sum, bb->frequency);
  377. lsum = 0;
  378. FOR_EACH_EDGE (e, ei, bb->preds)
  379. lsum += e->count;
  380. if (lsum - bb->count > 100 || lsum - bb->count < -100)
  381. fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
  382. (int) lsum, (int) bb->count);
  383. }
  384. }
  385. /* Emit basic block information for BB. HEADER is true if the user wants
  386. the generic information and the predecessors, FOOTER is true if they want
  387. the successors. FLAGS is the dump flags of interest; TDF_DETAILS emit
  388. global register liveness information. PREFIX is put in front of every
  389. line. The output is emitted to FILE. */
  390. void
  391. dump_bb_info (basic_block bb, bool header, bool footer, int flags,
  392. const char *prefix, FILE *file)
  393. {
  394. edge e;
  395. edge_iterator ei;
  396. if (header)
  397. {
  398. fprintf (file, "\n%sBasic block %d ", prefix, bb->index);
  399. if (bb->prev_bb)
  400. fprintf (file, ", prev %d", bb->prev_bb->index);
  401. if (bb->next_bb)
  402. fprintf (file, ", next %d", bb->next_bb->index);
  403. fprintf (file, ", loop_depth %d, count ", bb->loop_depth);
  404. fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
  405. fprintf (file, ", freq %i", bb->frequency);
  406. if (maybe_hot_bb_p (bb))
  407. fprintf (file, ", maybe hot");
  408. if (probably_never_executed_bb_p (bb))
  409. fprintf (file, ", probably never executed");
  410. fprintf (file, ".\n");
  411. fprintf (file, "%sPredecessors: ", prefix);
  412. FOR_EACH_EDGE (e, ei, bb->preds)
  413. dump_edge_info (file, e, 0);
  414. }
  415. if (footer)
  416. {
  417. fprintf (file, "\n%sSuccessors: ", prefix);
  418. FOR_EACH_EDGE (e, ei, bb->succs)
  419. dump_edge_info (file, e, 1);
  420. }
  421. if ((flags & TDF_DETAILS)
  422. && (bb->flags & BB_RTL))
  423. {
  424. if (bb->il.rtl->global_live_at_start && header)
  425. {
  426. fprintf (file, "\n%sRegisters live at start:", prefix);
  427. dump_regset (bb->il.rtl->global_live_at_start, file);
  428. }
  429. if (bb->il.rtl->global_live_at_end && footer)
  430. {
  431. fprintf (file, "\n%sRegisters live at end:", prefix);
  432. dump_regset (bb->il.rtl->global_live_at_end, file);
  433. }
  434. }
  435. putc ('\n', file);
  436. }
  437. void
  438. dump_flow_info (FILE *file, int flags)
  439. {
  440. basic_block bb;
  441. /* There are no pseudo registers after reload. Don't dump them. */
  442. if (reg_n_info && !reload_completed
  443. && (flags & TDF_DETAILS) != 0)
  444. {
  445. unsigned int i, max = max_reg_num ();
  446. fprintf (file, "%d registers.\n", max);
  447. for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
  448. if (REG_N_REFS (i))
  449. {
  450. enum reg_class class, altclass;
  451. fprintf (file, "\nRegister %d used %d times across %d insns",
  452. i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
  453. if (REG_BASIC_BLOCK (i) >= 0)
  454. fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
  455. if (REG_N_SETS (i))
  456. fprintf (file, "; set %d time%s", REG_N_SETS (i),
  457. (REG_N_SETS (i) == 1) ? "" : "s");
  458. if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
  459. fprintf (file, "; user var");
  460. if (REG_N_DEATHS (i) != 1)
  461. fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
  462. if (REG_N_CALLS_CROSSED (i) == 1)
  463. fprintf (file, "; crosses 1 call");
  464. else if (REG_N_CALLS_CROSSED (i))
  465. fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
  466. if (regno_reg_rtx[i] != NULL
  467. && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
  468. fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
  469. class = reg_preferred_class (i);
  470. altclass = reg_alternate_class (i);
  471. if (class != GENERAL_REGS || altclass != ALL_REGS)
  472. {
  473. if (altclass == ALL_REGS || class == ALL_REGS)
  474. fprintf (file, "; pref %s", reg_class_names[(int) class]);
  475. else if (altclass == NO_REGS)
  476. fprintf (file, "; %s or none", reg_class_names[(int) class]);
  477. else
  478. fprintf (file, "; pref %s, else %s",
  479. reg_class_names[(int) class],
  480. reg_class_names[(int) altclass]);
  481. }
  482. if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
  483. fprintf (file, "; pointer");
  484. fprintf (file, ".\n");
  485. }
  486. }
  487. fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
  488. FOR_EACH_BB (bb)
  489. {
  490. dump_bb_info (bb, true, true, flags, "", file);
  491. check_bb_profile (bb, file);
  492. }
  493. putc ('\n', file);
  494. }
  495. void
  496. debug_flow_info (void)
  497. {
  498. dump_flow_info (stderr, TDF_DETAILS);
  499. }
  500. void
  501. dump_edge_info (FILE *file, edge e, int do_succ)
  502. {
  503. basic_block side = (do_succ ? e->dest : e->src);
  504. if (side == ENTRY_BLOCK_PTR)
  505. fputs (" ENTRY", file);
  506. else if (side == EXIT_BLOCK_PTR)
  507. fputs (" EXIT", file);
  508. else
  509. fprintf (file, " %d", side->index);
  510. if (e->probability)
  511. fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
  512. if (e->count)
  513. {
  514. fprintf (file, " count:");
  515. fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
  516. }
  517. if (e->flags)
  518. {
  519. static const char * const bitnames[] = {
  520. "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
  521. "can_fallthru", "irreducible", "sibcall", "loop_exit",
  522. "true", "false", "exec"
  523. };
  524. int comma = 0;
  525. int i, flags = e->flags;
  526. fputs (" (", file);
  527. for (i = 0; flags; i++)
  528. if (flags & (1 << i))
  529. {
  530. flags &= ~(1 << i);
  531. if (comma)
  532. fputc (',', file);
  533. if (i < (int) ARRAY_SIZE (bitnames))
  534. fputs (bitnames[i], file);
  535. else
  536. fprintf (file, "%d", i);
  537. comma = 1;
  538. }
  539. fputc (')', file);
  540. }
  541. }
  542. /* Simple routines to easily allocate AUX fields of basic blocks. */
  543. static struct obstack block_aux_obstack;
  544. static void *first_block_aux_obj = 0;
  545. static struct obstack edge_aux_obstack;
  546. static void *first_edge_aux_obj = 0;
  547. /* Allocate a memory block of SIZE as BB->aux. The obstack must
  548. be first initialized by alloc_aux_for_blocks. */
  549. inline void
  550. alloc_aux_for_block (basic_block bb, int size)
  551. {
  552. /* Verify that aux field is clear. */
  553. gcc_assert (!bb->aux && first_block_aux_obj);
  554. bb->aux = obstack_alloc (&block_aux_obstack, size);
  555. memset (bb->aux, 0, size);
  556. }
  557. /* Initialize the block_aux_obstack and if SIZE is nonzero, call
  558. alloc_aux_for_block for each basic block. */
  559. void
  560. alloc_aux_for_blocks (int size)
  561. {
  562. static int initialized;
  563. if (!initialized)
  564. {
  565. gcc_obstack_init (&block_aux_obstack);
  566. initialized = 1;
  567. }
  568. else
  569. /* Check whether AUX data are still allocated. */
  570. gcc_assert (!first_block_aux_obj);
  571. first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
  572. if (size)
  573. {
  574. basic_block bb;
  575. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
  576. alloc_aux_for_block (bb, size);
  577. }
  578. }
  579. /* Clear AUX pointers of all blocks. */
  580. void
  581. clear_aux_for_blocks (void)
  582. {
  583. basic_block bb;
  584. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
  585. bb->aux = NULL;
  586. }
  587. /* Free data allocated in block_aux_obstack and clear AUX pointers
  588. of all blocks. */
  589. void
  590. free_aux_for_blocks (void)
  591. {
  592. gcc_assert (first_block_aux_obj);
  593. obstack_free (&block_aux_obstack, first_block_aux_obj);
  594. first_block_aux_obj = NULL;
  595. clear_aux_for_blocks ();
  596. }
  597. /* Allocate a memory edge of SIZE as BB->aux. The obstack must
  598. be first initialized by alloc_aux_for_edges. */
  599. inline void
  600. alloc_aux_for_edge (edge e, int size)
  601. {
  602. /* Verify that aux field is clear. */
  603. gcc_assert (!e->aux && first_edge_aux_obj);
  604. e->aux = obstack_alloc (&edge_aux_obstack, size);
  605. memset (e->aux, 0, size);
  606. }
  607. /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
  608. alloc_aux_for_edge for each basic edge. */
  609. void
  610. alloc_aux_for_edges (int size)
  611. {
  612. static int initialized;
  613. if (!initialized)
  614. {
  615. gcc_obstack_init (&edge_aux_obstack);
  616. initialized = 1;
  617. }
  618. else
  619. /* Check whether AUX data are still allocated. */
  620. gcc_assert (!first_edge_aux_obj);
  621. first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
  622. if (size)
  623. {
  624. basic_block bb;
  625. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
  626. {
  627. edge e;
  628. edge_iterator ei;
  629. FOR_EACH_EDGE (e, ei, bb->succs)
  630. alloc_aux_for_edge (e, size);
  631. }
  632. }
  633. }
  634. /* Clear AUX pointers of all edges. */
  635. void
  636. clear_aux_for_edges (void)
  637. {
  638. basic_block bb;
  639. edge e;
  640. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
  641. {
  642. edge_iterator ei;
  643. FOR_EACH_EDGE (e, ei, bb->succs)
  644. e->aux = NULL;
  645. }
  646. }
  647. /* Free data allocated in edge_aux_obstack and clear AUX pointers
  648. of all edges. */
  649. void
  650. free_aux_for_edges (void)
  651. {
  652. gcc_assert (first_edge_aux_obj);
  653. obstack_free (&edge_aux_obstack, first_edge_aux_obj);
  654. first_edge_aux_obj = NULL;
  655. clear_aux_for_edges ();
  656. }
  657. void
  658. debug_bb (basic_block bb)
  659. {
  660. dump_bb (bb, stderr, 0);
  661. }
  662. basic_block
  663. debug_bb_n (int n)
  664. {
  665. basic_block bb = BASIC_BLOCK (n);
  666. dump_bb (bb, stderr, 0);
  667. return bb;
  668. }
  669. /* Dumps cfg related information about basic block BB to FILE. */
  670. static void
  671. dump_cfg_bb_info (FILE *file, basic_block bb)
  672. {
  673. unsigned i;
  674. edge_iterator ei;
  675. bool first = true;
  676. static const char * const bb_bitnames[] =
  677. {
  678. "dirty", "new", "reachable", "visited", "irreducible_loop", "superblock"
  679. };
  680. const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
  681. edge e;
  682. fprintf (file, "Basic block %d", bb->index);
  683. for (i = 0; i < n_bitnames; i++)
  684. if (bb->flags & (1 << i))
  685. {
  686. if (first)
  687. fprintf (file, " (");
  688. else
  689. fprintf (file, ", ");
  690. first = false;
  691. fputs (bb_bitnames[i], file);
  692. }
  693. if (!first)
  694. fprintf (file, ")");
  695. fprintf (file, "\n");
  696. fprintf (file, "Predecessors: ");
  697. FOR_EACH_EDGE (e, ei, bb->preds)
  698. dump_edge_info (file, e, 0);
  699. fprintf (file, "\nSuccessors: ");
  700. FOR_EACH_EDGE (e, ei, bb->succs)
  701. dump_edge_info (file, e, 1);
  702. fprintf (file, "\n\n");
  703. }
  704. /* Dumps a brief description of cfg to FILE. */
  705. void
  706. brief_dump_cfg (FILE *file)
  707. {
  708. basic_block bb;
  709. FOR_EACH_BB (bb)
  710. {
  711. dump_cfg_bb_info (file, bb);
  712. }
  713. }
  714. /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
  715. leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
  716. redirected to destination of TAKEN_EDGE.
  717. This function may leave the profile inconsistent in the case TAKEN_EDGE
  718. frequency or count is believed to be lower than FREQUENCY or COUNT
  719. respectively. */
  720. void
  721. update_bb_profile_for_threading (basic_block bb, int edge_frequency,
  722. gcov_type count, edge taken_edge)
  723. {
  724. edge c;
  725. int prob;
  726. edge_iterator ei;
  727. bb->count -= count;
  728. if (bb->count < 0)
  729. {
  730. if (dump_file)
  731. fprintf (dump_file, "bb %i count became negative after threading",
  732. bb->index);
  733. bb->count = 0;
  734. }
  735. /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
  736. Watch for overflows. */
  737. if (bb->frequency)
  738. prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
  739. else
  740. prob = 0;
  741. if (prob > taken_edge->probability)
  742. {
  743. if (dump_file)
  744. fprintf (dump_file, "Jump threading proved probability of edge "
  745. "%i->%i too small (it is %i, should be %i).\n",
  746. taken_edge->src->index, taken_edge->dest->index,
  747. taken_edge->probability, prob);
  748. prob = taken_edge->probability;
  749. }
  750. /* Now rescale the probabilities. */
  751. taken_edge->probability -= prob;
  752. prob = REG_BR_PROB_BASE - prob;
  753. bb->frequency -= edge_frequency;
  754. if (bb->frequency < 0)
  755. bb->frequency = 0;
  756. if (prob <= 0)
  757. {
  758. if (dump_file)
  759. fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
  760. "frequency of block should end up being 0, it is %i\n",
  761. bb->index, bb->frequency);
  762. EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
  763. ei = ei_start (bb->succs);
  764. ei_next (&ei);
  765. for (; (c = ei_safe_edge (ei)); ei_next (&ei))
  766. c->probability = 0;
  767. }
  768. else if (prob != REG_BR_PROB_BASE)
  769. {
  770. int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
  771. FOR_EACH_EDGE (c, ei, bb->succs)
  772. {
  773. c->probability = RDIV (c->probability * scale, 65536);
  774. if (c->probability > REG_BR_PROB_BASE)
  775. c->probability = REG_BR_PROB_BASE;
  776. }
  777. }
  778. gcc_assert (bb == taken_edge->src);
  779. taken_edge->count -= count;
  780. if (taken_edge->count < 0)
  781. {
  782. if (dump_file)
  783. fprintf (dump_file, "edge %i->%i count became negative after threading",
  784. taken_edge->src->index, taken_edge->dest->index);
  785. taken_edge->count = 0;
  786. }
  787. }
  788. /* Multiply all frequencies of basic blocks in array BBS of length NBBS
  789. by NUM/DEN, in int arithmetic. May lose some accuracy. */
  790. void
  791. scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
  792. {
  793. int i;
  794. edge e;
  795. if (num < 0)
  796. num = 0;
  797. if (num > den)
  798. return;
  799. /* Assume that the users are producing the fraction from frequencies
  800. that never grow far enough to risk arithmetic overflow. */
  801. gcc_assert (num < 65536);
  802. for (i = 0; i < nbbs; i++)
  803. {
  804. edge_iterator ei;
  805. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  806. bbs[i]->count = RDIV (bbs[i]->count * num, den);
  807. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  808. e->count = RDIV (e->count * num, den);
  809. }
  810. }
  811. /* numbers smaller than this value are safe to multiply without getting
  812. 64bit overflow. */
  813. #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
  814. /* Multiply all frequencies of basic blocks in array BBS of length NBBS
  815. by NUM/DEN, in gcov_type arithmetic. More accurate than previous
  816. function but considerably slower. */
  817. void
  818. scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
  819. gcov_type den)
  820. {
  821. int i;
  822. edge e;
  823. gcov_type fraction = RDIV (num * 65536, den);
  824. gcc_assert (fraction >= 0);
  825. if (num < MAX_SAFE_MULTIPLIER)
  826. for (i = 0; i < nbbs; i++)
  827. {
  828. edge_iterator ei;
  829. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  830. if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
  831. bbs[i]->count = RDIV (bbs[i]->count * num, den);
  832. else
  833. bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
  834. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  835. if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
  836. e->count = RDIV (e->count * num, den);
  837. else
  838. e->count = RDIV (e->count * fraction, 65536);
  839. }
  840. else
  841. for (i = 0; i < nbbs; i++)
  842. {
  843. edge_iterator ei;
  844. if (sizeof (gcov_type) > sizeof (int))
  845. bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
  846. else
  847. bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
  848. bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
  849. FOR_EACH_EDGE (e, ei, bbs[i]->succs)
  850. e->count = RDIV (e->count * fraction, 65536);
  851. }
  852. }
  853. /* Data structures used to maintain mapping between basic blocks and
  854. copies. */
  855. static htab_t bb_original;
  856. static htab_t bb_copy;
  857. static alloc_pool original_copy_bb_pool;
  858. struct htab_bb_copy_original_entry
  859. {
  860. /* Block we are attaching info to. */
  861. int index1;
  862. /* Index of original or copy (depending on the hashtable) */
  863. int index2;
  864. };
  865. static hashval_t
  866. bb_copy_original_hash (const void *p)
  867. {
  868. struct htab_bb_copy_original_entry *data
  869. = ((struct htab_bb_copy_original_entry *)p);
  870. return data->index1;
  871. }
  872. static int
  873. bb_copy_original_eq (const void *p, const void *q)
  874. {
  875. struct htab_bb_copy_original_entry *data
  876. = ((struct htab_bb_copy_original_entry *)p);
  877. struct htab_bb_copy_original_entry *data2
  878. = ((struct htab_bb_copy_original_entry *)q);
  879. return data->index1 == data2->index1;
  880. }
  881. /* Initialize the data structures to maintain mapping between blocks
  882. and its copies. */
  883. void
  884. initialize_original_copy_tables (void)
  885. {
  886. gcc_assert (!original_copy_bb_pool);
  887. original_copy_bb_pool
  888. = create_alloc_pool ("original_copy",
  889. sizeof (struct htab_bb_copy_original_entry), 10);
  890. bb_original = htab_create (10, bb_copy_original_hash,
  891. bb_copy_original_eq, NULL);
  892. bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
  893. }
  894. /* Free the data structures to maintain mapping between blocks and
  895. its copies. */
  896. void
  897. free_original_copy_tables (void)
  898. {
  899. gcc_assert (original_copy_bb_pool);
  900. htab_delete (bb_copy);
  901. htab_delete (bb_original);
  902. free_alloc_pool (original_copy_bb_pool);
  903. bb_copy = NULL;
  904. bb_original = NULL;
  905. original_copy_bb_pool = NULL;
  906. }
  907. /* Set original for basic block. Do nothing when data structures are not
  908. initialized so passes not needing this don't need to care. */
  909. void
  910. set_bb_original (basic_block bb, basic_block original)
  911. {
  912. if (original_copy_bb_pool)
  913. {
  914. struct htab_bb_copy_original_entry **slot;
  915. struct htab_bb_copy_original_entry key;
  916. key.index1 = bb->index;
  917. slot =
  918. (struct htab_bb_copy_original_entry **) htab_find_slot (bb_original,
  919. &key, INSERT);
  920. if (*slot)
  921. (*slot)->index2 = original->index;
  922. else
  923. {
  924. *slot = pool_alloc (original_copy_bb_pool);
  925. (*slot)->index1 = bb->index;
  926. (*slot)->index2 = original->index;
  927. }
  928. }
  929. }
  930. /* Get the original basic block. */
  931. basic_block
  932. get_bb_original (basic_block bb)
  933. {
  934. struct htab_bb_copy_original_entry *entry;
  935. struct htab_bb_copy_original_entry key;
  936. gcc_assert (original_copy_bb_pool);
  937. key.index1 = bb->index;
  938. entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
  939. if (entry)
  940. return BASIC_BLOCK (entry->index2);
  941. else
  942. return NULL;
  943. }
  944. /* Set copy for basic block. Do nothing when data structures are not
  945. initialized so passes not needing this don't need to care. */
  946. void
  947. set_bb_copy (basic_block bb, basic_block copy)
  948. {
  949. if (original_copy_bb_pool)
  950. {
  951. struct htab_bb_copy_original_entry **slot;
  952. struct htab_bb_copy_original_entry key;
  953. key.index1 = bb->index;
  954. slot =
  955. (struct htab_bb_copy_original_entry **) htab_find_slot (bb_copy,
  956. &key, INSERT);
  957. if (*slot)
  958. (*slot)->index2 = copy->index;
  959. else
  960. {
  961. *slot = pool_alloc (original_copy_bb_pool);
  962. (*slot)->index1 = bb->index;
  963. (*slot)->index2 = copy->index;
  964. }
  965. }
  966. }
  967. /* Get the copy of basic block. */
  968. basic_block
  969. get_bb_copy (basic_block bb)
  970. {
  971. struct htab_bb_copy_original_entry *entry;
  972. struct htab_bb_copy_original_entry key;
  973. gcc_assert (original_copy_bb_pool);
  974. key.index1 = bb->index;
  975. entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
  976. if (entry)
  977. return BASIC_BLOCK (entry->index2);
  978. else
  979. return NULL;
  980. }