1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002-2013 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 "et-forest.h"
29 #include "alloc-pool.h"
31 /* We do not enable this with ENABLE_CHECKING, since it is awfully slow. */
35 #include "basic-block.h" /* To access index in record_path_before_1. */
38 /* The occurrence of a node in the et tree. */
41 struct et_node
*of
; /* The node. */
43 struct et_occ
*parent
; /* Parent in the splay-tree. */
44 struct et_occ
*prev
; /* Left son in the splay-tree. */
45 struct et_occ
*next
; /* Right son in the splay-tree. */
47 int depth
; /* The depth of the node is the sum of depth
48 fields on the path to the root. */
49 int min
; /* The minimum value of the depth in the subtree
50 is obtained by adding sum of depth fields
51 on the path to the root. */
52 struct et_occ
*min_occ
; /* The occurrence in the subtree with the minimal
56 static alloc_pool et_nodes
;
57 static alloc_pool et_occurrences
;
59 /* Changes depth of OCC to D. */
62 set_depth (struct et_occ
*occ
, int d
)
67 occ
->min
+= d
- occ
->depth
;
71 /* Adds D to the depth of OCC. */
74 set_depth_add (struct et_occ
*occ
, int d
)
83 /* Sets prev field of OCC to P. */
86 set_prev (struct et_occ
*occ
, struct et_occ
*t
)
89 gcc_assert (occ
!= t
);
97 /* Sets next field of OCC to P. */
100 set_next (struct et_occ
*occ
, struct et_occ
*t
)
103 gcc_assert (occ
!= t
);
111 /* Recompute minimum for occurrence OCC. */
114 et_recomp_min (struct et_occ
*occ
)
116 struct et_occ
*mson
= occ
->prev
;
120 && mson
->min
> occ
->next
->min
))
123 if (mson
&& mson
->min
< 0)
125 occ
->min
= mson
->min
+ occ
->depth
;
126 occ
->min_occ
= mson
->min_occ
;
130 occ
->min
= occ
->depth
;
136 /* Checks whether neighborhood of OCC seems sane. */
139 et_check_occ_sanity (struct et_occ
*occ
)
144 gcc_assert (occ
->parent
!= occ
);
145 gcc_assert (occ
->prev
!= occ
);
146 gcc_assert (occ
->next
!= occ
);
147 gcc_assert (!occ
->next
|| occ
->next
!= occ
->prev
);
151 gcc_assert (occ
->next
!= occ
->parent
);
152 gcc_assert (occ
->next
->parent
== occ
);
157 gcc_assert (occ
->prev
!= occ
->parent
);
158 gcc_assert (occ
->prev
->parent
== occ
);
161 gcc_assert (!occ
->parent
162 || occ
->parent
->prev
== occ
163 || occ
->parent
->next
== occ
);
166 /* Checks whether tree rooted at OCC is sane. */
169 et_check_sanity (struct et_occ
*occ
)
171 et_check_occ_sanity (occ
);
173 et_check_sanity (occ
->prev
);
175 et_check_sanity (occ
->next
);
178 /* Checks whether tree containing OCC is sane. */
181 et_check_tree_sanity (struct et_occ
*occ
)
186 et_check_sanity (occ
);
189 /* For recording the paths. */
191 /* An ad-hoc constant; if the function has more blocks, this won't work,
192 but since it is used for debugging only, it does not matter. */
193 #define MAX_NODES 100000
196 static void *datas
[MAX_NODES
];
197 static int depths
[MAX_NODES
];
199 /* Records the path represented by OCC, with depth incremented by DEPTH. */
202 record_path_before_1 (struct et_occ
*occ
, int depth
)
211 m
= record_path_before_1 (occ
->prev
, depth
);
216 fprintf (stderr
, "%d (%d); ", ((basic_block
) occ
->of
->data
)->index
, depth
);
218 gcc_assert (len
< MAX_NODES
);
221 datas
[len
] = occ
->of
;
226 m
= record_path_before_1 (occ
->next
, depth
);
231 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
236 /* Records the path represented by a tree containing OCC. */
239 record_path_before (struct et_occ
*occ
)
245 record_path_before_1 (occ
, 0);
246 fprintf (stderr
, "\n");
249 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
250 was not changed since the last recording. */
253 check_path_after_1 (struct et_occ
*occ
, int depth
)
262 m
= check_path_after_1 (occ
->next
, depth
);
268 gcc_assert (depths
[len
] == depth
&& datas
[len
] == occ
->of
);
272 m
= check_path_after_1 (occ
->prev
, depth
);
277 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
282 /* Checks whether the path represented by a tree containing OCC was
283 not changed since the last recording. */
286 check_path_after (struct et_occ
*occ
)
291 check_path_after_1 (occ
, 0);
297 /* Splay the occurrence OCC to the root of the tree. */
300 et_splay (struct et_occ
*occ
)
302 struct et_occ
*f
, *gf
, *ggf
;
303 int occ_depth
, f_depth
, gf_depth
;
306 record_path_before (occ
);
307 et_check_tree_sanity (occ
);
312 occ_depth
= occ
->depth
;
321 set_depth_add (occ
, f_depth
);
322 occ
->min_occ
= f
->min_occ
;
328 set_prev (f
, occ
->next
);
330 set_depth_add (f
->prev
, occ_depth
);
335 set_next (f
, occ
->prev
);
337 set_depth_add (f
->next
, occ_depth
);
339 set_depth (f
, -occ_depth
);
344 et_check_tree_sanity (occ
);
345 check_path_after (occ
);
350 gf_depth
= gf
->depth
;
352 set_depth_add (occ
, f_depth
+ gf_depth
);
353 occ
->min_occ
= gf
->min_occ
;
363 set_prev (gf
, f
->next
);
364 set_prev (f
, occ
->next
);
368 set_depth (f
, -occ_depth
);
369 set_depth_add (f
->prev
, occ_depth
);
370 set_depth (gf
, -f_depth
);
371 set_depth_add (gf
->prev
, f_depth
);
376 set_prev (gf
, occ
->next
);
377 set_next (f
, occ
->prev
);
381 set_depth (f
, -occ_depth
);
382 set_depth_add (f
->next
, occ_depth
);
383 set_depth (gf
, -occ_depth
- f_depth
);
384 set_depth_add (gf
->prev
, occ_depth
+ f_depth
);
392 set_next (gf
, occ
->prev
);
393 set_prev (f
, occ
->next
);
397 set_depth (f
, -occ_depth
);
398 set_depth_add (f
->prev
, occ_depth
);
399 set_depth (gf
, -occ_depth
- f_depth
);
400 set_depth_add (gf
->next
, occ_depth
+ f_depth
);
405 set_next (gf
, f
->prev
);
406 set_next (f
, occ
->prev
);
410 set_depth (f
, -occ_depth
);
411 set_depth_add (f
->next
, occ_depth
);
412 set_depth (gf
, -f_depth
);
413 set_depth_add (gf
->next
, f_depth
);
429 et_check_tree_sanity (occ
);
434 et_check_sanity (occ
);
435 check_path_after (occ
);
439 /* Create a new et tree occurrence of NODE. */
441 static struct et_occ
*
442 et_new_occ (struct et_node
*node
)
447 et_occurrences
= create_alloc_pool ("et_occ pool", sizeof (struct et_occ
), 300);
448 nw
= (struct et_occ
*) pool_alloc (et_occurrences
);
462 /* Create a new et tree containing DATA. */
465 et_new_tree (void *data
)
470 et_nodes
= create_alloc_pool ("et_node pool", sizeof (struct et_node
), 300);
471 nw
= (struct et_node
*) pool_alloc (et_nodes
);
479 nw
->rightmost_occ
= et_new_occ (nw
);
480 nw
->parent_occ
= NULL
;
485 /* Releases et tree T. */
488 et_free_tree (struct et_node
*t
)
496 pool_free (et_occurrences
, t
->rightmost_occ
);
497 pool_free (et_nodes
, t
);
500 /* Releases et tree T without maintaining other nodes. */
503 et_free_tree_force (struct et_node
*t
)
505 pool_free (et_occurrences
, t
->rightmost_occ
);
507 pool_free (et_occurrences
, t
->parent_occ
);
508 pool_free (et_nodes
, t
);
511 /* Release the alloc pools, if they are empty. */
516 free_alloc_pool_if_empty (&et_occurrences
);
517 free_alloc_pool_if_empty (&et_nodes
);
520 /* Sets father of et tree T to FATHER. */
523 et_set_father (struct et_node
*t
, struct et_node
*father
)
525 struct et_node
*left
, *right
;
526 struct et_occ
*rmost
, *left_part
, *new_f_occ
, *p
;
528 /* Update the path represented in the splay tree. */
529 new_f_occ
= et_new_occ (father
);
531 rmost
= father
->rightmost_occ
;
534 left_part
= rmost
->prev
;
536 p
= t
->rightmost_occ
;
539 set_prev (new_f_occ
, left_part
);
540 set_next (new_f_occ
, p
);
544 et_recomp_min (new_f_occ
);
546 set_prev (rmost
, new_f_occ
);
548 if (new_f_occ
->min
+ rmost
->depth
< rmost
->min
)
550 rmost
->min
= new_f_occ
->min
+ rmost
->depth
;
551 rmost
->min_occ
= new_f_occ
->min_occ
;
554 t
->parent_occ
= new_f_occ
;
556 /* Update the tree. */
572 et_check_tree_sanity (rmost
);
573 record_path_before (rmost
);
577 /* Splits the edge from T to its father. */
580 et_split (struct et_node
*t
)
582 struct et_node
*father
= t
->father
;
583 struct et_occ
*r
, *l
, *rmost
, *p_occ
;
585 /* Update the path represented by the splay tree. */
586 rmost
= t
->rightmost_occ
;
589 for (r
= rmost
->next
; r
->prev
; r
= r
->prev
)
593 r
->prev
->parent
= NULL
;
594 p_occ
= t
->parent_occ
;
596 t
->parent_occ
= NULL
;
599 p_occ
->next
->parent
= NULL
;
609 pool_free (et_occurrences
, p_occ
);
611 /* Update the tree. */
612 if (father
->son
== t
)
613 father
->son
= t
->right
;
614 if (father
->son
== t
)
618 t
->left
->right
= t
->right
;
619 t
->right
->left
= t
->left
;
621 t
->left
= t
->right
= NULL
;
625 et_check_tree_sanity (rmost
);
626 record_path_before (rmost
);
628 et_check_tree_sanity (r
);
629 record_path_before (r
);
633 /* Finds the nearest common ancestor of the nodes N1 and N2. */
636 et_nca (struct et_node
*n1
, struct et_node
*n2
)
638 struct et_occ
*o1
= n1
->rightmost_occ
, *o2
= n2
->rightmost_occ
, *om
;
639 struct et_occ
*l
, *r
, *ret
;
654 if (l
== o2
|| (l
&& l
->parent
!= NULL
))
662 else if (r
== o2
|| (r
&& r
->parent
!= NULL
))
672 /* O1 and O2 are in different components of the forest. */
688 mn
= o2
->depth
+ o1
->depth
;
692 et_check_tree_sanity (o2
);
695 if (ret
&& ret
->min
+ o1
->depth
+ o2
->depth
< mn
)
696 return ret
->min_occ
->of
;
701 /* Checks whether the node UP is an ancestor of the node DOWN. */
704 et_below (struct et_node
*down
, struct et_node
*up
)
706 struct et_occ
*u
= up
->rightmost_occ
, *d
= down
->rightmost_occ
;
707 struct et_occ
*l
, *r
;
726 if (l
== d
|| l
->parent
!= NULL
)
732 et_check_tree_sanity (u
);
739 /* In case O1 and O2 are in two different trees, we must just restore the
741 if (r
&& r
->parent
!= NULL
)
747 et_check_tree_sanity (u
);
755 return !d
->next
|| d
->next
->min
+ d
->depth
>= 0;
758 /* Returns the root of the tree that contains NODE. */
761 et_root (struct et_node
*node
)
763 struct et_occ
*occ
= node
->rightmost_occ
, *r
;
765 /* The root of the tree corresponds to the rightmost occurrence in the
768 for (r
= occ
; r
->next
; r
= r
->next
)