dominance.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /* Calculate (post)dominators in slightly super-linear time.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Matz (matz@ifh.de).
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This file implements the well known algorithm from Lengauer and Tarjan
  17. to compute the dominators in a control flow graph. A basic block D is said
  18. to dominate another block X, when all paths from the entry node of the CFG
  19. to X go also over D. The dominance relation is a transitive reflexive
  20. relation and its minimal transitive reduction is a tree, called the
  21. dominator tree. So for each block X besides the entry block exists a
  22. block I(X), called the immediate dominator of X, which is the parent of X
  23. in the dominator tree.
  24. The algorithm computes this dominator tree implicitly by computing for
  25. each block its immediate dominator. We use tree balancing and path
  26. compression, so it's the O(e*a(e,v)) variant, where a(e,v) is the very
  27. slowly growing functional inverse of the Ackerman function. */
  28. #include "config.h"
  29. #include "system.h"
  30. #include "coretypes.h"
  31. #include "tm.h"
  32. #include "rtl.h"
  33. #include "hard-reg-set.h"
  34. #include "obstack.h"
  35. #include "predict.h"
  36. #include "vec.h"
  37. #include "hashtab.h"
  38. #include "hash-set.h"
  39. #include "machmode.h"
  40. #include "input.h"
  41. #include "function.h"
  42. #include "dominance.h"
  43. #include "cfg.h"
  44. #include "cfganal.h"
  45. #include "basic-block.h"
  46. #include "diagnostic-core.h"
  47. #include "et-forest.h"
  48. #include "timevar.h"
  49. #include "hash-map.h"
  50. #include "graphds.h"
  51. #include "bitmap.h"
  52. /* We name our nodes with integers, beginning with 1. Zero is reserved for
  53. 'undefined' or 'end of list'. The name of each node is given by the dfs
  54. number of the corresponding basic block. Please note, that we include the
  55. artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
  56. support multiple entry points. Its dfs number is of course 1. */
  57. /* Type of Basic Block aka. TBB */
  58. typedef unsigned int TBB;
  59. /* We work in a poor-mans object oriented fashion, and carry an instance of
  60. this structure through all our 'methods'. It holds various arrays
  61. reflecting the (sub)structure of the flowgraph. Most of them are of type
  62. TBB and are also indexed by TBB. */
  63. struct dom_info
  64. {
  65. /* The parent of a node in the DFS tree. */
  66. TBB *dfs_parent;
  67. /* For a node x key[x] is roughly the node nearest to the root from which
  68. exists a way to x only over nodes behind x. Such a node is also called
  69. semidominator. */
  70. TBB *key;
  71. /* The value in path_min[x] is the node y on the path from x to the root of
  72. the tree x is in with the smallest key[y]. */
  73. TBB *path_min;
  74. /* bucket[x] points to the first node of the set of nodes having x as key. */
  75. TBB *bucket;
  76. /* And next_bucket[x] points to the next node. */
  77. TBB *next_bucket;
  78. /* After the algorithm is done, dom[x] contains the immediate dominator
  79. of x. */
  80. TBB *dom;
  81. /* The following few fields implement the structures needed for disjoint
  82. sets. */
  83. /* set_chain[x] is the next node on the path from x to the representative
  84. of the set containing x. If set_chain[x]==0 then x is a root. */
  85. TBB *set_chain;
  86. /* set_size[x] is the number of elements in the set named by x. */
  87. unsigned int *set_size;
  88. /* set_child[x] is used for balancing the tree representing a set. It can
  89. be understood as the next sibling of x. */
  90. TBB *set_child;
  91. /* If b is the number of a basic block (BB->index), dfs_order[b] is the
  92. number of that node in DFS order counted from 1. This is an index
  93. into most of the other arrays in this structure. */
  94. TBB *dfs_order;
  95. /* If x is the DFS-index of a node which corresponds with a basic block,
  96. dfs_to_bb[x] is that basic block. Note, that in our structure there are
  97. more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
  98. is true for every basic block bb, but not the opposite. */
  99. basic_block *dfs_to_bb;
  100. /* This is the next free DFS number when creating the DFS tree. */
  101. unsigned int dfsnum;
  102. /* The number of nodes in the DFS tree (==dfsnum-1). */
  103. unsigned int nodes;
  104. /* Blocks with bits set here have a fake edge to EXIT. These are used
  105. to turn a DFS forest into a proper tree. */
  106. bitmap fake_exit_edge;
  107. };
  108. static void init_dom_info (struct dom_info *, enum cdi_direction);
  109. static void free_dom_info (struct dom_info *);
  110. static void calc_dfs_tree_nonrec (struct dom_info *, basic_block, bool);
  111. static void calc_dfs_tree (struct dom_info *, bool);
  112. static void compress (struct dom_info *, TBB);
  113. static TBB eval (struct dom_info *, TBB);
  114. static void link_roots (struct dom_info *, TBB, TBB);
  115. static void calc_idoms (struct dom_info *, bool);
  116. void debug_dominance_info (enum cdi_direction);
  117. void debug_dominance_tree (enum cdi_direction, basic_block);
  118. /* Helper macro for allocating and initializing an array,
  119. for aesthetic reasons. */
  120. #define init_ar(var, type, num, content) \
  121. do \
  122. { \
  123. unsigned int i = 1; /* Catch content == i. */ \
  124. if (! (content)) \
  125. (var) = XCNEWVEC (type, num); \
  126. else \
  127. { \
  128. (var) = XNEWVEC (type, (num)); \
  129. for (i = 0; i < num; i++) \
  130. (var)[i] = (content); \
  131. } \
  132. } \
  133. while (0)
  134. /* Allocate all needed memory in a pessimistic fashion (so we round up).
  135. This initializes the contents of DI, which already must be allocated. */
  136. static void
  137. init_dom_info (struct dom_info *di, enum cdi_direction dir)
  138. {
  139. /* We need memory for n_basic_blocks nodes. */
  140. unsigned int num = n_basic_blocks_for_fn (cfun);
  141. init_ar (di->dfs_parent, TBB, num, 0);
  142. init_ar (di->path_min, TBB, num, i);
  143. init_ar (di->key, TBB, num, i);
  144. init_ar (di->dom, TBB, num, 0);
  145. init_ar (di->bucket, TBB, num, 0);
  146. init_ar (di->next_bucket, TBB, num, 0);
  147. init_ar (di->set_chain, TBB, num, 0);
  148. init_ar (di->set_size, unsigned int, num, 1);
  149. init_ar (di->set_child, TBB, num, 0);
  150. init_ar (di->dfs_order, TBB,
  151. (unsigned int) last_basic_block_for_fn (cfun) + 1, 0);
  152. init_ar (di->dfs_to_bb, basic_block, num, 0);
  153. di->dfsnum = 1;
  154. di->nodes = 0;
  155. switch (dir)
  156. {
  157. case CDI_DOMINATORS:
  158. di->fake_exit_edge = NULL;
  159. break;
  160. case CDI_POST_DOMINATORS:
  161. di->fake_exit_edge = BITMAP_ALLOC (NULL);
  162. break;
  163. default:
  164. gcc_unreachable ();
  165. break;
  166. }
  167. }
  168. #undef init_ar
  169. /* Map dominance calculation type to array index used for various
  170. dominance information arrays. This version is simple -- it will need
  171. to be modified, obviously, if additional values are added to
  172. cdi_direction. */
  173. static unsigned int
  174. dom_convert_dir_to_idx (enum cdi_direction dir)
  175. {
  176. gcc_checking_assert (dir == CDI_DOMINATORS || dir == CDI_POST_DOMINATORS);
  177. return dir - 1;
  178. }
  179. /* Free all allocated memory in DI, but not DI itself. */
  180. static void
  181. free_dom_info (struct dom_info *di)
  182. {
  183. free (di->dfs_parent);
  184. free (di->path_min);
  185. free (di->key);
  186. free (di->dom);
  187. free (di->bucket);
  188. free (di->next_bucket);
  189. free (di->set_chain);
  190. free (di->set_size);
  191. free (di->set_child);
  192. free (di->dfs_order);
  193. free (di->dfs_to_bb);
  194. BITMAP_FREE (di->fake_exit_edge);
  195. }
  196. /* The nonrecursive variant of creating a DFS tree. DI is our working
  197. structure, BB the starting basic block for this tree and REVERSE
  198. is true, if predecessors should be visited instead of successors of a
  199. node. After this is done all nodes reachable from BB were visited, have
  200. assigned their dfs number and are linked together to form a tree. */
  201. static void
  202. calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb, bool reverse)
  203. {
  204. /* We call this _only_ if bb is not already visited. */
  205. edge e;
  206. TBB child_i, my_i = 0;
  207. edge_iterator *stack;
  208. edge_iterator ei, einext;
  209. int sp;
  210. /* Start block (the entry block for forward problem, exit block for backward
  211. problem). */
  212. basic_block en_block;
  213. /* Ending block. */
  214. basic_block ex_block;
  215. stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
  216. sp = 0;
  217. /* Initialize our border blocks, and the first edge. */
  218. if (reverse)
  219. {
  220. ei = ei_start (bb->preds);
  221. en_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
  222. ex_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  223. }
  224. else
  225. {
  226. ei = ei_start (bb->succs);
  227. en_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  228. ex_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
  229. }
  230. /* When the stack is empty we break out of this loop. */
  231. while (1)
  232. {
  233. basic_block bn;
  234. /* This loop traverses edges e in depth first manner, and fills the
  235. stack. */
  236. while (!ei_end_p (ei))
  237. {
  238. e = ei_edge (ei);
  239. /* Deduce from E the current and the next block (BB and BN), and the
  240. next edge. */
  241. if (reverse)
  242. {
  243. bn = e->src;
  244. /* If the next node BN is either already visited or a border
  245. block the current edge is useless, and simply overwritten
  246. with the next edge out of the current node. */
  247. if (bn == ex_block || di->dfs_order[bn->index])
  248. {
  249. ei_next (&ei);
  250. continue;
  251. }
  252. bb = e->dest;
  253. einext = ei_start (bn->preds);
  254. }
  255. else
  256. {
  257. bn = e->dest;
  258. if (bn == ex_block || di->dfs_order[bn->index])
  259. {
  260. ei_next (&ei);
  261. continue;
  262. }
  263. bb = e->src;
  264. einext = ei_start (bn->succs);
  265. }
  266. gcc_assert (bn != en_block);
  267. /* Fill the DFS tree info calculatable _before_ recursing. */
  268. if (bb != en_block)
  269. my_i = di->dfs_order[bb->index];
  270. else
  271. my_i = di->dfs_order[last_basic_block_for_fn (cfun)];
  272. child_i = di->dfs_order[bn->index] = di->dfsnum++;
  273. di->dfs_to_bb[child_i] = bn;
  274. di->dfs_parent[child_i] = my_i;
  275. /* Save the current point in the CFG on the stack, and recurse. */
  276. stack[sp++] = ei;
  277. ei = einext;
  278. }
  279. if (!sp)
  280. break;
  281. ei = stack[--sp];
  282. /* OK. The edge-list was exhausted, meaning normally we would
  283. end the recursion. After returning from the recursive call,
  284. there were (may be) other statements which were run after a
  285. child node was completely considered by DFS. Here is the
  286. point to do it in the non-recursive variant.
  287. E.g. The block just completed is in e->dest for forward DFS,
  288. the block not yet completed (the parent of the one above)
  289. in e->src. This could be used e.g. for computing the number of
  290. descendants or the tree depth. */
  291. ei_next (&ei);
  292. }
  293. free (stack);
  294. }
  295. /* The main entry for calculating the DFS tree or forest. DI is our working
  296. structure and REVERSE is true, if we are interested in the reverse flow
  297. graph. In that case the result is not necessarily a tree but a forest,
  298. because there may be nodes from which the EXIT_BLOCK is unreachable. */
  299. static void
  300. calc_dfs_tree (struct dom_info *di, bool reverse)
  301. {
  302. /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
  303. basic_block begin = (reverse
  304. ? EXIT_BLOCK_PTR_FOR_FN (cfun) : ENTRY_BLOCK_PTR_FOR_FN (cfun));
  305. di->dfs_order[last_basic_block_for_fn (cfun)] = di->dfsnum;
  306. di->dfs_to_bb[di->dfsnum] = begin;
  307. di->dfsnum++;
  308. calc_dfs_tree_nonrec (di, begin, reverse);
  309. if (reverse)
  310. {
  311. /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
  312. They are reverse-unreachable. In the dom-case we disallow such
  313. nodes, but in post-dom we have to deal with them.
  314. There are two situations in which this occurs. First, noreturn
  315. functions. Second, infinite loops. In the first case we need to
  316. pretend that there is an edge to the exit block. In the second
  317. case, we wind up with a forest. We need to process all noreturn
  318. blocks before we know if we've got any infinite loops. */
  319. basic_block b;
  320. bool saw_unconnected = false;
  321. FOR_EACH_BB_REVERSE_FN (b, cfun)
  322. {
  323. if (EDGE_COUNT (b->succs) > 0)
  324. {
  325. if (di->dfs_order[b->index] == 0)
  326. saw_unconnected = true;
  327. continue;
  328. }
  329. bitmap_set_bit (di->fake_exit_edge, b->index);
  330. di->dfs_order[b->index] = di->dfsnum;
  331. di->dfs_to_bb[di->dfsnum] = b;
  332. di->dfs_parent[di->dfsnum] =
  333. di->dfs_order[last_basic_block_for_fn (cfun)];
  334. di->dfsnum++;
  335. calc_dfs_tree_nonrec (di, b, reverse);
  336. }
  337. if (saw_unconnected)
  338. {
  339. FOR_EACH_BB_REVERSE_FN (b, cfun)
  340. {
  341. basic_block b2;
  342. if (di->dfs_order[b->index])
  343. continue;
  344. b2 = dfs_find_deadend (b);
  345. gcc_checking_assert (di->dfs_order[b2->index] == 0);
  346. bitmap_set_bit (di->fake_exit_edge, b2->index);
  347. di->dfs_order[b2->index] = di->dfsnum;
  348. di->dfs_to_bb[di->dfsnum] = b2;
  349. di->dfs_parent[di->dfsnum] =
  350. di->dfs_order[last_basic_block_for_fn (cfun)];
  351. di->dfsnum++;
  352. calc_dfs_tree_nonrec (di, b2, reverse);
  353. gcc_checking_assert (di->dfs_order[b->index]);
  354. }
  355. }
  356. }
  357. di->nodes = di->dfsnum - 1;
  358. /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
  359. gcc_assert (di->nodes == (unsigned int) n_basic_blocks_for_fn (cfun) - 1);
  360. }
  361. /* Compress the path from V to the root of its set and update path_min at the
  362. same time. After compress(di, V) set_chain[V] is the root of the set V is
  363. in and path_min[V] is the node with the smallest key[] value on the path
  364. from V to that root. */
  365. static void
  366. compress (struct dom_info *di, TBB v)
  367. {
  368. /* Btw. It's not worth to unrecurse compress() as the depth is usually not
  369. greater than 5 even for huge graphs (I've not seen call depth > 4).
  370. Also performance wise compress() ranges _far_ behind eval(). */
  371. TBB parent = di->set_chain[v];
  372. if (di->set_chain[parent])
  373. {
  374. compress (di, parent);
  375. if (di->key[di->path_min[parent]] < di->key[di->path_min[v]])
  376. di->path_min[v] = di->path_min[parent];
  377. di->set_chain[v] = di->set_chain[parent];
  378. }
  379. }
  380. /* Compress the path from V to the set root of V if needed (when the root has
  381. changed since the last call). Returns the node with the smallest key[]
  382. value on the path from V to the root. */
  383. static inline TBB
  384. eval (struct dom_info *di, TBB v)
  385. {
  386. /* The representative of the set V is in, also called root (as the set
  387. representation is a tree). */
  388. TBB rep = di->set_chain[v];
  389. /* V itself is the root. */
  390. if (!rep)
  391. return di->path_min[v];
  392. /* Compress only if necessary. */
  393. if (di->set_chain[rep])
  394. {
  395. compress (di, v);
  396. rep = di->set_chain[v];
  397. }
  398. if (di->key[di->path_min[rep]] >= di->key[di->path_min[v]])
  399. return di->path_min[v];
  400. else
  401. return di->path_min[rep];
  402. }
  403. /* This essentially merges the two sets of V and W, giving a single set with
  404. the new root V. The internal representation of these disjoint sets is a
  405. balanced tree. Currently link(V,W) is only used with V being the parent
  406. of W. */
  407. static void
  408. link_roots (struct dom_info *di, TBB v, TBB w)
  409. {
  410. TBB s = w;
  411. /* Rebalance the tree. */
  412. while (di->key[di->path_min[w]] < di->key[di->path_min[di->set_child[s]]])
  413. {
  414. if (di->set_size[s] + di->set_size[di->set_child[di->set_child[s]]]
  415. >= 2 * di->set_size[di->set_child[s]])
  416. {
  417. di->set_chain[di->set_child[s]] = s;
  418. di->set_child[s] = di->set_child[di->set_child[s]];
  419. }
  420. else
  421. {
  422. di->set_size[di->set_child[s]] = di->set_size[s];
  423. s = di->set_chain[s] = di->set_child[s];
  424. }
  425. }
  426. di->path_min[s] = di->path_min[w];
  427. di->set_size[v] += di->set_size[w];
  428. if (di->set_size[v] < 2 * di->set_size[w])
  429. {
  430. TBB tmp = s;
  431. s = di->set_child[v];
  432. di->set_child[v] = tmp;
  433. }
  434. /* Merge all subtrees. */
  435. while (s)
  436. {
  437. di->set_chain[s] = v;
  438. s = di->set_child[s];
  439. }
  440. }
  441. /* This calculates the immediate dominators (or post-dominators if REVERSE is
  442. true). DI is our working structure and should hold the DFS forest.
  443. On return the immediate dominator to node V is in di->dom[V]. */
  444. static void
  445. calc_idoms (struct dom_info *di, bool reverse)
  446. {
  447. TBB v, w, k, par;
  448. basic_block en_block;
  449. edge_iterator ei, einext;
  450. if (reverse)
  451. en_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
  452. else
  453. en_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  454. /* Go backwards in DFS order, to first look at the leafs. */
  455. v = di->nodes;
  456. while (v > 1)
  457. {
  458. basic_block bb = di->dfs_to_bb[v];
  459. edge e;
  460. par = di->dfs_parent[v];
  461. k = v;
  462. ei = (reverse) ? ei_start (bb->succs) : ei_start (bb->preds);
  463. if (reverse)
  464. {
  465. /* If this block has a fake edge to exit, process that first. */
  466. if (bitmap_bit_p (di->fake_exit_edge, bb->index))
  467. {
  468. einext = ei;
  469. einext.index = 0;
  470. goto do_fake_exit_edge;
  471. }
  472. }
  473. /* Search all direct predecessors for the smallest node with a path
  474. to them. That way we have the smallest node with also a path to
  475. us only over nodes behind us. In effect we search for our
  476. semidominator. */
  477. while (!ei_end_p (ei))
  478. {
  479. TBB k1;
  480. basic_block b;
  481. e = ei_edge (ei);
  482. b = (reverse) ? e->dest : e->src;
  483. einext = ei;
  484. ei_next (&einext);
  485. if (b == en_block)
  486. {
  487. do_fake_exit_edge:
  488. k1 = di->dfs_order[last_basic_block_for_fn (cfun)];
  489. }
  490. else
  491. k1 = di->dfs_order[b->index];
  492. /* Call eval() only if really needed. If k1 is above V in DFS tree,
  493. then we know, that eval(k1) == k1 and key[k1] == k1. */
  494. if (k1 > v)
  495. k1 = di->key[eval (di, k1)];
  496. if (k1 < k)
  497. k = k1;
  498. ei = einext;
  499. }
  500. di->key[v] = k;
  501. link_roots (di, par, v);
  502. di->next_bucket[v] = di->bucket[k];
  503. di->bucket[k] = v;
  504. /* Transform semidominators into dominators. */
  505. for (w = di->bucket[par]; w; w = di->next_bucket[w])
  506. {
  507. k = eval (di, w);
  508. if (di->key[k] < di->key[w])
  509. di->dom[w] = k;
  510. else
  511. di->dom[w] = par;
  512. }
  513. /* We don't need to cleanup next_bucket[]. */
  514. di->bucket[par] = 0;
  515. v--;
  516. }
  517. /* Explicitly define the dominators. */
  518. di->dom[1] = 0;
  519. for (v = 2; v <= di->nodes; v++)
  520. if (di->dom[v] != di->key[v])
  521. di->dom[v] = di->dom[di->dom[v]];
  522. }
  523. /* Assign dfs numbers starting from NUM to NODE and its sons. */
  524. static void
  525. assign_dfs_numbers (struct et_node *node, int *num)
  526. {
  527. struct et_node *son;
  528. node->dfs_num_in = (*num)++;
  529. if (node->son)
  530. {
  531. assign_dfs_numbers (node->son, num);
  532. for (son = node->son->right; son != node->son; son = son->right)
  533. assign_dfs_numbers (son, num);
  534. }
  535. node->dfs_num_out = (*num)++;
  536. }
  537. /* Compute the data necessary for fast resolving of dominator queries in a
  538. static dominator tree. */
  539. static void
  540. compute_dom_fast_query (enum cdi_direction dir)
  541. {
  542. int num = 0;
  543. basic_block bb;
  544. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  545. gcc_checking_assert (dom_info_available_p (dir));
  546. if (dom_computed[dir_index] == DOM_OK)
  547. return;
  548. FOR_ALL_BB_FN (bb, cfun)
  549. {
  550. if (!bb->dom[dir_index]->father)
  551. assign_dfs_numbers (bb->dom[dir_index], &num);
  552. }
  553. dom_computed[dir_index] = DOM_OK;
  554. }
  555. /* The main entry point into this module. DIR is set depending on whether
  556. we want to compute dominators or postdominators. */
  557. void
  558. calculate_dominance_info (enum cdi_direction dir)
  559. {
  560. struct dom_info di;
  561. basic_block b;
  562. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  563. bool reverse = (dir == CDI_POST_DOMINATORS) ? true : false;
  564. if (dom_computed[dir_index] == DOM_OK)
  565. return;
  566. timevar_push (TV_DOMINANCE);
  567. if (!dom_info_available_p (dir))
  568. {
  569. gcc_assert (!n_bbs_in_dom_tree[dir_index]);
  570. FOR_ALL_BB_FN (b, cfun)
  571. {
  572. b->dom[dir_index] = et_new_tree (b);
  573. }
  574. n_bbs_in_dom_tree[dir_index] = n_basic_blocks_for_fn (cfun);
  575. init_dom_info (&di, dir);
  576. calc_dfs_tree (&di, reverse);
  577. calc_idoms (&di, reverse);
  578. FOR_EACH_BB_FN (b, cfun)
  579. {
  580. TBB d = di.dom[di.dfs_order[b->index]];
  581. if (di.dfs_to_bb[d])
  582. et_set_father (b->dom[dir_index], di.dfs_to_bb[d]->dom[dir_index]);
  583. }
  584. free_dom_info (&di);
  585. dom_computed[dir_index] = DOM_NO_FAST_QUERY;
  586. }
  587. compute_dom_fast_query (dir);
  588. timevar_pop (TV_DOMINANCE);
  589. }
  590. /* Free dominance information for direction DIR. */
  591. void
  592. free_dominance_info (function *fn, enum cdi_direction dir)
  593. {
  594. basic_block bb;
  595. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  596. if (!dom_info_available_p (fn, dir))
  597. return;
  598. FOR_ALL_BB_FN (bb, fn)
  599. {
  600. et_free_tree_force (bb->dom[dir_index]);
  601. bb->dom[dir_index] = NULL;
  602. }
  603. et_free_pools ();
  604. fn->cfg->x_n_bbs_in_dom_tree[dir_index] = 0;
  605. fn->cfg->x_dom_computed[dir_index] = DOM_NONE;
  606. }
  607. void
  608. free_dominance_info (enum cdi_direction dir)
  609. {
  610. free_dominance_info (cfun, dir);
  611. }
  612. /* Return the immediate dominator of basic block BB. */
  613. basic_block
  614. get_immediate_dominator (enum cdi_direction dir, basic_block bb)
  615. {
  616. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  617. struct et_node *node = bb->dom[dir_index];
  618. gcc_checking_assert (dom_computed[dir_index]);
  619. if (!node->father)
  620. return NULL;
  621. return (basic_block) node->father->data;
  622. }
  623. /* Set the immediate dominator of the block possibly removing
  624. existing edge. NULL can be used to remove any edge. */
  625. void
  626. set_immediate_dominator (enum cdi_direction dir, basic_block bb,
  627. basic_block dominated_by)
  628. {
  629. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  630. struct et_node *node = bb->dom[dir_index];
  631. gcc_checking_assert (dom_computed[dir_index]);
  632. if (node->father)
  633. {
  634. if (node->father->data == dominated_by)
  635. return;
  636. et_split (node);
  637. }
  638. if (dominated_by)
  639. et_set_father (node, dominated_by->dom[dir_index]);
  640. if (dom_computed[dir_index] == DOM_OK)
  641. dom_computed[dir_index] = DOM_NO_FAST_QUERY;
  642. }
  643. /* Returns the list of basic blocks immediately dominated by BB, in the
  644. direction DIR. */
  645. vec<basic_block>
  646. get_dominated_by (enum cdi_direction dir, basic_block bb)
  647. {
  648. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  649. struct et_node *node = bb->dom[dir_index], *son = node->son, *ason;
  650. vec<basic_block> bbs = vNULL;
  651. gcc_checking_assert (dom_computed[dir_index]);
  652. if (!son)
  653. return vNULL;
  654. bbs.safe_push ((basic_block) son->data);
  655. for (ason = son->right; ason != son; ason = ason->right)
  656. bbs.safe_push ((basic_block) ason->data);
  657. return bbs;
  658. }
  659. /* Returns the list of basic blocks that are immediately dominated (in
  660. direction DIR) by some block between N_REGION ones stored in REGION,
  661. except for blocks in the REGION itself. */
  662. vec<basic_block>
  663. get_dominated_by_region (enum cdi_direction dir, basic_block *region,
  664. unsigned n_region)
  665. {
  666. unsigned i;
  667. basic_block dom;
  668. vec<basic_block> doms = vNULL;
  669. for (i = 0; i < n_region; i++)
  670. region[i]->flags |= BB_DUPLICATED;
  671. for (i = 0; i < n_region; i++)
  672. for (dom = first_dom_son (dir, region[i]);
  673. dom;
  674. dom = next_dom_son (dir, dom))
  675. if (!(dom->flags & BB_DUPLICATED))
  676. doms.safe_push (dom);
  677. for (i = 0; i < n_region; i++)
  678. region[i]->flags &= ~BB_DUPLICATED;
  679. return doms;
  680. }
  681. /* Returns the list of basic blocks including BB dominated by BB, in the
  682. direction DIR up to DEPTH in the dominator tree. The DEPTH of zero will
  683. produce a vector containing all dominated blocks. The vector will be sorted
  684. in preorder. */
  685. vec<basic_block>
  686. get_dominated_to_depth (enum cdi_direction dir, basic_block bb, int depth)
  687. {
  688. vec<basic_block> bbs = vNULL;
  689. unsigned i;
  690. unsigned next_level_start;
  691. i = 0;
  692. bbs.safe_push (bb);
  693. next_level_start = 1; /* = bbs.length (); */
  694. do
  695. {
  696. basic_block son;
  697. bb = bbs[i++];
  698. for (son = first_dom_son (dir, bb);
  699. son;
  700. son = next_dom_son (dir, son))
  701. bbs.safe_push (son);
  702. if (i == next_level_start && --depth)
  703. next_level_start = bbs.length ();
  704. }
  705. while (i < next_level_start);
  706. return bbs;
  707. }
  708. /* Returns the list of basic blocks including BB dominated by BB, in the
  709. direction DIR. The vector will be sorted in preorder. */
  710. vec<basic_block>
  711. get_all_dominated_blocks (enum cdi_direction dir, basic_block bb)
  712. {
  713. return get_dominated_to_depth (dir, bb, 0);
  714. }
  715. /* Redirect all edges pointing to BB to TO. */
  716. void
  717. redirect_immediate_dominators (enum cdi_direction dir, basic_block bb,
  718. basic_block to)
  719. {
  720. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  721. struct et_node *bb_node, *to_node, *son;
  722. bb_node = bb->dom[dir_index];
  723. to_node = to->dom[dir_index];
  724. gcc_checking_assert (dom_computed[dir_index]);
  725. if (!bb_node->son)
  726. return;
  727. while (bb_node->son)
  728. {
  729. son = bb_node->son;
  730. et_split (son);
  731. et_set_father (son, to_node);
  732. }
  733. if (dom_computed[dir_index] == DOM_OK)
  734. dom_computed[dir_index] = DOM_NO_FAST_QUERY;
  735. }
  736. /* Find first basic block in the tree dominating both BB1 and BB2. */
  737. basic_block
  738. nearest_common_dominator (enum cdi_direction dir, basic_block bb1, basic_block bb2)
  739. {
  740. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  741. gcc_checking_assert (dom_computed[dir_index]);
  742. if (!bb1)
  743. return bb2;
  744. if (!bb2)
  745. return bb1;
  746. return (basic_block) et_nca (bb1->dom[dir_index], bb2->dom[dir_index])->data;
  747. }
  748. /* Find the nearest common dominator for the basic blocks in BLOCKS,
  749. using dominance direction DIR. */
  750. basic_block
  751. nearest_common_dominator_for_set (enum cdi_direction dir, bitmap blocks)
  752. {
  753. unsigned i, first;
  754. bitmap_iterator bi;
  755. basic_block dom;
  756. first = bitmap_first_set_bit (blocks);
  757. dom = BASIC_BLOCK_FOR_FN (cfun, first);
  758. EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
  759. if (dom != BASIC_BLOCK_FOR_FN (cfun, i))
  760. dom = nearest_common_dominator (dir, dom, BASIC_BLOCK_FOR_FN (cfun, i));
  761. return dom;
  762. }
  763. /* Given a dominator tree, we can determine whether one thing
  764. dominates another in constant time by using two DFS numbers:
  765. 1. The number for when we visit a node on the way down the tree
  766. 2. The number for when we visit a node on the way back up the tree
  767. You can view these as bounds for the range of dfs numbers the
  768. nodes in the subtree of the dominator tree rooted at that node
  769. will contain.
  770. The dominator tree is always a simple acyclic tree, so there are
  771. only three possible relations two nodes in the dominator tree have
  772. to each other:
  773. 1. Node A is above Node B (and thus, Node A dominates node B)
  774. A
  775. |
  776. C
  777. / \
  778. B D
  779. In the above case, DFS_Number_In of A will be <= DFS_Number_In of
  780. B, and DFS_Number_Out of A will be >= DFS_Number_Out of B. This is
  781. because we must hit A in the dominator tree *before* B on the walk
  782. down, and we will hit A *after* B on the walk back up
  783. 2. Node A is below node B (and thus, node B dominates node A)
  784. B
  785. |
  786. A
  787. / \
  788. C D
  789. In the above case, DFS_Number_In of A will be >= DFS_Number_In of
  790. B, and DFS_Number_Out of A will be <= DFS_Number_Out of B.
  791. This is because we must hit A in the dominator tree *after* B on
  792. the walk down, and we will hit A *before* B on the walk back up
  793. 3. Node A and B are siblings (and thus, neither dominates the other)
  794. C
  795. |
  796. D
  797. / \
  798. A B
  799. In the above case, DFS_Number_In of A will *always* be <=
  800. DFS_Number_In of B, and DFS_Number_Out of A will *always* be <=
  801. DFS_Number_Out of B. This is because we will always finish the dfs
  802. walk of one of the subtrees before the other, and thus, the dfs
  803. numbers for one subtree can't intersect with the range of dfs
  804. numbers for the other subtree. If you swap A and B's position in
  805. the dominator tree, the comparison changes direction, but the point
  806. is that both comparisons will always go the same way if there is no
  807. dominance relationship.
  808. Thus, it is sufficient to write
  809. A_Dominates_B (node A, node B)
  810. {
  811. return DFS_Number_In(A) <= DFS_Number_In(B)
  812. && DFS_Number_Out (A) >= DFS_Number_Out(B);
  813. }
  814. A_Dominated_by_B (node A, node B)
  815. {
  816. return DFS_Number_In(A) >= DFS_Number_In(B)
  817. && DFS_Number_Out (A) <= DFS_Number_Out(B);
  818. } */
  819. /* Return TRUE in case BB1 is dominated by BB2. */
  820. bool
  821. dominated_by_p (enum cdi_direction dir, const_basic_block bb1, const_basic_block bb2)
  822. {
  823. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  824. struct et_node *n1 = bb1->dom[dir_index], *n2 = bb2->dom[dir_index];
  825. gcc_checking_assert (dom_computed[dir_index]);
  826. if (dom_computed[dir_index] == DOM_OK)
  827. return (n1->dfs_num_in >= n2->dfs_num_in
  828. && n1->dfs_num_out <= n2->dfs_num_out);
  829. return et_below (n1, n2);
  830. }
  831. /* Returns the entry dfs number for basic block BB, in the direction DIR. */
  832. unsigned
  833. bb_dom_dfs_in (enum cdi_direction dir, basic_block bb)
  834. {
  835. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  836. struct et_node *n = bb->dom[dir_index];
  837. gcc_checking_assert (dom_computed[dir_index] == DOM_OK);
  838. return n->dfs_num_in;
  839. }
  840. /* Returns the exit dfs number for basic block BB, in the direction DIR. */
  841. unsigned
  842. bb_dom_dfs_out (enum cdi_direction dir, basic_block bb)
  843. {
  844. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  845. struct et_node *n = bb->dom[dir_index];
  846. gcc_checking_assert (dom_computed[dir_index] == DOM_OK);
  847. return n->dfs_num_out;
  848. }
  849. /* Verify invariants of dominator structure. */
  850. DEBUG_FUNCTION void
  851. verify_dominators (enum cdi_direction dir)
  852. {
  853. int err = 0;
  854. basic_block bb, imm_bb, imm_bb_correct;
  855. struct dom_info di;
  856. bool reverse = (dir == CDI_POST_DOMINATORS) ? true : false;
  857. gcc_assert (dom_info_available_p (dir));
  858. init_dom_info (&di, dir);
  859. calc_dfs_tree (&di, reverse);
  860. calc_idoms (&di, reverse);
  861. FOR_EACH_BB_FN (bb, cfun)
  862. {
  863. imm_bb = get_immediate_dominator (dir, bb);
  864. if (!imm_bb)
  865. {
  866. error ("dominator of %d status unknown", bb->index);
  867. err = 1;
  868. }
  869. imm_bb_correct = di.dfs_to_bb[di.dom[di.dfs_order[bb->index]]];
  870. if (imm_bb != imm_bb_correct)
  871. {
  872. error ("dominator of %d should be %d, not %d",
  873. bb->index, imm_bb_correct->index, imm_bb->index);
  874. err = 1;
  875. }
  876. }
  877. free_dom_info (&di);
  878. gcc_assert (!err);
  879. }
  880. /* Determine immediate dominator (or postdominator, according to DIR) of BB,
  881. assuming that dominators of other blocks are correct. We also use it to
  882. recompute the dominators in a restricted area, by iterating it until it
  883. reaches a fixed point. */
  884. basic_block
  885. recompute_dominator (enum cdi_direction dir, basic_block bb)
  886. {
  887. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  888. basic_block dom_bb = NULL;
  889. edge e;
  890. edge_iterator ei;
  891. gcc_checking_assert (dom_computed[dir_index]);
  892. if (dir == CDI_DOMINATORS)
  893. {
  894. FOR_EACH_EDGE (e, ei, bb->preds)
  895. {
  896. if (!dominated_by_p (dir, e->src, bb))
  897. dom_bb = nearest_common_dominator (dir, dom_bb, e->src);
  898. }
  899. }
  900. else
  901. {
  902. FOR_EACH_EDGE (e, ei, bb->succs)
  903. {
  904. if (!dominated_by_p (dir, e->dest, bb))
  905. dom_bb = nearest_common_dominator (dir, dom_bb, e->dest);
  906. }
  907. }
  908. return dom_bb;
  909. }
  910. /* Use simple heuristics (see iterate_fix_dominators) to determine dominators
  911. of BBS. We assume that all the immediate dominators except for those of the
  912. blocks in BBS are correct. If CONSERVATIVE is true, we also assume that the
  913. currently recorded immediate dominators of blocks in BBS really dominate the
  914. blocks. The basic blocks for that we determine the dominator are removed
  915. from BBS. */
  916. static void
  917. prune_bbs_to_update_dominators (vec<basic_block> bbs,
  918. bool conservative)
  919. {
  920. unsigned i;
  921. bool single;
  922. basic_block bb, dom = NULL;
  923. edge_iterator ei;
  924. edge e;
  925. for (i = 0; bbs.iterate (i, &bb);)
  926. {
  927. if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  928. goto succeed;
  929. if (single_pred_p (bb))
  930. {
  931. set_immediate_dominator (CDI_DOMINATORS, bb, single_pred (bb));
  932. goto succeed;
  933. }
  934. if (!conservative)
  935. goto fail;
  936. single = true;
  937. dom = NULL;
  938. FOR_EACH_EDGE (e, ei, bb->preds)
  939. {
  940. if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
  941. continue;
  942. if (!dom)
  943. dom = e->src;
  944. else
  945. {
  946. single = false;
  947. dom = nearest_common_dominator (CDI_DOMINATORS, dom, e->src);
  948. }
  949. }
  950. gcc_assert (dom != NULL);
  951. if (single
  952. || find_edge (dom, bb))
  953. {
  954. set_immediate_dominator (CDI_DOMINATORS, bb, dom);
  955. goto succeed;
  956. }
  957. fail:
  958. i++;
  959. continue;
  960. succeed:
  961. bbs.unordered_remove (i);
  962. }
  963. }
  964. /* Returns root of the dominance tree in the direction DIR that contains
  965. BB. */
  966. static basic_block
  967. root_of_dom_tree (enum cdi_direction dir, basic_block bb)
  968. {
  969. return (basic_block) et_root (bb->dom[dom_convert_dir_to_idx (dir)])->data;
  970. }
  971. /* See the comment in iterate_fix_dominators. Finds the immediate dominators
  972. for the sons of Y, found using the SON and BROTHER arrays representing
  973. the dominance tree of graph G. BBS maps the vertices of G to the basic
  974. blocks. */
  975. static void
  976. determine_dominators_for_sons (struct graph *g, vec<basic_block> bbs,
  977. int y, int *son, int *brother)
  978. {
  979. bitmap gprime;
  980. int i, a, nc;
  981. vec<int> *sccs;
  982. basic_block bb, dom, ybb;
  983. unsigned si;
  984. edge e;
  985. edge_iterator ei;
  986. if (son[y] == -1)
  987. return;
  988. if (y == (int) bbs.length ())
  989. ybb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  990. else
  991. ybb = bbs[y];
  992. if (brother[son[y]] == -1)
  993. {
  994. /* Handle the common case Y has just one son specially. */
  995. bb = bbs[son[y]];
  996. set_immediate_dominator (CDI_DOMINATORS, bb,
  997. recompute_dominator (CDI_DOMINATORS, bb));
  998. identify_vertices (g, y, son[y]);
  999. return;
  1000. }
  1001. gprime = BITMAP_ALLOC (NULL);
  1002. for (a = son[y]; a != -1; a = brother[a])
  1003. bitmap_set_bit (gprime, a);
  1004. nc = graphds_scc (g, gprime);
  1005. BITMAP_FREE (gprime);
  1006. /* ??? Needed to work around the pre-processor confusion with
  1007. using a multi-argument template type as macro argument. */
  1008. typedef vec<int> vec_int_heap;
  1009. sccs = XCNEWVEC (vec_int_heap, nc);
  1010. for (a = son[y]; a != -1; a = brother[a])
  1011. sccs[g->vertices[a].component].safe_push (a);
  1012. for (i = nc - 1; i >= 0; i--)
  1013. {
  1014. dom = NULL;
  1015. FOR_EACH_VEC_ELT (sccs[i], si, a)
  1016. {
  1017. bb = bbs[a];
  1018. FOR_EACH_EDGE (e, ei, bb->preds)
  1019. {
  1020. if (root_of_dom_tree (CDI_DOMINATORS, e->src) != ybb)
  1021. continue;
  1022. dom = nearest_common_dominator (CDI_DOMINATORS, dom, e->src);
  1023. }
  1024. }
  1025. gcc_assert (dom != NULL);
  1026. FOR_EACH_VEC_ELT (sccs[i], si, a)
  1027. {
  1028. bb = bbs[a];
  1029. set_immediate_dominator (CDI_DOMINATORS, bb, dom);
  1030. }
  1031. }
  1032. for (i = 0; i < nc; i++)
  1033. sccs[i].release ();
  1034. free (sccs);
  1035. for (a = son[y]; a != -1; a = brother[a])
  1036. identify_vertices (g, y, a);
  1037. }
  1038. /* Recompute dominance information for basic blocks in the set BBS. The
  1039. function assumes that the immediate dominators of all the other blocks
  1040. in CFG are correct, and that there are no unreachable blocks.
  1041. If CONSERVATIVE is true, we additionally assume that all the ancestors of
  1042. a block of BBS in the current dominance tree dominate it. */
  1043. void
  1044. iterate_fix_dominators (enum cdi_direction dir, vec<basic_block> bbs,
  1045. bool conservative)
  1046. {
  1047. unsigned i;
  1048. basic_block bb, dom;
  1049. struct graph *g;
  1050. int n, y;
  1051. size_t dom_i;
  1052. edge e;
  1053. edge_iterator ei;
  1054. int *parent, *son, *brother;
  1055. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1056. /* We only support updating dominators. There are some problems with
  1057. updating postdominators (need to add fake edges from infinite loops
  1058. and noreturn functions), and since we do not currently use
  1059. iterate_fix_dominators for postdominators, any attempt to handle these
  1060. problems would be unused, untested, and almost surely buggy. We keep
  1061. the DIR argument for consistency with the rest of the dominator analysis
  1062. interface. */
  1063. gcc_checking_assert (dir == CDI_DOMINATORS && dom_computed[dir_index]);
  1064. /* The algorithm we use takes inspiration from the following papers, although
  1065. the details are quite different from any of them:
  1066. [1] G. Ramalingam, T. Reps, An Incremental Algorithm for Maintaining the
  1067. Dominator Tree of a Reducible Flowgraph
  1068. [2] V. C. Sreedhar, G. R. Gao, Y.-F. Lee: Incremental computation of
  1069. dominator trees
  1070. [3] K. D. Cooper, T. J. Harvey and K. Kennedy: A Simple, Fast Dominance
  1071. Algorithm
  1072. First, we use the following heuristics to decrease the size of the BBS
  1073. set:
  1074. a) if BB has a single predecessor, then its immediate dominator is this
  1075. predecessor
  1076. additionally, if CONSERVATIVE is true:
  1077. b) if all the predecessors of BB except for one (X) are dominated by BB,
  1078. then X is the immediate dominator of BB
  1079. c) if the nearest common ancestor of the predecessors of BB is X and
  1080. X -> BB is an edge in CFG, then X is the immediate dominator of BB
  1081. Then, we need to establish the dominance relation among the basic blocks
  1082. in BBS. We split the dominance tree by removing the immediate dominator
  1083. edges from BBS, creating a forest F. We form a graph G whose vertices
  1084. are BBS and ENTRY and X -> Y is an edge of G if there exists an edge
  1085. X' -> Y in CFG such that X' belongs to the tree of the dominance forest
  1086. whose root is X. We then determine dominance tree of G. Note that
  1087. for X, Y in BBS, X dominates Y in CFG if and only if X dominates Y in G.
  1088. In this step, we can use arbitrary algorithm to determine dominators.
  1089. We decided to prefer the algorithm [3] to the algorithm of
  1090. Lengauer and Tarjan, since the set BBS is usually small (rarely exceeding
  1091. 10 during gcc bootstrap), and [3] should perform better in this case.
  1092. Finally, we need to determine the immediate dominators for the basic
  1093. blocks of BBS. If the immediate dominator of X in G is Y, then
  1094. the immediate dominator of X in CFG belongs to the tree of F rooted in
  1095. Y. We process the dominator tree T of G recursively, starting from leaves.
  1096. Suppose that X_1, X_2, ..., X_k are the sons of Y in T, and that the
  1097. subtrees of the dominance tree of CFG rooted in X_i are already correct.
  1098. Let G' be the subgraph of G induced by {X_1, X_2, ..., X_k}. We make
  1099. the following observations:
  1100. (i) the immediate dominator of all blocks in a strongly connected
  1101. component of G' is the same
  1102. (ii) if X has no predecessors in G', then the immediate dominator of X
  1103. is the nearest common ancestor of the predecessors of X in the
  1104. subtree of F rooted in Y
  1105. Therefore, it suffices to find the topological ordering of G', and
  1106. process the nodes X_i in this order using the rules (i) and (ii).
  1107. Then, we contract all the nodes X_i with Y in G, so that the further
  1108. steps work correctly. */
  1109. if (!conservative)
  1110. {
  1111. /* Split the tree now. If the idoms of blocks in BBS are not
  1112. conservatively correct, setting the dominators using the
  1113. heuristics in prune_bbs_to_update_dominators could
  1114. create cycles in the dominance "tree", and cause ICE. */
  1115. FOR_EACH_VEC_ELT (bbs, i, bb)
  1116. set_immediate_dominator (CDI_DOMINATORS, bb, NULL);
  1117. }
  1118. prune_bbs_to_update_dominators (bbs, conservative);
  1119. n = bbs.length ();
  1120. if (n == 0)
  1121. return;
  1122. if (n == 1)
  1123. {
  1124. bb = bbs[0];
  1125. set_immediate_dominator (CDI_DOMINATORS, bb,
  1126. recompute_dominator (CDI_DOMINATORS, bb));
  1127. return;
  1128. }
  1129. /* Construct the graph G. */
  1130. hash_map<basic_block, int> map (251);
  1131. FOR_EACH_VEC_ELT (bbs, i, bb)
  1132. {
  1133. /* If the dominance tree is conservatively correct, split it now. */
  1134. if (conservative)
  1135. set_immediate_dominator (CDI_DOMINATORS, bb, NULL);
  1136. map.put (bb, i);
  1137. }
  1138. map.put (ENTRY_BLOCK_PTR_FOR_FN (cfun), n);
  1139. g = new_graph (n + 1);
  1140. for (y = 0; y < g->n_vertices; y++)
  1141. g->vertices[y].data = BITMAP_ALLOC (NULL);
  1142. FOR_EACH_VEC_ELT (bbs, i, bb)
  1143. {
  1144. FOR_EACH_EDGE (e, ei, bb->preds)
  1145. {
  1146. dom = root_of_dom_tree (CDI_DOMINATORS, e->src);
  1147. if (dom == bb)
  1148. continue;
  1149. dom_i = *map.get (dom);
  1150. /* Do not include parallel edges to G. */
  1151. if (!bitmap_set_bit ((bitmap) g->vertices[dom_i].data, i))
  1152. continue;
  1153. add_edge (g, dom_i, i);
  1154. }
  1155. }
  1156. for (y = 0; y < g->n_vertices; y++)
  1157. BITMAP_FREE (g->vertices[y].data);
  1158. /* Find the dominator tree of G. */
  1159. son = XNEWVEC (int, n + 1);
  1160. brother = XNEWVEC (int, n + 1);
  1161. parent = XNEWVEC (int, n + 1);
  1162. graphds_domtree (g, n, parent, son, brother);
  1163. /* Finally, traverse the tree and find the immediate dominators. */
  1164. for (y = n; son[y] != -1; y = son[y])
  1165. continue;
  1166. while (y != -1)
  1167. {
  1168. determine_dominators_for_sons (g, bbs, y, son, brother);
  1169. if (brother[y] != -1)
  1170. {
  1171. y = brother[y];
  1172. while (son[y] != -1)
  1173. y = son[y];
  1174. }
  1175. else
  1176. y = parent[y];
  1177. }
  1178. free (son);
  1179. free (brother);
  1180. free (parent);
  1181. free_graph (g);
  1182. }
  1183. void
  1184. add_to_dominance_info (enum cdi_direction dir, basic_block bb)
  1185. {
  1186. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1187. gcc_checking_assert (dom_computed[dir_index] && !bb->dom[dir_index]);
  1188. n_bbs_in_dom_tree[dir_index]++;
  1189. bb->dom[dir_index] = et_new_tree (bb);
  1190. if (dom_computed[dir_index] == DOM_OK)
  1191. dom_computed[dir_index] = DOM_NO_FAST_QUERY;
  1192. }
  1193. void
  1194. delete_from_dominance_info (enum cdi_direction dir, basic_block bb)
  1195. {
  1196. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1197. gcc_checking_assert (dom_computed[dir_index]);
  1198. et_free_tree (bb->dom[dir_index]);
  1199. bb->dom[dir_index] = NULL;
  1200. n_bbs_in_dom_tree[dir_index]--;
  1201. if (dom_computed[dir_index] == DOM_OK)
  1202. dom_computed[dir_index] = DOM_NO_FAST_QUERY;
  1203. }
  1204. /* Returns the first son of BB in the dominator or postdominator tree
  1205. as determined by DIR. */
  1206. basic_block
  1207. first_dom_son (enum cdi_direction dir, basic_block bb)
  1208. {
  1209. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1210. struct et_node *son = bb->dom[dir_index]->son;
  1211. return (basic_block) (son ? son->data : NULL);
  1212. }
  1213. /* Returns the next dominance son after BB in the dominator or postdominator
  1214. tree as determined by DIR, or NULL if it was the last one. */
  1215. basic_block
  1216. next_dom_son (enum cdi_direction dir, basic_block bb)
  1217. {
  1218. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1219. struct et_node *next = bb->dom[dir_index]->right;
  1220. return (basic_block) (next->father->son == next ? NULL : next->data);
  1221. }
  1222. /* Return dominance availability for dominance info DIR. */
  1223. enum dom_state
  1224. dom_info_state (function *fn, enum cdi_direction dir)
  1225. {
  1226. if (!fn->cfg)
  1227. return DOM_NONE;
  1228. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1229. return fn->cfg->x_dom_computed[dir_index];
  1230. }
  1231. enum dom_state
  1232. dom_info_state (enum cdi_direction dir)
  1233. {
  1234. return dom_info_state (cfun, dir);
  1235. }
  1236. /* Set the dominance availability for dominance info DIR to NEW_STATE. */
  1237. void
  1238. set_dom_info_availability (enum cdi_direction dir, enum dom_state new_state)
  1239. {
  1240. unsigned int dir_index = dom_convert_dir_to_idx (dir);
  1241. dom_computed[dir_index] = new_state;
  1242. }
  1243. /* Returns true if dominance information for direction DIR is available. */
  1244. bool
  1245. dom_info_available_p (function *fn, enum cdi_direction dir)
  1246. {
  1247. return dom_info_state (fn, dir) != DOM_NONE;
  1248. }
  1249. bool
  1250. dom_info_available_p (enum cdi_direction dir)
  1251. {
  1252. return dom_info_available_p (cfun, dir);
  1253. }
  1254. DEBUG_FUNCTION void
  1255. debug_dominance_info (enum cdi_direction dir)
  1256. {
  1257. basic_block bb, bb2;
  1258. FOR_EACH_BB_FN (bb, cfun)
  1259. if ((bb2 = get_immediate_dominator (dir, bb)))
  1260. fprintf (stderr, "%i %i\n", bb->index, bb2->index);
  1261. }
  1262. /* Prints to stderr representation of the dominance tree (for direction DIR)
  1263. rooted in ROOT, indented by INDENT tabulators. If INDENT_FIRST is false,
  1264. the first line of the output is not indented. */
  1265. static void
  1266. debug_dominance_tree_1 (enum cdi_direction dir, basic_block root,
  1267. unsigned indent, bool indent_first)
  1268. {
  1269. basic_block son;
  1270. unsigned i;
  1271. bool first = true;
  1272. if (indent_first)
  1273. for (i = 0; i < indent; i++)
  1274. fprintf (stderr, "\t");
  1275. fprintf (stderr, "%d\t", root->index);
  1276. for (son = first_dom_son (dir, root);
  1277. son;
  1278. son = next_dom_son (dir, son))
  1279. {
  1280. debug_dominance_tree_1 (dir, son, indent + 1, !first);
  1281. first = false;
  1282. }
  1283. if (first)
  1284. fprintf (stderr, "\n");
  1285. }
  1286. /* Prints to stderr representation of the dominance tree (for direction DIR)
  1287. rooted in ROOT. */
  1288. DEBUG_FUNCTION void
  1289. debug_dominance_tree (enum cdi_direction dir, basic_block root)
  1290. {
  1291. debug_dominance_tree_1 (dir, root, 0, false);
  1292. }