Add a hint about the infamous pn_Cmp_Lg/Ne mixup in the assertion message of verify_n...
[libfirm] / ir / ir / irlinkednodeset.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @author    Michael Beck
23  * @brief     A linked nodeset.
24  * @version   $Id$
25  */
26 #ifndef _FIRM_IRLINKEDNODESET_H_
27 #define _FIRM_IRLINKEDNODESET_H_
28
29 #include "firm_types.h"
30 #include "xmalloc.h"
31 #include "list.h"
32
33 /*
34  * sebastian experimental:
35  * use ordered arrays as node sets.
36  * the guys here have made good experiences with that.
37  * Internally we use normal Firm arrays and binary
38  * search for locating the elements. Using arrays should
39  * give the sets a small footprint.
40  */
41 #undef IR_NODESET_USE_ORDERED_SETS
42
43 typedef struct ir_lnk_nodeset_entry_t {
44         ir_node     *node;  /**< the node itself */
45         list_head   list;   /**< link field for the list iterator */
46 } ir_lnk_nodeset_entry_t;
47
48 #define HashSet          ir_lnk_nodeset_t
49 #define HashSetIterator  ir_lnk_nodeset_iterator_t
50 #define ValueType        ir_lnk_nodeset_entry_t
51 #define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
52 #define DO_REHASH
53 #define NO_ITERATOR
54
55 #include "hashset.h"
56
57 #undef NO_ITERATOR
58 #undef DO_REHASH
59 #undef ADDITIONAL_DATA
60 #undef ValueType
61 #undef HashSetIterator
62 #undef HashSet
63
64 typedef struct ir_lnk_nodeset_t ir_lnk_nodeset_t;
65 typedef struct ir_lnk_nodeset_iterator_t {
66         list_head              *iter;       /**< points to the list head of the last element */
67         const ir_lnk_nodeset_t *nodeset;    /**< lithe nodeset of this iterator. */
68 } ir_lnk_nodeset_iterator_t;
69
70 /**
71  * Initializes a linked nodeset with default size.
72  *
73  * @param nodeset      Pointer to allocated space for the nodeset
74  */
75 void ir_lnk_nodeset_init(ir_lnk_nodeset_t *nodeset);
76
77 /**
78  * Initializes a linked nodeset.
79  *
80  * @param nodeset             Pointer to allocated space for the nodeset
81  * @param expected_elements   Number of elements expected in the nodeset (roughly)
82  */
83 void ir_lnk_nodeset_init_size(ir_lnk_nodeset_t *nodeset, size_t expected_elements);
84
85 /**
86  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
87  * the nodeset itself is not freed.
88  *
89  * @param nodeset   Pointer to the nodeset
90  */
91 void ir_lnk_nodeset_destroy(ir_lnk_nodeset_t *nodeset);
92
93 /**
94  * Allocates memory for a linked nodeset and initializes the set.
95  *
96  * @param expected_elements   Number of elements expected in the nodeset (roughly)
97  * @return The initialized nodeset
98  */
99 static inline ir_lnk_nodeset_t *ir_lnk_nodeset_new(size_t expected_elements) {
100         ir_lnk_nodeset_t *res = XMALLOC(ir_lnk_nodeset_t);
101         ir_lnk_nodeset_init_size(res, expected_elements);
102         return res;
103 }
104
105 /**
106  * Destroys a linked nodeset and frees the memory of the nodeset itself.
107  */
108 static inline void ir_lnk_nodeset_del(ir_lnk_nodeset_t *nodeset) {
109         ir_lnk_nodeset_destroy(nodeset);
110         xfree(nodeset);
111 }
112
113 /**
114  * Inserts a node into a linked nodeset.
115  *
116  * @param nodeset   Pointer to the nodeset
117  * @param node      node to insert into the nodeset
118  * @returns         1 if the element has been inserted,
119  *                  0 if it was already there
120  */
121 int ir_lnk_nodeset_insert(ir_lnk_nodeset_t *nodeset, ir_node *node);
122
123
124 /**
125  * Removes a node from a linked nodeset. Does nothing if the nodeset doesn't contain
126  * the node.
127  *
128  * @param nodeset  Pointer to the nodeset
129  * @param node     Node to remove from the nodeset
130  */
131 void ir_lnk_nodeset_remove(ir_lnk_nodeset_t *nodeset, const ir_node *node);
132
133 /**
134  * Tests whether a linked nodeset contains a specific node.
135  *
136  * @param nodeset   Pointer to the nodeset
137  * @param node      The pointer to find
138  * @returns         1 if nodeset contains the node, 0 else
139  */
140 int ir_lnk_nodeset_contains(const ir_lnk_nodeset_t *nodeset, const ir_node *node);
141
142 /**
143  * Returns the number of nodes contained in the linked nodeset.
144  *
145  * @param nodeset   Pointer to the nodeset
146  * @returns         Number of nodes contained in the linked nodeset
147  */
148 size_t ir_lnk_nodeset_size(const ir_lnk_nodeset_t *nodeset);
149
150 /**
151  * Initializes a nodeset iterator. Sets the iterator before the first element in
152  * the linked nodeset.
153  *
154  * @param iterator   Pointer to already allocated iterator memory
155  * @param nodeset       Pointer to the nodeset
156  */
157 void ir_lnk_nodeset_iterator_init(ir_lnk_nodeset_iterator_t *iterator,
158                                   const ir_lnk_nodeset_t *nodeset);
159
160 /**
161  * Advances the iterator and returns the current element or NULL if all elements
162  * in the linked nodeset have been processed.
163  * @attention It is not allowed to use ir_lnk_nodeset_insert or ir_lnk_nodeset_remove while
164  *            iterating over a nodeset.
165  *
166  * @param iterator  Pointer to the nodeset iterator.
167  * @returns         Next element in the nodeset or NULL
168  */
169 ir_node *ir_lnk_nodeset_iterator_next(ir_lnk_nodeset_iterator_t *iterator);
170
171 /**
172  * Removes the element the iterator currently points to.
173  *
174  * @param nodeset   Pointer to the linked nodeset
175  * @param iterator  Pointer to the linked nodeset iterator.
176  */
177 void ir_lnk_nodeset_remove_iterator(ir_lnk_nodeset_t *nodeset,
178                                     ir_lnk_nodeset_iterator_t *iterator);
179
180 #define foreach_ir_lnk_nodeset(nodeset, irn, iter) \
181         for (ir_lnk_nodeset_iterator_init(&iter, nodeset), \
182         irn = ir_lnk_nodeset_iterator_next(&iter);    \
183                 irn != NULL; irn = ir_lnk_nodeset_iterator_next(&iter))
184
185 #endif