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