1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002-2015 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 ENABLE_CHECKING, since it is awfully slow. */
40 #include "hard-reg-set.h"
43 #include "basic-block.h"
46 /* The occurrence of a node in the et tree. */
49 struct et_node *of; /* The node. */
51 struct et_occ *parent; /* Parent in the splay-tree. */
52 struct et_occ *prev; /* Left son in the splay-tree. */
53 struct et_occ *next; /* Right son in the splay-tree. */
55 int depth; /* The depth of the node is the sum of depth
56 fields on the path to the root. */
57 int min; /* The minimum value of the depth in the subtree
58 is obtained by adding sum of depth fields
59 on the path to the root. */
60 struct et_occ *min_occ; /* The occurrence in the subtree with the minimal
63 /* Pool allocation new operator. */
64 inline void *operator new (size_t)
66 return pool.allocate ();
69 /* Delete operator utilizing pool allocation. */
70 inline void operator delete (void *ptr)
72 pool.remove ((et_occ *) ptr);
75 /* Memory allocation pool. */
76 static pool_allocator<et_occ> pool;
80 pool_allocator<et_node> et_node::pool ("et_nodes pool", 300);
81 pool_allocator<et_occ> et_occ::pool ("et_occ pool", 300);
83 /* Changes depth of OCC to D. */
86 set_depth (struct et_occ *occ, int d)
91 occ->min += d - occ->depth;
95 /* Adds D to the depth of OCC. */
98 set_depth_add (struct et_occ *occ, int d)
107 /* Sets prev field of OCC to P. */
110 set_prev (struct et_occ *occ, struct et_occ *t)
113 gcc_assert (occ != t);
121 /* Sets next field of OCC to P. */
124 set_next (struct et_occ *occ, struct et_occ *t)
127 gcc_assert (occ != t);
135 /* Recompute minimum for occurrence OCC. */
138 et_recomp_min (struct et_occ *occ)
140 struct et_occ *mson = occ->prev;
144 && mson->min > occ->next->min))
147 if (mson && mson->min < 0)
149 occ->min = mson->min + occ->depth;
150 occ->min_occ = mson->min_occ;
154 occ->min = occ->depth;
160 /* Checks whether neighborhood of OCC seems sane. */
163 et_check_occ_sanity (struct et_occ *occ)
168 gcc_assert (occ->parent != occ);
169 gcc_assert (occ->prev != occ);
170 gcc_assert (occ->next != occ);
171 gcc_assert (!occ->next || occ->next != occ->prev);
175 gcc_assert (occ->next != occ->parent);
176 gcc_assert (occ->next->parent == occ);
181 gcc_assert (occ->prev != occ->parent);
182 gcc_assert (occ->prev->parent == occ);
185 gcc_assert (!occ->parent
186 || occ->parent->prev == occ
187 || occ->parent->next == occ);
190 /* Checks whether tree rooted at OCC is sane. */
193 et_check_sanity (struct et_occ *occ)
195 et_check_occ_sanity (occ);
197 et_check_sanity (occ->prev);
199 et_check_sanity (occ->next);
202 /* Checks whether tree containing OCC is sane. */
205 et_check_tree_sanity (struct et_occ *occ)
210 et_check_sanity (occ);
213 /* For recording the paths. */
215 /* An ad-hoc constant; if the function has more blocks, this won't work,
216 but since it is used for debugging only, it does not matter. */
217 #define MAX_NODES 100000
220 static void *datas[MAX_NODES];
221 static int depths[MAX_NODES];
223 /* Records the path represented by OCC, with depth incremented by DEPTH. */
226 record_path_before_1 (struct et_occ *occ, int depth)
235 m = record_path_before_1 (occ->prev, depth);
240 fprintf (stderr, "%d (%d); ", ((basic_block) occ->of->data)->index, depth);
242 gcc_assert (len < MAX_NODES);
245 datas[len] = occ->of;
250 m = record_path_before_1 (occ->next, depth);
255 gcc_assert (mn == occ->min + depth - occ->depth);
260 /* Records the path represented by a tree containing OCC. */
263 record_path_before (struct et_occ *occ)
269 record_path_before_1 (occ, 0);
270 fprintf (stderr, "\n");
273 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
274 was not changed since the last recording. */
277 check_path_after_1 (struct et_occ *occ, int depth)
286 m = check_path_after_1 (occ->next, depth);
292 gcc_assert (depths[len] == depth && datas[len] == occ->of);
296 m = check_path_after_1 (occ->prev, depth);
301 gcc_assert (mn == occ->min + depth - occ->depth);
306 /* Checks whether the path represented by a tree containing OCC was
307 not changed since the last recording. */
310 check_path_after (struct et_occ *occ)
315 check_path_after_1 (occ, 0);
321 /* Splay the occurrence OCC to the root of the tree. */
324 et_splay (struct et_occ *occ)
326 struct et_occ *f, *gf, *ggf;
327 int occ_depth, f_depth, gf_depth;
330 record_path_before (occ);
331 et_check_tree_sanity (occ);
336 occ_depth = occ->depth;
345 set_depth_add (occ, f_depth);
346 occ->min_occ = f->min_occ;
352 set_prev (f, occ->next);
354 set_depth_add (f->prev, occ_depth);
359 set_next (f, occ->prev);
361 set_depth_add (f->next, occ_depth);
363 set_depth (f, -occ_depth);
368 et_check_tree_sanity (occ);
369 check_path_after (occ);
374 gf_depth = gf->depth;
376 set_depth_add (occ, f_depth + gf_depth);
377 occ->min_occ = gf->min_occ;
387 set_prev (gf, f->next);
388 set_prev (f, occ->next);
392 set_depth (f, -occ_depth);
393 set_depth_add (f->prev, occ_depth);
394 set_depth (gf, -f_depth);
395 set_depth_add (gf->prev, f_depth);
400 set_prev (gf, occ->next);
401 set_next (f, occ->prev);
405 set_depth (f, -occ_depth);
406 set_depth_add (f->next, occ_depth);
407 set_depth (gf, -occ_depth - f_depth);
408 set_depth_add (gf->prev, occ_depth + f_depth);
416 set_next (gf, occ->prev);
417 set_prev (f, occ->next);
421 set_depth (f, -occ_depth);
422 set_depth_add (f->prev, occ_depth);
423 set_depth (gf, -occ_depth - f_depth);
424 set_depth_add (gf->next, occ_depth + f_depth);
429 set_next (gf, f->prev);
430 set_next (f, occ->prev);
434 set_depth (f, -occ_depth);
435 set_depth_add (f->next, occ_depth);
436 set_depth (gf, -f_depth);
437 set_depth_add (gf->next, f_depth);
453 et_check_tree_sanity (occ);
458 et_check_sanity (occ);
459 check_path_after (occ);
463 /* Create a new et tree occurrence of NODE. */
465 static struct et_occ *
466 et_new_occ (struct et_node *node)
468 et_occ *nw = new et_occ;
482 /* Create a new et tree containing DATA. */
485 et_new_tree (void *data)
497 nw->rightmost_occ = et_new_occ (nw);
498 nw->parent_occ = NULL;
503 /* Releases et tree T. */
506 et_free_tree (struct et_node *t)
514 delete t->rightmost_occ;
518 /* Releases et tree T without maintaining other nodes. */
521 et_free_tree_force (struct et_node *t)
523 delete t->rightmost_occ;
525 delete t->parent_occ;
529 /* Release the alloc pools, if they are empty. */
534 et_occ::pool.release_if_empty ();
535 et_node::pool.release_if_empty ();
538 /* Sets father of et tree T to FATHER. */
541 et_set_father (struct et_node *t, struct et_node *father)
543 struct et_node *left, *right;
544 struct et_occ *rmost, *left_part, *new_f_occ, *p;
546 /* Update the path represented in the splay tree. */
547 new_f_occ = et_new_occ (father);
549 rmost = father->rightmost_occ;
552 left_part = rmost->prev;
554 p = t->rightmost_occ;
557 set_prev (new_f_occ, left_part);
558 set_next (new_f_occ, p);
562 et_recomp_min (new_f_occ);
564 set_prev (rmost, new_f_occ);
566 if (new_f_occ->min + rmost->depth < rmost->min)
568 rmost->min = new_f_occ->min + rmost->depth;
569 rmost->min_occ = new_f_occ->min_occ;
572 t->parent_occ = new_f_occ;
574 /* Update the tree. */
590 et_check_tree_sanity (rmost);
591 record_path_before (rmost);
595 /* Splits the edge from T to its father. */
598 et_split (struct et_node *t)
600 struct et_node *father = t->father;
601 struct et_occ *r, *l, *rmost, *p_occ;
603 /* Update the path represented by the splay tree. */
604 rmost = t->rightmost_occ;
607 for (r = rmost->next; r->prev; r = r->prev)
611 r->prev->parent = NULL;
612 p_occ = t->parent_occ;
614 t->parent_occ = NULL;
617 p_occ->next->parent = NULL;
629 /* Update the tree. */
630 if (father->son == t)
631 father->son = t->right;
632 if (father->son == t)
636 t->left->right = t->right;
637 t->right->left = t->left;
639 t->left = t->right = NULL;
643 et_check_tree_sanity (rmost);
644 record_path_before (rmost);
646 et_check_tree_sanity (r);
647 record_path_before (r);
651 /* Finds the nearest common ancestor of the nodes N1 and N2. */
654 et_nca (struct et_node *n1, struct et_node *n2)
656 struct et_occ *o1 = n1->rightmost_occ, *o2 = n2->rightmost_occ, *om;
657 struct et_occ *l, *r, *ret;
672 if (l == o2 || (l && l->parent != NULL))
680 else if (r == o2 || (r && r->parent != NULL))
690 /* O1 and O2 are in different components of the forest. */
706 mn = o2->depth + o1->depth;
710 et_check_tree_sanity (o2);
713 if (ret && ret->min + o1->depth + o2->depth < mn)
714 return ret->min_occ->of;
719 /* Checks whether the node UP is an ancestor of the node DOWN. */
722 et_below (struct et_node *down, struct et_node *up)
724 struct et_occ *u = up->rightmost_occ, *d = down->rightmost_occ;
725 struct et_occ *l, *r;
744 if (l == d || l->parent != NULL)
750 et_check_tree_sanity (u);
757 /* In case O1 and O2 are in two different trees, we must just restore the
759 if (r && r->parent != NULL)
765 et_check_tree_sanity (u);
773 return !d->next || d->next->min + d->depth >= 0;
776 /* Returns the root of the tree that contains NODE. */
779 et_root (struct et_node *node)
781 struct et_occ *occ = node->rightmost_occ, *r;
783 /* The root of the tree corresponds to the rightmost occurrence in the
786 for (r = occ; r->next; r = r->next)