1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 The ET-forest structure is described in:
21 D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
22 J. G'omput. System Sci., 26(3):362 381, 1983.
27 #include "coretypes.h"
28 #include "alloc-pool.h"
29 #include "et-forest.h"
31 /* We do not enable this with CHECKING_P, since it is awfully slow. */
36 #include "hard-reg-set.h"
39 /* The occurrence of a node in the et tree. */
42 struct et_node *of; /* The node. */
44 struct et_occ *parent; /* Parent in the splay-tree. */
45 struct et_occ *prev; /* Left son in the splay-tree. */
46 struct et_occ *next; /* Right son in the splay-tree. */
48 int depth; /* The depth of the node is the sum of depth
49 fields on the path to the root. */
50 int min; /* The minimum value of the depth in the subtree
51 is obtained by adding sum of depth fields
52 on the path to the root. */
53 struct et_occ *min_occ; /* The occurrence in the subtree with the minimal
57 static object_allocator<et_node> et_nodes ("et_nodes pool");
58 static object_allocator<et_occ> et_occurrences ("et_occ pool");
60 /* Changes depth of OCC to D. */
63 set_depth (struct et_occ *occ, int d)
68 occ->min += d - occ->depth;
72 /* Adds D to the depth of OCC. */
75 set_depth_add (struct et_occ *occ, int d)
84 /* Sets prev field of OCC to P. */
87 set_prev (struct et_occ *occ, struct et_occ *t)
90 gcc_assert (occ != t);
98 /* Sets next field of OCC to P. */
101 set_next (struct et_occ *occ, struct et_occ *t)
104 gcc_assert (occ != t);
112 /* Recompute minimum for occurrence OCC. */
115 et_recomp_min (struct et_occ *occ)
117 struct et_occ *mson = occ->prev;
121 && mson->min > occ->next->min))
124 if (mson && mson->min < 0)
126 occ->min = mson->min + occ->depth;
127 occ->min_occ = mson->min_occ;
131 occ->min = occ->depth;
137 /* Checks whether neighborhood of OCC seems sane. */
140 et_check_occ_sanity (struct et_occ *occ)
145 gcc_assert (occ->parent != occ);
146 gcc_assert (occ->prev != occ);
147 gcc_assert (occ->next != occ);
148 gcc_assert (!occ->next || occ->next != occ->prev);
152 gcc_assert (occ->next != occ->parent);
153 gcc_assert (occ->next->parent == occ);
158 gcc_assert (occ->prev != occ->parent);
159 gcc_assert (occ->prev->parent == occ);
162 gcc_assert (!occ->parent
163 || occ->parent->prev == occ
164 || occ->parent->next == occ);
167 /* Checks whether tree rooted at OCC is sane. */
170 et_check_sanity (struct et_occ *occ)
172 et_check_occ_sanity (occ);
174 et_check_sanity (occ->prev);
176 et_check_sanity (occ->next);
179 /* Checks whether tree containing OCC is sane. */
182 et_check_tree_sanity (struct et_occ *occ)
187 et_check_sanity (occ);
190 /* For recording the paths. */
192 /* An ad-hoc constant; if the function has more blocks, this won't work,
193 but since it is used for debugging only, it does not matter. */
194 #define MAX_NODES 100000
197 static void *datas[MAX_NODES];
198 static int depths[MAX_NODES];
200 /* Records the path represented by OCC, with depth incremented by DEPTH. */
203 record_path_before_1 (struct et_occ *occ, int depth)
212 m = record_path_before_1 (occ->prev, depth);
217 fprintf (stderr, "%d (%d); ", ((basic_block) occ->of->data)->index, depth);
219 gcc_assert (len < MAX_NODES);
222 datas[len] = occ->of;
227 m = record_path_before_1 (occ->next, depth);
232 gcc_assert (mn == occ->min + depth - occ->depth);
237 /* Records the path represented by a tree containing OCC. */
240 record_path_before (struct et_occ *occ)
246 record_path_before_1 (occ, 0);
247 fprintf (stderr, "\n");
250 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
251 was not changed since the last recording. */
254 check_path_after_1 (struct et_occ *occ, int depth)
263 m = check_path_after_1 (occ->next, depth);
269 gcc_assert (depths[len] == depth && datas[len] == occ->of);
273 m = check_path_after_1 (occ->prev, depth);
278 gcc_assert (mn == occ->min + depth - occ->depth);
283 /* Checks whether the path represented by a tree containing OCC was
284 not changed since the last recording. */
287 check_path_after (struct et_occ *occ)
292 check_path_after_1 (occ, 0);
298 /* Splay the occurrence OCC to the root of the tree. */
301 et_splay (struct et_occ *occ)
303 struct et_occ *f, *gf, *ggf;
304 int occ_depth, f_depth, gf_depth;
307 record_path_before (occ);
308 et_check_tree_sanity (occ);
313 occ_depth = occ->depth;
322 set_depth_add (occ, f_depth);
323 occ->min_occ = f->min_occ;
329 set_prev (f, occ->next);
331 set_depth_add (f->prev, occ_depth);
336 set_next (f, occ->prev);
338 set_depth_add (f->next, occ_depth);
340 set_depth (f, -occ_depth);
345 et_check_tree_sanity (occ);
346 check_path_after (occ);
351 gf_depth = gf->depth;
353 set_depth_add (occ, f_depth + gf_depth);
354 occ->min_occ = gf->min_occ;
364 set_prev (gf, f->next);
365 set_prev (f, occ->next);
369 set_depth (f, -occ_depth);
370 set_depth_add (f->prev, occ_depth);
371 set_depth (gf, -f_depth);
372 set_depth_add (gf->prev, f_depth);
377 set_prev (gf, occ->next);
378 set_next (f, occ->prev);
382 set_depth (f, -occ_depth);
383 set_depth_add (f->next, occ_depth);
384 set_depth (gf, -occ_depth - f_depth);
385 set_depth_add (gf->prev, occ_depth + f_depth);
393 set_next (gf, occ->prev);
394 set_prev (f, occ->next);
398 set_depth (f, -occ_depth);
399 set_depth_add (f->prev, occ_depth);
400 set_depth (gf, -occ_depth - f_depth);
401 set_depth_add (gf->next, occ_depth + f_depth);
406 set_next (gf, f->prev);
407 set_next (f, occ->prev);
411 set_depth (f, -occ_depth);
412 set_depth_add (f->next, occ_depth);
413 set_depth (gf, -f_depth);
414 set_depth_add (gf->next, f_depth);
430 et_check_tree_sanity (occ);
435 et_check_sanity (occ);
436 check_path_after (occ);
440 /* Create a new et tree occurrence of NODE. */
442 static struct et_occ *
443 et_new_occ (struct et_node *node)
445 et_occ *nw = et_occurrences.allocate ();
459 /* Create a new et tree containing DATA. */
462 et_new_tree (void *data)
464 et_node *nw = et_nodes.allocate ();
472 nw->rightmost_occ = et_new_occ (nw);
473 nw->parent_occ = NULL;
478 /* Releases et tree T. */
481 et_free_tree (struct et_node *t)
489 et_occurrences.remove (t->rightmost_occ);
493 /* Releases et tree T without maintaining other nodes. */
496 et_free_tree_force (struct et_node *t)
498 et_occurrences.remove (t->rightmost_occ);
500 et_occurrences.remove (t->parent_occ);
504 /* Release the alloc pools, if they are empty. */
509 et_occurrences.release_if_empty ();
510 et_nodes.release_if_empty ();
513 /* Sets father of et tree T to FATHER. */
516 et_set_father (struct et_node *t, struct et_node *father)
518 struct et_node *left, *right;
519 struct et_occ *rmost, *left_part, *new_f_occ, *p;
521 /* Update the path represented in the splay tree. */
522 new_f_occ = et_new_occ (father);
524 rmost = father->rightmost_occ;
527 left_part = rmost->prev;
529 p = t->rightmost_occ;
532 set_prev (new_f_occ, left_part);
533 set_next (new_f_occ, p);
537 et_recomp_min (new_f_occ);
539 set_prev (rmost, new_f_occ);
541 if (new_f_occ->min + rmost->depth < rmost->min)
543 rmost->min = new_f_occ->min + rmost->depth;
544 rmost->min_occ = new_f_occ->min_occ;
547 t->parent_occ = new_f_occ;
549 /* Update the tree. */
565 et_check_tree_sanity (rmost);
566 record_path_before (rmost);
570 /* Splits the edge from T to its father. */
573 et_split (struct et_node *t)
575 struct et_node *father = t->father;
576 struct et_occ *r, *l, *rmost, *p_occ;
578 /* Update the path represented by the splay tree. */
579 rmost = t->rightmost_occ;
582 for (r = rmost->next; r->prev; r = r->prev)
586 r->prev->parent = NULL;
587 p_occ = t->parent_occ;
589 t->parent_occ = NULL;
592 p_occ->next->parent = NULL;
602 et_occurrences.remove (p_occ);
604 /* Update the tree. */
605 if (father->son == t)
606 father->son = t->right;
607 if (father->son == t)
611 t->left->right = t->right;
612 t->right->left = t->left;
614 t->left = t->right = NULL;
618 et_check_tree_sanity (rmost);
619 record_path_before (rmost);
621 et_check_tree_sanity (r);
622 record_path_before (r);
626 /* Finds the nearest common ancestor of the nodes N1 and N2. */
629 et_nca (struct et_node *n1, struct et_node *n2)
631 struct et_occ *o1 = n1->rightmost_occ, *o2 = n2->rightmost_occ, *om;
632 struct et_occ *l, *r, *ret;
647 if (l == o2 || (l && l->parent != NULL))
655 else if (r == o2 || (r && r->parent != NULL))
665 /* O1 and O2 are in different components of the forest. */
681 mn = o2->depth + o1->depth;
685 et_check_tree_sanity (o2);
688 if (ret && ret->min + o1->depth + o2->depth < mn)
689 return ret->min_occ->of;
694 /* Checks whether the node UP is an ancestor of the node DOWN. */
697 et_below (struct et_node *down, struct et_node *up)
699 struct et_occ *u = up->rightmost_occ, *d = down->rightmost_occ;
700 struct et_occ *l, *r;
719 if (l == d || l->parent != NULL)
725 et_check_tree_sanity (u);
732 /* In case O1 and O2 are in two different trees, we must just restore the
734 if (r && r->parent != NULL)
740 et_check_tree_sanity (u);
748 return !d->next || d->next->min + d->depth >= 0;
751 /* Returns the root of the tree that contains NODE. */
754 et_root (struct et_node *node)
756 struct et_occ *occ = node->rightmost_occ, *r;
758 /* The root of the tree corresponds to the rightmost occurrence in the
761 for (r = occ; r->next; r = r->next)