]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/et-forest.c
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. */
39 #include "hard-reg-set.h"
42 #include "basic-block.h"
45 /* The occurrence of a node in the et tree. */
48 struct et_node
*of
; /* The node. */
50 struct et_occ
*parent
; /* Parent in the splay-tree. */
51 struct et_occ
*prev
; /* Left son in the splay-tree. */
52 struct et_occ
*next
; /* Right son in the splay-tree. */
54 int depth
; /* The depth of the node is the sum of depth
55 fields on the path to the root. */
56 int min
; /* The minimum value of the depth in the subtree
57 is obtained by adding sum of depth fields
58 on the path to the root. */
59 struct et_occ
*min_occ
; /* The occurrence in the subtree with the minimal
62 /* Pool allocation new operator. */
63 inline void *operator new (size_t)
65 return pool
.allocate ();
68 /* Delete operator utilizing pool allocation. */
69 inline void operator delete (void *ptr
)
71 pool
.remove ((et_occ
*) ptr
);
74 /* Memory allocation pool. */
75 static pool_allocator
<et_occ
> pool
;
79 pool_allocator
<et_node
> et_node::pool ("et_nodes pool", 300);
80 pool_allocator
<et_occ
> et_occ::pool ("et_occ pool", 300);
82 /* Changes depth of OCC to D. */
85 set_depth (struct et_occ
*occ
, int d
)
90 occ
->min
+= d
- occ
->depth
;
94 /* Adds D to the depth of OCC. */
97 set_depth_add (struct et_occ
*occ
, int d
)
106 /* Sets prev field of OCC to P. */
109 set_prev (struct et_occ
*occ
, struct et_occ
*t
)
112 gcc_assert (occ
!= t
);
120 /* Sets next field of OCC to P. */
123 set_next (struct et_occ
*occ
, struct et_occ
*t
)
126 gcc_assert (occ
!= t
);
134 /* Recompute minimum for occurrence OCC. */
137 et_recomp_min (struct et_occ
*occ
)
139 struct et_occ
*mson
= occ
->prev
;
143 && mson
->min
> occ
->next
->min
))
146 if (mson
&& mson
->min
< 0)
148 occ
->min
= mson
->min
+ occ
->depth
;
149 occ
->min_occ
= mson
->min_occ
;
153 occ
->min
= occ
->depth
;
159 /* Checks whether neighborhood of OCC seems sane. */
162 et_check_occ_sanity (struct et_occ
*occ
)
167 gcc_assert (occ
->parent
!= occ
);
168 gcc_assert (occ
->prev
!= occ
);
169 gcc_assert (occ
->next
!= occ
);
170 gcc_assert (!occ
->next
|| occ
->next
!= occ
->prev
);
174 gcc_assert (occ
->next
!= occ
->parent
);
175 gcc_assert (occ
->next
->parent
== occ
);
180 gcc_assert (occ
->prev
!= occ
->parent
);
181 gcc_assert (occ
->prev
->parent
== occ
);
184 gcc_assert (!occ
->parent
185 || occ
->parent
->prev
== occ
186 || occ
->parent
->next
== occ
);
189 /* Checks whether tree rooted at OCC is sane. */
192 et_check_sanity (struct et_occ
*occ
)
194 et_check_occ_sanity (occ
);
196 et_check_sanity (occ
->prev
);
198 et_check_sanity (occ
->next
);
201 /* Checks whether tree containing OCC is sane. */
204 et_check_tree_sanity (struct et_occ
*occ
)
209 et_check_sanity (occ
);
212 /* For recording the paths. */
214 /* An ad-hoc constant; if the function has more blocks, this won't work,
215 but since it is used for debugging only, it does not matter. */
216 #define MAX_NODES 100000
219 static void *datas
[MAX_NODES
];
220 static int depths
[MAX_NODES
];
222 /* Records the path represented by OCC, with depth incremented by DEPTH. */
225 record_path_before_1 (struct et_occ
*occ
, int depth
)
234 m
= record_path_before_1 (occ
->prev
, depth
);
239 fprintf (stderr
, "%d (%d); ", ((basic_block
) occ
->of
->data
)->index
, depth
);
241 gcc_assert (len
< MAX_NODES
);
244 datas
[len
] = occ
->of
;
249 m
= record_path_before_1 (occ
->next
, depth
);
254 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
259 /* Records the path represented by a tree containing OCC. */
262 record_path_before (struct et_occ
*occ
)
268 record_path_before_1 (occ
, 0);
269 fprintf (stderr
, "\n");
272 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
273 was not changed since the last recording. */
276 check_path_after_1 (struct et_occ
*occ
, int depth
)
285 m
= check_path_after_1 (occ
->next
, depth
);
291 gcc_assert (depths
[len
] == depth
&& datas
[len
] == occ
->of
);
295 m
= check_path_after_1 (occ
->prev
, depth
);
300 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
305 /* Checks whether the path represented by a tree containing OCC was
306 not changed since the last recording. */
309 check_path_after (struct et_occ
*occ
)
314 check_path_after_1 (occ
, 0);
320 /* Splay the occurrence OCC to the root of the tree. */
323 et_splay (struct et_occ
*occ
)
325 struct et_occ
*f
, *gf
, *ggf
;
326 int occ_depth
, f_depth
, gf_depth
;
329 record_path_before (occ
);
330 et_check_tree_sanity (occ
);
335 occ_depth
= occ
->depth
;
344 set_depth_add (occ
, f_depth
);
345 occ
->min_occ
= f
->min_occ
;
351 set_prev (f
, occ
->next
);
353 set_depth_add (f
->prev
, occ_depth
);
358 set_next (f
, occ
->prev
);
360 set_depth_add (f
->next
, occ_depth
);
362 set_depth (f
, -occ_depth
);
367 et_check_tree_sanity (occ
);
368 check_path_after (occ
);
373 gf_depth
= gf
->depth
;
375 set_depth_add (occ
, f_depth
+ gf_depth
);
376 occ
->min_occ
= gf
->min_occ
;
386 set_prev (gf
, f
->next
);
387 set_prev (f
, occ
->next
);
391 set_depth (f
, -occ_depth
);
392 set_depth_add (f
->prev
, occ_depth
);
393 set_depth (gf
, -f_depth
);
394 set_depth_add (gf
->prev
, f_depth
);
399 set_prev (gf
, occ
->next
);
400 set_next (f
, occ
->prev
);
404 set_depth (f
, -occ_depth
);
405 set_depth_add (f
->next
, occ_depth
);
406 set_depth (gf
, -occ_depth
- f_depth
);
407 set_depth_add (gf
->prev
, occ_depth
+ f_depth
);
415 set_next (gf
, occ
->prev
);
416 set_prev (f
, occ
->next
);
420 set_depth (f
, -occ_depth
);
421 set_depth_add (f
->prev
, occ_depth
);
422 set_depth (gf
, -occ_depth
- f_depth
);
423 set_depth_add (gf
->next
, occ_depth
+ f_depth
);
428 set_next (gf
, f
->prev
);
429 set_next (f
, occ
->prev
);
433 set_depth (f
, -occ_depth
);
434 set_depth_add (f
->next
, occ_depth
);
435 set_depth (gf
, -f_depth
);
436 set_depth_add (gf
->next
, f_depth
);
452 et_check_tree_sanity (occ
);
457 et_check_sanity (occ
);
458 check_path_after (occ
);
462 /* Create a new et tree occurrence of NODE. */
464 static struct et_occ
*
465 et_new_occ (struct et_node
*node
)
467 et_occ
*nw
= new et_occ
;
481 /* Create a new et tree containing DATA. */
484 et_new_tree (void *data
)
496 nw
->rightmost_occ
= et_new_occ (nw
);
497 nw
->parent_occ
= NULL
;
502 /* Releases et tree T. */
505 et_free_tree (struct et_node
*t
)
513 delete t
->rightmost_occ
;
517 /* Releases et tree T without maintaining other nodes. */
520 et_free_tree_force (struct et_node
*t
)
522 delete t
->rightmost_occ
;
524 delete t
->parent_occ
;
528 /* Release the alloc pools, if they are empty. */
533 et_occ::pool
.release_if_empty ();
534 et_node::pool
.release_if_empty ();
537 /* Sets father of et tree T to FATHER. */
540 et_set_father (struct et_node
*t
, struct et_node
*father
)
542 struct et_node
*left
, *right
;
543 struct et_occ
*rmost
, *left_part
, *new_f_occ
, *p
;
545 /* Update the path represented in the splay tree. */
546 new_f_occ
= et_new_occ (father
);
548 rmost
= father
->rightmost_occ
;
551 left_part
= rmost
->prev
;
553 p
= t
->rightmost_occ
;
556 set_prev (new_f_occ
, left_part
);
557 set_next (new_f_occ
, p
);
561 et_recomp_min (new_f_occ
);
563 set_prev (rmost
, new_f_occ
);
565 if (new_f_occ
->min
+ rmost
->depth
< rmost
->min
)
567 rmost
->min
= new_f_occ
->min
+ rmost
->depth
;
568 rmost
->min_occ
= new_f_occ
->min_occ
;
571 t
->parent_occ
= new_f_occ
;
573 /* Update the tree. */
589 et_check_tree_sanity (rmost
);
590 record_path_before (rmost
);
594 /* Splits the edge from T to its father. */
597 et_split (struct et_node
*t
)
599 struct et_node
*father
= t
->father
;
600 struct et_occ
*r
, *l
, *rmost
, *p_occ
;
602 /* Update the path represented by the splay tree. */
603 rmost
= t
->rightmost_occ
;
606 for (r
= rmost
->next
; r
->prev
; r
= r
->prev
)
610 r
->prev
->parent
= NULL
;
611 p_occ
= t
->parent_occ
;
613 t
->parent_occ
= NULL
;
616 p_occ
->next
->parent
= NULL
;
628 /* Update the tree. */
629 if (father
->son
== t
)
630 father
->son
= t
->right
;
631 if (father
->son
== t
)
635 t
->left
->right
= t
->right
;
636 t
->right
->left
= t
->left
;
638 t
->left
= t
->right
= NULL
;
642 et_check_tree_sanity (rmost
);
643 record_path_before (rmost
);
645 et_check_tree_sanity (r
);
646 record_path_before (r
);
650 /* Finds the nearest common ancestor of the nodes N1 and N2. */
653 et_nca (struct et_node
*n1
, struct et_node
*n2
)
655 struct et_occ
*o1
= n1
->rightmost_occ
, *o2
= n2
->rightmost_occ
, *om
;
656 struct et_occ
*l
, *r
, *ret
;
671 if (l
== o2
|| (l
&& l
->parent
!= NULL
))
679 else if (r
== o2
|| (r
&& r
->parent
!= NULL
))
689 /* O1 and O2 are in different components of the forest. */
705 mn
= o2
->depth
+ o1
->depth
;
709 et_check_tree_sanity (o2
);
712 if (ret
&& ret
->min
+ o1
->depth
+ o2
->depth
< mn
)
713 return ret
->min_occ
->of
;
718 /* Checks whether the node UP is an ancestor of the node DOWN. */
721 et_below (struct et_node
*down
, struct et_node
*up
)
723 struct et_occ
*u
= up
->rightmost_occ
, *d
= down
->rightmost_occ
;
724 struct et_occ
*l
, *r
;
743 if (l
== d
|| l
->parent
!= NULL
)
749 et_check_tree_sanity (u
);
756 /* In case O1 and O2 are in two different trees, we must just restore the
758 if (r
&& r
->parent
!= NULL
)
764 et_check_tree_sanity (u
);
772 return !d
->next
|| d
->next
->min
+ d
->depth
>= 0;
775 /* Returns the root of the tree that contains NODE. */
778 et_root (struct et_node
*node
)
780 struct et_occ
*occ
= node
->rightmost_occ
, *r
;
782 /* The root of the tree corresponds to the rightmost occurrence in the
785 for (r
= occ
; r
->next
; r
= r
->next
)