BugFix: when a node in schedule got exchanged, it is turned into Bad: do not set...
[libfirm] / ir / be / benodesets.h
1 /**
2  * A lightweight wrapper around pset to store IR nodes.
3  * In some algorithms we want a more deterministic behavior
4  * which the pset_ptr did not guarantee due to it's hash function
5  */
6 #ifndef _BENODESETS_H
7 #define _BENODESETS_H
8
9 #include "firm_types.h"
10 #include "pset.h"
11
12 typedef struct pset nodeset;
13
14 /**
15  * Calculates a hash value for a node.
16  */
17 unsigned nodeset_hash(const ir_node *n);
18
19 /**
20  * Creates a new nodeset.
21  *
22  * @param slots   Initial number of collision chains.  I.e., #slots
23  *                different keys can be hashed without collisions.
24  *
25  * @returns
26  *    created nodeset
27  */
28 static INLINE nodeset *new_nodeset(int slots)
29 {
30         return new_pset(pset_default_ptr_cmp, slots);
31 }
32
33 /**
34  * Deletes a nodeset.
35  *
36  * @param nset   the nodeset
37  *
38  * @note
39  *    This does NOT delete the elements of this node set, just it's pointers!
40  */
41 static INLINE void del_nodeset(nodeset *nset)
42 {
43         del_pset(nset);
44 }
45
46 /**
47  * Returns the number of nodes in a nodeset.
48  *
49  * @param nset   the nodeset
50  */
51 static INLINE int nodeset_count(nodeset *nset)
52 {
53         return pset_count(nset);
54 }
55
56 /**
57  * Searches a node in a node set.
58  *
59  * @param nset  the pset to search in
60  * @param key   the node to search
61  *
62  * @return
63  *    the pointer of the found node in the nodeset or NULL if it was not found
64  */
65 static INLINE ir_node *nodeset_find(nodeset *nset, ir_node *key)
66 {
67         return (ir_node *) pset_find(nset, key, nodeset_hash(key));
68 }
69
70 /**
71  * Inserts a node into a pset.
72  *
73  * @param nset  the nodeset to insert in
74  * @param key   a pointer to the element to be inserted
75  *
76  * @return a pointer to the inserted element
77  *
78  * @note
79  *    It is not possible to insert an element more than once. If an element
80  *    that should be inserted is already in the set, this functions does
81  *    nothing but returning its already existing set_entry.
82  */
83 static INLINE ir_node *nodeset_insert(nodeset *nset, ir_node *key)
84 {
85         return (ir_node *) pset_insert(nset, key, nodeset_hash(key));
86 }
87
88 /**
89  * Removes a node from a nodeset.
90  *
91  * @param nset  the nodeset to delete in
92  * @param key   a pointer to the element to be deleted
93  *
94  * @return
95  *    the pointer to the removed element
96  *
97  * @remark
98  *    The current implementation did not allow to remove non-existing elements.
99  *    @@@ so, does it do now?
100  *    Further, it is allowed to remove elements during an iteration
101  *    including the current one.
102  */
103 static INLINE ir_node *nodeset_remove(nodeset *nset, ir_node *key)
104 {
105         return (ir_node *) pset_remove(nset, key, nodeset_hash(key));
106 }
107
108 /**
109  * Returns the first node of a nodeset.
110  *
111  * @param nset  the nodeset to iterate
112  *
113  * @return a node or NULL if the set is empty
114  */
115 static INLINE ir_node *nodeset_first(nodeset *nset)
116 {
117         return (ir_node *) pset_first(nset);
118 }
119
120 /**
121  * Returns the next node of a nodeset.
122  *
123  * @param nset  the nodeset to iterate
124  *
125  * @return a node or NULL if the iteration is finished
126  */
127 static INLINE ir_node *nodeset_next(nodeset *nset)
128 {
129         return (ir_node *) pset_next(nset);
130 }
131
132 /**
133  * Breaks the iteration of a set. Must be called before
134  * the next nodeset_first() call if the iteration was NOT
135  * finished.
136  *
137  * @param nset  the nodeset
138  */
139 static INLINE void nodeset_break(nodeset *nset)
140 {
141         pset_break(nset);
142 }
143
144 /**
145  * Iterate over a node set.
146  *
147  * @param nset  the nodeset
148  * @param irn   the iterator node
149  */
150 #define foreach_nodeset(nset, irn)      for (irn = nodeset_first(nset); irn; irn = nodeset_next(nset))
151
152 #endif /* _BENODESETS_H */