remove $Id$, it doesn't work with git anyway
[libfirm] / ir / ir / valueset.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 value set, containing expression for values.
24  */
25 #ifndef _FIRM_VALUESET_H_
26 #define _FIRM_VALUESET_H_
27
28 #include "firm_types.h"
29 #include "xmalloc.h"
30 #include "list.h"
31
32 typedef struct ir_valueset_entry_t {
33         ir_node     *value;  /**< the represented value */
34         ir_node     *expr;   /**< the leader expression for the value in the current set */
35         list_head   list;    /**< link field for the list iterator */
36 } ir_valueset_entry_t;
37
38 #define HashSet          ir_valueset_t
39 #define HashSetIterator  ir_valueset_iterator_t
40 #define ValueType        ir_valueset_entry_t
41 #define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
42 #undef DO_REHASH
43 #define NO_ITERATOR
44
45 #include "hashset.h"
46
47 #undef NO_ITERATOR
48 #undef ADDITIONAL_DATA
49 #undef ValueType
50 #undef HashSetIterator
51 #undef HashSet
52
53 typedef struct ir_valueset_t ir_valueset_t;
54 typedef struct ir_valueset_iterator_t {
55         list_head           *iter;       /**< points to the list head of the last element */
56         const ir_valueset_t *valueset;   /**< the value set of this iterator. */
57 } ir_valueset_iterator_t;
58
59 /**
60  * Initializes a value set with default size.
61  *
62  * @param valueset      Pointer to allocated space for the value set
63  */
64 void ir_valueset_init(ir_valueset_t *valueset);
65
66 /**
67  * Initializes a value set.
68  *
69  * @param valueset            Pointer to allocated space for the value set
70  * @param expected_elements   Number of elements expected in the value set (roughly)
71  */
72 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
73
74 /**
75  * Destroys a value set and frees the memory allocated for hashtable. The memory of
76  * the value set itself is not freed.
77  *
78  * @param valueset   Pointer to the value set
79  */
80 void ir_valueset_destroy(ir_valueset_t *valueset);
81
82 /**
83  * Allocates memory for a value set and initializes it.
84  *
85  * @param expected_elements   Number of elements expected in the value set (roughly)
86  * @return The initialized value set
87  */
88 static inline ir_valueset_t *ir_valueset_new(size_t expected_elements) {
89         ir_valueset_t *res = XMALLOC(ir_valueset_t);
90         ir_valueset_init_size(res, expected_elements);
91         return res;
92 }
93
94 /**
95  * Destroys a value set and frees the memory of the set itself.
96  */
97 static inline void ir_valueset_del(ir_valueset_t *valueset) {
98         ir_valueset_destroy(valueset);
99         xfree(valueset);
100 }
101
102 /**
103  * Inserts a (value, expression) pair into a valueset if the value is not already
104  * known, else does nothing.
105  *
106  * @param valueset  Pointer to the value set
107  * @param value     the value to insert into the value set
108  * @param expr      the expression to associate with the value
109  * @returns         1 if the value has been inserted,
110  *                  0 if it was already there
111  */
112 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
113
114 /**
115  * Inserts a (value, expression) pair into a valueset if the value is not already
116  * known, else replace the expression for the given value.
117  *
118  * @param valueset  Pointer to the value set
119  * @param value     the value to insert into the value set
120  * @param expr      the expression to associate with the value
121  * @returns         1 if the value has been inserted,
122  *                  0 if it was already there
123  */
124 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
125
126 /**
127  * Get the leader expression of a specific value from the value set.
128  *
129  * @param valueset  Pointer to the value set
130  * @param value     The value to find
131  * @returns         the associated expression of the value or NULL
132  */
133 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
134
135 /**
136  * Removes a value from a value set. Does nothing if the value set doesn't contain
137  * the value.
138  *
139  * @param valueset  Pointer to the value set
140  * @param value     value to remove from the values et
141  */
142 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
143
144 /**
145  * Returns the number of values contained in the value set.
146  *
147  * @param valueset  Pointer to the value set
148  * @returns         Number of values contained in the value set
149  */
150 size_t ir_valueset_size(const ir_valueset_t *valueset);
151
152 /**
153  * Initializes a value set iterator. Sets the iterator before the first element in
154  * the value set.
155  *
156  * @param iterator   Pointer to already allocated iterator memory
157  * @param valueset   Pointer to the value set
158  */
159 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
160                                const ir_valueset_t *valueset);
161
162 /**
163  * Advances the iterator and returns the current element or NULL if all elements
164  * in the value set have been processed.
165  * @note It is not allowed to use ir_valueset_insert() or ir_valueset_remove() while
166  *            iterating over a nodemap.
167  *
168  * @param iterator  Pointer to the value set iterator.
169  * @param expr      After return contains the associated expression for the value or NULL
170  * @returns         Next element in the value set or NULL
171  */
172 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
173
174 /**
175  * Removes the element the iterator currently points to.
176  *
177  * @param valueset  Pointer to the value set
178  * @param iterator  Pointer to the value set iterator.
179  */
180 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
181
182 #define foreach_valueset(valueset, value, expr, iter) \
183         for (ir_valueset_iterator_init(&iter, valueset), \
184         value = ir_valueset_iterator_next(&iter, &expr);    \
185                 value != NULL; value = ir_valueset_iterator_next(&iter, &expr))
186
187 #endif