1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002-2014 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. */
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
64 static alloc_pool et_nodes
;
65 static alloc_pool et_occurrences
;
67 /* Changes depth of OCC to D. */
70 set_depth (struct et_occ
*occ
, int d
)
75 occ
->min
+= d
- occ
->depth
;
79 /* Adds D to the depth of OCC. */
82 set_depth_add (struct et_occ
*occ
, int d
)
91 /* Sets prev field of OCC to P. */
94 set_prev (struct et_occ
*occ
, struct et_occ
*t
)
97 gcc_assert (occ
!= t
);
105 /* Sets next field of OCC to P. */
108 set_next (struct et_occ
*occ
, struct et_occ
*t
)
111 gcc_assert (occ
!= t
);
119 /* Recompute minimum for occurrence OCC. */
122 et_recomp_min (struct et_occ
*occ
)
124 struct et_occ
*mson
= occ
->prev
;
128 && mson
->min
> occ
->next
->min
))
131 if (mson
&& mson
->min
< 0)
133 occ
->min
= mson
->min
+ occ
->depth
;
134 occ
->min_occ
= mson
->min_occ
;
138 occ
->min
= occ
->depth
;
144 /* Checks whether neighborhood of OCC seems sane. */
147 et_check_occ_sanity (struct et_occ
*occ
)
152 gcc_assert (occ
->parent
!= occ
);
153 gcc_assert (occ
->prev
!= occ
);
154 gcc_assert (occ
->next
!= occ
);
155 gcc_assert (!occ
->next
|| occ
->next
!= occ
->prev
);
159 gcc_assert (occ
->next
!= occ
->parent
);
160 gcc_assert (occ
->next
->parent
== occ
);
165 gcc_assert (occ
->prev
!= occ
->parent
);
166 gcc_assert (occ
->prev
->parent
== occ
);
169 gcc_assert (!occ
->parent
170 || occ
->parent
->prev
== occ
171 || occ
->parent
->next
== occ
);
174 /* Checks whether tree rooted at OCC is sane. */
177 et_check_sanity (struct et_occ
*occ
)
179 et_check_occ_sanity (occ
);
181 et_check_sanity (occ
->prev
);
183 et_check_sanity (occ
->next
);
186 /* Checks whether tree containing OCC is sane. */
189 et_check_tree_sanity (struct et_occ
*occ
)
194 et_check_sanity (occ
);
197 /* For recording the paths. */
199 /* An ad-hoc constant; if the function has more blocks, this won't work,
200 but since it is used for debugging only, it does not matter. */
201 #define MAX_NODES 100000
204 static void *datas
[MAX_NODES
];
205 static int depths
[MAX_NODES
];
207 /* Records the path represented by OCC, with depth incremented by DEPTH. */
210 record_path_before_1 (struct et_occ
*occ
, int depth
)
219 m
= record_path_before_1 (occ
->prev
, depth
);
224 fprintf (stderr
, "%d (%d); ", ((basic_block
) occ
->of
->data
)->index
, depth
);
226 gcc_assert (len
< MAX_NODES
);
229 datas
[len
] = occ
->of
;
234 m
= record_path_before_1 (occ
->next
, depth
);
239 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
244 /* Records the path represented by a tree containing OCC. */
247 record_path_before (struct et_occ
*occ
)
253 record_path_before_1 (occ
, 0);
254 fprintf (stderr
, "\n");
257 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
258 was not changed since the last recording. */
261 check_path_after_1 (struct et_occ
*occ
, int depth
)
270 m
= check_path_after_1 (occ
->next
, depth
);
276 gcc_assert (depths
[len
] == depth
&& datas
[len
] == occ
->of
);
280 m
= check_path_after_1 (occ
->prev
, depth
);
285 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
290 /* Checks whether the path represented by a tree containing OCC was
291 not changed since the last recording. */
294 check_path_after (struct et_occ
*occ
)
299 check_path_after_1 (occ
, 0);
305 /* Splay the occurrence OCC to the root of the tree. */
308 et_splay (struct et_occ
*occ
)
310 struct et_occ
*f
, *gf
, *ggf
;
311 int occ_depth
, f_depth
, gf_depth
;
314 record_path_before (occ
);
315 et_check_tree_sanity (occ
);
320 occ_depth
= occ
->depth
;
329 set_depth_add (occ
, f_depth
);
330 occ
->min_occ
= f
->min_occ
;
336 set_prev (f
, occ
->next
);
338 set_depth_add (f
->prev
, occ_depth
);
343 set_next (f
, occ
->prev
);
345 set_depth_add (f
->next
, occ_depth
);
347 set_depth (f
, -occ_depth
);
352 et_check_tree_sanity (occ
);
353 check_path_after (occ
);
358 gf_depth
= gf
->depth
;
360 set_depth_add (occ
, f_depth
+ gf_depth
);
361 occ
->min_occ
= gf
->min_occ
;
371 set_prev (gf
, f
->next
);
372 set_prev (f
, occ
->next
);
376 set_depth (f
, -occ_depth
);
377 set_depth_add (f
->prev
, occ_depth
);
378 set_depth (gf
, -f_depth
);
379 set_depth_add (gf
->prev
, f_depth
);
384 set_prev (gf
, occ
->next
);
385 set_next (f
, occ
->prev
);
389 set_depth (f
, -occ_depth
);
390 set_depth_add (f
->next
, occ_depth
);
391 set_depth (gf
, -occ_depth
- f_depth
);
392 set_depth_add (gf
->prev
, occ_depth
+ f_depth
);
400 set_next (gf
, occ
->prev
);
401 set_prev (f
, occ
->next
);
405 set_depth (f
, -occ_depth
);
406 set_depth_add (f
->prev
, occ_depth
);
407 set_depth (gf
, -occ_depth
- f_depth
);
408 set_depth_add (gf
->next
, occ_depth
+ f_depth
);
413 set_next (gf
, f
->prev
);
414 set_next (f
, occ
->prev
);
418 set_depth (f
, -occ_depth
);
419 set_depth_add (f
->next
, occ_depth
);
420 set_depth (gf
, -f_depth
);
421 set_depth_add (gf
->next
, f_depth
);
437 et_check_tree_sanity (occ
);
442 et_check_sanity (occ
);
443 check_path_after (occ
);
447 /* Create a new et tree occurrence of NODE. */
449 static struct et_occ
*
450 et_new_occ (struct et_node
*node
)
455 et_occurrences
= create_alloc_pool ("et_occ pool", sizeof (struct et_occ
), 300);
456 nw
= (struct et_occ
*) pool_alloc (et_occurrences
);
470 /* Create a new et tree containing DATA. */
473 et_new_tree (void *data
)
478 et_nodes
= create_alloc_pool ("et_node pool", sizeof (struct et_node
), 300);
479 nw
= (struct et_node
*) pool_alloc (et_nodes
);
487 nw
->rightmost_occ
= et_new_occ (nw
);
488 nw
->parent_occ
= NULL
;
493 /* Releases et tree T. */
496 et_free_tree (struct et_node
*t
)
504 pool_free (et_occurrences
, t
->rightmost_occ
);
505 pool_free (et_nodes
, t
);
508 /* Releases et tree T without maintaining other nodes. */
511 et_free_tree_force (struct et_node
*t
)
513 pool_free (et_occurrences
, t
->rightmost_occ
);
515 pool_free (et_occurrences
, t
->parent_occ
);
516 pool_free (et_nodes
, t
);
519 /* Release the alloc pools, if they are empty. */
524 free_alloc_pool_if_empty (&et_occurrences
);
525 free_alloc_pool_if_empty (&et_nodes
);
528 /* Sets father of et tree T to FATHER. */
531 et_set_father (struct et_node
*t
, struct et_node
*father
)
533 struct et_node
*left
, *right
;
534 struct et_occ
*rmost
, *left_part
, *new_f_occ
, *p
;
536 /* Update the path represented in the splay tree. */
537 new_f_occ
= et_new_occ (father
);
539 rmost
= father
->rightmost_occ
;
542 left_part
= rmost
->prev
;
544 p
= t
->rightmost_occ
;
547 set_prev (new_f_occ
, left_part
);
548 set_next (new_f_occ
, p
);
552 et_recomp_min (new_f_occ
);
554 set_prev (rmost
, new_f_occ
);
556 if (new_f_occ
->min
+ rmost
->depth
< rmost
->min
)
558 rmost
->min
= new_f_occ
->min
+ rmost
->depth
;
559 rmost
->min_occ
= new_f_occ
->min_occ
;
562 t
->parent_occ
= new_f_occ
;
564 /* Update the tree. */
580 et_check_tree_sanity (rmost
);
581 record_path_before (rmost
);
585 /* Splits the edge from T to its father. */
588 et_split (struct et_node
*t
)
590 struct et_node
*father
= t
->father
;
591 struct et_occ
*r
, *l
, *rmost
, *p_occ
;
593 /* Update the path represented by the splay tree. */
594 rmost
= t
->rightmost_occ
;
597 for (r
= rmost
->next
; r
->prev
; r
= r
->prev
)
601 r
->prev
->parent
= NULL
;
602 p_occ
= t
->parent_occ
;
604 t
->parent_occ
= NULL
;
607 p_occ
->next
->parent
= NULL
;
617 pool_free (et_occurrences
, p_occ
);
619 /* Update the tree. */
620 if (father
->son
== t
)
621 father
->son
= t
->right
;
622 if (father
->son
== t
)
626 t
->left
->right
= t
->right
;
627 t
->right
->left
= t
->left
;
629 t
->left
= t
->right
= NULL
;
633 et_check_tree_sanity (rmost
);
634 record_path_before (rmost
);
636 et_check_tree_sanity (r
);
637 record_path_before (r
);
641 /* Finds the nearest common ancestor of the nodes N1 and N2. */
644 et_nca (struct et_node
*n1
, struct et_node
*n2
)
646 struct et_occ
*o1
= n1
->rightmost_occ
, *o2
= n2
->rightmost_occ
, *om
;
647 struct et_occ
*l
, *r
, *ret
;
662 if (l
== o2
|| (l
&& l
->parent
!= NULL
))
670 else if (r
== o2
|| (r
&& r
->parent
!= NULL
))
680 /* O1 and O2 are in different components of the forest. */
696 mn
= o2
->depth
+ o1
->depth
;
700 et_check_tree_sanity (o2
);
703 if (ret
&& ret
->min
+ o1
->depth
+ o2
->depth
< mn
)
704 return ret
->min_occ
->of
;
709 /* Checks whether the node UP is an ancestor of the node DOWN. */
712 et_below (struct et_node
*down
, struct et_node
*up
)
714 struct et_occ
*u
= up
->rightmost_occ
, *d
= down
->rightmost_occ
;
715 struct et_occ
*l
, *r
;
734 if (l
== d
|| l
->parent
!= NULL
)
740 et_check_tree_sanity (u
);
747 /* In case O1 and O2 are in two different trees, we must just restore the
749 if (r
&& r
->parent
!= NULL
)
755 et_check_tree_sanity (u
);
763 return !d
->next
|| d
->next
->min
+ d
->depth
>= 0;
766 /* Returns the root of the tree that contains NODE. */
769 et_root (struct et_node
*node
)
771 struct et_occ
*occ
= node
->rightmost_occ
, *r
;
773 /* The root of the tree corresponds to the rightmost occurrence in the
776 for (r
= occ
; r
->next
; r
= r
->next
)