et-forest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* ET-trees data structure implementation.
  2. Contributed by Pavel Nejedly
  3. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>.
  16. The ET-forest structure is described in:
  17. D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
  18. J. G'omput. System Sci., 26(3):362 381, 1983.
  19. */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "et-forest.h"
  24. #include "alloc-pool.h"
  25. /* We do not enable this with ENABLE_CHECKING, since it is awfully slow. */
  26. #undef DEBUG_ET
  27. #ifdef DEBUG_ET
  28. #include "vec.h"
  29. #include "hashtab.h"
  30. #include "hash-set.h"
  31. #include "machmode.h"
  32. #include "tm.h"
  33. #include "hard-reg-set.h"
  34. #include "input.h"
  35. #include "function.h"
  36. #include "basic-block.h"
  37. #endif
  38. /* The occurrence of a node in the et tree. */
  39. struct et_occ
  40. {
  41. struct et_node *of; /* The node. */
  42. struct et_occ *parent; /* Parent in the splay-tree. */
  43. struct et_occ *prev; /* Left son in the splay-tree. */
  44. struct et_occ *next; /* Right son in the splay-tree. */
  45. int depth; /* The depth of the node is the sum of depth
  46. fields on the path to the root. */
  47. int min; /* The minimum value of the depth in the subtree
  48. is obtained by adding sum of depth fields
  49. on the path to the root. */
  50. struct et_occ *min_occ; /* The occurrence in the subtree with the minimal
  51. depth. */
  52. };
  53. static alloc_pool et_nodes;
  54. static alloc_pool et_occurrences;
  55. /* Changes depth of OCC to D. */
  56. static inline void
  57. set_depth (struct et_occ *occ, int d)
  58. {
  59. if (!occ)
  60. return;
  61. occ->min += d - occ->depth;
  62. occ->depth = d;
  63. }
  64. /* Adds D to the depth of OCC. */
  65. static inline void
  66. set_depth_add (struct et_occ *occ, int d)
  67. {
  68. if (!occ)
  69. return;
  70. occ->min += d;
  71. occ->depth += d;
  72. }
  73. /* Sets prev field of OCC to P. */
  74. static inline void
  75. set_prev (struct et_occ *occ, struct et_occ *t)
  76. {
  77. #ifdef DEBUG_ET
  78. gcc_assert (occ != t);
  79. #endif
  80. occ->prev = t;
  81. if (t)
  82. t->parent = occ;
  83. }
  84. /* Sets next field of OCC to P. */
  85. static inline void
  86. set_next (struct et_occ *occ, struct et_occ *t)
  87. {
  88. #ifdef DEBUG_ET
  89. gcc_assert (occ != t);
  90. #endif
  91. occ->next = t;
  92. if (t)
  93. t->parent = occ;
  94. }
  95. /* Recompute minimum for occurrence OCC. */
  96. static inline void
  97. et_recomp_min (struct et_occ *occ)
  98. {
  99. struct et_occ *mson = occ->prev;
  100. if (!mson
  101. || (occ->next
  102. && mson->min > occ->next->min))
  103. mson = occ->next;
  104. if (mson && mson->min < 0)
  105. {
  106. occ->min = mson->min + occ->depth;
  107. occ->min_occ = mson->min_occ;
  108. }
  109. else
  110. {
  111. occ->min = occ->depth;
  112. occ->min_occ = occ;
  113. }
  114. }
  115. #ifdef DEBUG_ET
  116. /* Checks whether neighborhood of OCC seems sane. */
  117. static void
  118. et_check_occ_sanity (struct et_occ *occ)
  119. {
  120. if (!occ)
  121. return;
  122. gcc_assert (occ->parent != occ);
  123. gcc_assert (occ->prev != occ);
  124. gcc_assert (occ->next != occ);
  125. gcc_assert (!occ->next || occ->next != occ->prev);
  126. if (occ->next)
  127. {
  128. gcc_assert (occ->next != occ->parent);
  129. gcc_assert (occ->next->parent == occ);
  130. }
  131. if (occ->prev)
  132. {
  133. gcc_assert (occ->prev != occ->parent);
  134. gcc_assert (occ->prev->parent == occ);
  135. }
  136. gcc_assert (!occ->parent
  137. || occ->parent->prev == occ
  138. || occ->parent->next == occ);
  139. }
  140. /* Checks whether tree rooted at OCC is sane. */
  141. static void
  142. et_check_sanity (struct et_occ *occ)
  143. {
  144. et_check_occ_sanity (occ);
  145. if (occ->prev)
  146. et_check_sanity (occ->prev);
  147. if (occ->next)
  148. et_check_sanity (occ->next);
  149. }
  150. /* Checks whether tree containing OCC is sane. */
  151. static void
  152. et_check_tree_sanity (struct et_occ *occ)
  153. {
  154. while (occ->parent)
  155. occ = occ->parent;
  156. et_check_sanity (occ);
  157. }
  158. /* For recording the paths. */
  159. /* An ad-hoc constant; if the function has more blocks, this won't work,
  160. but since it is used for debugging only, it does not matter. */
  161. #define MAX_NODES 100000
  162. static int len;
  163. static void *datas[MAX_NODES];
  164. static int depths[MAX_NODES];
  165. /* Records the path represented by OCC, with depth incremented by DEPTH. */
  166. static int
  167. record_path_before_1 (struct et_occ *occ, int depth)
  168. {
  169. int mn, m;
  170. depth += occ->depth;
  171. mn = depth;
  172. if (occ->prev)
  173. {
  174. m = record_path_before_1 (occ->prev, depth);
  175. if (m < mn)
  176. mn = m;
  177. }
  178. fprintf (stderr, "%d (%d); ", ((basic_block) occ->of->data)->index, depth);
  179. gcc_assert (len < MAX_NODES);
  180. depths[len] = depth;
  181. datas[len] = occ->of;
  182. len++;
  183. if (occ->next)
  184. {
  185. m = record_path_before_1 (occ->next, depth);
  186. if (m < mn)
  187. mn = m;
  188. }
  189. gcc_assert (mn == occ->min + depth - occ->depth);
  190. return mn;
  191. }
  192. /* Records the path represented by a tree containing OCC. */
  193. static void
  194. record_path_before (struct et_occ *occ)
  195. {
  196. while (occ->parent)
  197. occ = occ->parent;
  198. len = 0;
  199. record_path_before_1 (occ, 0);
  200. fprintf (stderr, "\n");
  201. }
  202. /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
  203. was not changed since the last recording. */
  204. static int
  205. check_path_after_1 (struct et_occ *occ, int depth)
  206. {
  207. int mn, m;
  208. depth += occ->depth;
  209. mn = depth;
  210. if (occ->next)
  211. {
  212. m = check_path_after_1 (occ->next, depth);
  213. if (m < mn)
  214. mn = m;
  215. }
  216. len--;
  217. gcc_assert (depths[len] == depth && datas[len] == occ->of);
  218. if (occ->prev)
  219. {
  220. m = check_path_after_1 (occ->prev, depth);
  221. if (m < mn)
  222. mn = m;
  223. }
  224. gcc_assert (mn == occ->min + depth - occ->depth);
  225. return mn;
  226. }
  227. /* Checks whether the path represented by a tree containing OCC was
  228. not changed since the last recording. */
  229. static void
  230. check_path_after (struct et_occ *occ)
  231. {
  232. while (occ->parent)
  233. occ = occ->parent;
  234. check_path_after_1 (occ, 0);
  235. gcc_assert (!len);
  236. }
  237. #endif
  238. /* Splay the occurrence OCC to the root of the tree. */
  239. static void
  240. et_splay (struct et_occ *occ)
  241. {
  242. struct et_occ *f, *gf, *ggf;
  243. int occ_depth, f_depth, gf_depth;
  244. #ifdef DEBUG_ET
  245. record_path_before (occ);
  246. et_check_tree_sanity (occ);
  247. #endif
  248. while (occ->parent)
  249. {
  250. occ_depth = occ->depth;
  251. f = occ->parent;
  252. f_depth = f->depth;
  253. gf = f->parent;
  254. if (!gf)
  255. {
  256. set_depth_add (occ, f_depth);
  257. occ->min_occ = f->min_occ;
  258. occ->min = f->min;
  259. if (f->prev == occ)
  260. {
  261. /* zig */
  262. set_prev (f, occ->next);
  263. set_next (occ, f);
  264. set_depth_add (f->prev, occ_depth);
  265. }
  266. else
  267. {
  268. /* zag */
  269. set_next (f, occ->prev);
  270. set_prev (occ, f);
  271. set_depth_add (f->next, occ_depth);
  272. }
  273. set_depth (f, -occ_depth);
  274. occ->parent = NULL;
  275. et_recomp_min (f);
  276. #ifdef DEBUG_ET
  277. et_check_tree_sanity (occ);
  278. check_path_after (occ);
  279. #endif
  280. return;
  281. }
  282. gf_depth = gf->depth;
  283. set_depth_add (occ, f_depth + gf_depth);
  284. occ->min_occ = gf->min_occ;
  285. occ->min = gf->min;
  286. ggf = gf->parent;
  287. if (gf->prev == f)
  288. {
  289. if (f->prev == occ)
  290. {
  291. /* zig zig */
  292. set_prev (gf, f->next);
  293. set_prev (f, occ->next);
  294. set_next (occ, f);
  295. set_next (f, gf);
  296. set_depth (f, -occ_depth);
  297. set_depth_add (f->prev, occ_depth);
  298. set_depth (gf, -f_depth);
  299. set_depth_add (gf->prev, f_depth);
  300. }
  301. else
  302. {
  303. /* zag zig */
  304. set_prev (gf, occ->next);
  305. set_next (f, occ->prev);
  306. set_prev (occ, f);
  307. set_next (occ, gf);
  308. set_depth (f, -occ_depth);
  309. set_depth_add (f->next, occ_depth);
  310. set_depth (gf, -occ_depth - f_depth);
  311. set_depth_add (gf->prev, occ_depth + f_depth);
  312. }
  313. }
  314. else
  315. {
  316. if (f->prev == occ)
  317. {
  318. /* zig zag */
  319. set_next (gf, occ->prev);
  320. set_prev (f, occ->next);
  321. set_prev (occ, gf);
  322. set_next (occ, f);
  323. set_depth (f, -occ_depth);
  324. set_depth_add (f->prev, occ_depth);
  325. set_depth (gf, -occ_depth - f_depth);
  326. set_depth_add (gf->next, occ_depth + f_depth);
  327. }
  328. else
  329. {
  330. /* zag zag */
  331. set_next (gf, f->prev);
  332. set_next (f, occ->prev);
  333. set_prev (occ, f);
  334. set_prev (f, gf);
  335. set_depth (f, -occ_depth);
  336. set_depth_add (f->next, occ_depth);
  337. set_depth (gf, -f_depth);
  338. set_depth_add (gf->next, f_depth);
  339. }
  340. }
  341. occ->parent = ggf;
  342. if (ggf)
  343. {
  344. if (ggf->prev == gf)
  345. ggf->prev = occ;
  346. else
  347. ggf->next = occ;
  348. }
  349. et_recomp_min (gf);
  350. et_recomp_min (f);
  351. #ifdef DEBUG_ET
  352. et_check_tree_sanity (occ);
  353. #endif
  354. }
  355. #ifdef DEBUG_ET
  356. et_check_sanity (occ);
  357. check_path_after (occ);
  358. #endif
  359. }
  360. /* Create a new et tree occurrence of NODE. */
  361. static struct et_occ *
  362. et_new_occ (struct et_node *node)
  363. {
  364. struct et_occ *nw;
  365. if (!et_occurrences)
  366. et_occurrences = create_alloc_pool ("et_occ pool", sizeof (struct et_occ), 300);
  367. nw = (struct et_occ *) pool_alloc (et_occurrences);
  368. nw->of = node;
  369. nw->parent = NULL;
  370. nw->prev = NULL;
  371. nw->next = NULL;
  372. nw->depth = 0;
  373. nw->min_occ = nw;
  374. nw->min = 0;
  375. return nw;
  376. }
  377. /* Create a new et tree containing DATA. */
  378. struct et_node *
  379. et_new_tree (void *data)
  380. {
  381. struct et_node *nw;
  382. if (!et_nodes)
  383. et_nodes = create_alloc_pool ("et_node pool", sizeof (struct et_node), 300);
  384. nw = (struct et_node *) pool_alloc (et_nodes);
  385. nw->data = data;
  386. nw->father = NULL;
  387. nw->left = NULL;
  388. nw->right = NULL;
  389. nw->son = NULL;
  390. nw->rightmost_occ = et_new_occ (nw);
  391. nw->parent_occ = NULL;
  392. return nw;
  393. }
  394. /* Releases et tree T. */
  395. void
  396. et_free_tree (struct et_node *t)
  397. {
  398. while (t->son)
  399. et_split (t->son);
  400. if (t->father)
  401. et_split (t);
  402. pool_free (et_occurrences, t->rightmost_occ);
  403. pool_free (et_nodes, t);
  404. }
  405. /* Releases et tree T without maintaining other nodes. */
  406. void
  407. et_free_tree_force (struct et_node *t)
  408. {
  409. pool_free (et_occurrences, t->rightmost_occ);
  410. if (t->parent_occ)
  411. pool_free (et_occurrences, t->parent_occ);
  412. pool_free (et_nodes, t);
  413. }
  414. /* Release the alloc pools, if they are empty. */
  415. void
  416. et_free_pools (void)
  417. {
  418. free_alloc_pool_if_empty (&et_occurrences);
  419. free_alloc_pool_if_empty (&et_nodes);
  420. }
  421. /* Sets father of et tree T to FATHER. */
  422. void
  423. et_set_father (struct et_node *t, struct et_node *father)
  424. {
  425. struct et_node *left, *right;
  426. struct et_occ *rmost, *left_part, *new_f_occ, *p;
  427. /* Update the path represented in the splay tree. */
  428. new_f_occ = et_new_occ (father);
  429. rmost = father->rightmost_occ;
  430. et_splay (rmost);
  431. left_part = rmost->prev;
  432. p = t->rightmost_occ;
  433. et_splay (p);
  434. set_prev (new_f_occ, left_part);
  435. set_next (new_f_occ, p);
  436. p->depth++;
  437. p->min++;
  438. et_recomp_min (new_f_occ);
  439. set_prev (rmost, new_f_occ);
  440. if (new_f_occ->min + rmost->depth < rmost->min)
  441. {
  442. rmost->min = new_f_occ->min + rmost->depth;
  443. rmost->min_occ = new_f_occ->min_occ;
  444. }
  445. t->parent_occ = new_f_occ;
  446. /* Update the tree. */
  447. t->father = father;
  448. right = father->son;
  449. if (right)
  450. left = right->left;
  451. else
  452. left = right = t;
  453. left->right = t;
  454. right->left = t;
  455. t->left = left;
  456. t->right = right;
  457. father->son = t;
  458. #ifdef DEBUG_ET
  459. et_check_tree_sanity (rmost);
  460. record_path_before (rmost);
  461. #endif
  462. }
  463. /* Splits the edge from T to its father. */
  464. void
  465. et_split (struct et_node *t)
  466. {
  467. struct et_node *father = t->father;
  468. struct et_occ *r, *l, *rmost, *p_occ;
  469. /* Update the path represented by the splay tree. */
  470. rmost = t->rightmost_occ;
  471. et_splay (rmost);
  472. for (r = rmost->next; r->prev; r = r->prev)
  473. continue;
  474. et_splay (r);
  475. r->prev->parent = NULL;
  476. p_occ = t->parent_occ;
  477. et_splay (p_occ);
  478. t->parent_occ = NULL;
  479. l = p_occ->prev;
  480. p_occ->next->parent = NULL;
  481. set_prev (r, l);
  482. et_recomp_min (r);
  483. et_splay (rmost);
  484. rmost->depth = 0;
  485. rmost->min = 0;
  486. pool_free (et_occurrences, p_occ);
  487. /* Update the tree. */
  488. if (father->son == t)
  489. father->son = t->right;
  490. if (father->son == t)
  491. father->son = NULL;
  492. else
  493. {
  494. t->left->right = t->right;
  495. t->right->left = t->left;
  496. }
  497. t->left = t->right = NULL;
  498. t->father = NULL;
  499. #ifdef DEBUG_ET
  500. et_check_tree_sanity (rmost);
  501. record_path_before (rmost);
  502. et_check_tree_sanity (r);
  503. record_path_before (r);
  504. #endif
  505. }
  506. /* Finds the nearest common ancestor of the nodes N1 and N2. */
  507. struct et_node *
  508. et_nca (struct et_node *n1, struct et_node *n2)
  509. {
  510. struct et_occ *o1 = n1->rightmost_occ, *o2 = n2->rightmost_occ, *om;
  511. struct et_occ *l, *r, *ret;
  512. int mn;
  513. if (n1 == n2)
  514. return n1;
  515. et_splay (o1);
  516. l = o1->prev;
  517. r = o1->next;
  518. if (l)
  519. l->parent = NULL;
  520. if (r)
  521. r->parent = NULL;
  522. et_splay (o2);
  523. if (l == o2 || (l && l->parent != NULL))
  524. {
  525. ret = o2->next;
  526. set_prev (o1, o2);
  527. if (r)
  528. r->parent = o1;
  529. }
  530. else if (r == o2 || (r && r->parent != NULL))
  531. {
  532. ret = o2->prev;
  533. set_next (o1, o2);
  534. if (l)
  535. l->parent = o1;
  536. }
  537. else
  538. {
  539. /* O1 and O2 are in different components of the forest. */
  540. if (l)
  541. l->parent = o1;
  542. if (r)
  543. r->parent = o1;
  544. return NULL;
  545. }
  546. if (0 < o2->depth)
  547. {
  548. om = o1;
  549. mn = o1->depth;
  550. }
  551. else
  552. {
  553. om = o2;
  554. mn = o2->depth + o1->depth;
  555. }
  556. #ifdef DEBUG_ET
  557. et_check_tree_sanity (o2);
  558. #endif
  559. if (ret && ret->min + o1->depth + o2->depth < mn)
  560. return ret->min_occ->of;
  561. else
  562. return om->of;
  563. }
  564. /* Checks whether the node UP is an ancestor of the node DOWN. */
  565. bool
  566. et_below (struct et_node *down, struct et_node *up)
  567. {
  568. struct et_occ *u = up->rightmost_occ, *d = down->rightmost_occ;
  569. struct et_occ *l, *r;
  570. if (up == down)
  571. return true;
  572. et_splay (u);
  573. l = u->prev;
  574. r = u->next;
  575. if (!l)
  576. return false;
  577. l->parent = NULL;
  578. if (r)
  579. r->parent = NULL;
  580. et_splay (d);
  581. if (l == d || l->parent != NULL)
  582. {
  583. if (r)
  584. r->parent = u;
  585. set_prev (u, d);
  586. #ifdef DEBUG_ET
  587. et_check_tree_sanity (u);
  588. #endif
  589. }
  590. else
  591. {
  592. l->parent = u;
  593. /* In case O1 and O2 are in two different trees, we must just restore the
  594. original state. */
  595. if (r && r->parent != NULL)
  596. set_next (u, d);
  597. else
  598. set_next (u, r);
  599. #ifdef DEBUG_ET
  600. et_check_tree_sanity (u);
  601. #endif
  602. return false;
  603. }
  604. if (0 >= d->depth)
  605. return false;
  606. return !d->next || d->next->min + d->depth >= 0;
  607. }
  608. /* Returns the root of the tree that contains NODE. */
  609. struct et_node *
  610. et_root (struct et_node *node)
  611. {
  612. struct et_occ *occ = node->rightmost_occ, *r;
  613. /* The root of the tree corresponds to the rightmost occurrence in the
  614. represented path. */
  615. et_splay (occ);
  616. for (r = occ; r->next; r = r->next)
  617. continue;
  618. et_splay (r);
  619. return r->of;
  620. }