2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @author Michael Beck
23 * @brief A value set, containing expression for values.
25 #ifndef _FIRM_VALUESET_H_
26 #define _FIRM_VALUESET_H_
28 #include "firm_types.h"
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;
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;
48 #undef ADDITIONAL_DATA
50 #undef HashSetIterator
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;
60 * Initializes a value set with default size.
62 * @param valueset Pointer to allocated space for the value set
64 void ir_valueset_init(ir_valueset_t *valueset);
67 * Initializes a value set.
69 * @param valueset Pointer to allocated space for the value set
70 * @param expected_elements Number of elements expected in the value set (roughly)
72 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
75 * Destroys a value set and frees the memory allocated for hashtable. The memory of
76 * the value set itself is not freed.
78 * @param valueset Pointer to the value set
80 void ir_valueset_destroy(ir_valueset_t *valueset);
83 * Allocates memory for a value set and initializes it.
85 * @param expected_elements Number of elements expected in the value set (roughly)
86 * @return The initialized value set
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);
95 * Destroys a value set and frees the memory of the set itself.
97 static inline void ir_valueset_del(ir_valueset_t *valueset) {
98 ir_valueset_destroy(valueset);
103 * Inserts a (value, expression) pair into a valueset if the value is not already
104 * known, else does nothing.
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
112 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
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.
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
124 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
127 * Get the leader expression of a specific value from the value set.
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
133 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
136 * Removes a value from a value set. Does nothing if the value set doesn't contain
139 * @param valueset Pointer to the value set
140 * @param value value to remove from the values et
142 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
145 * Returns the number of values contained in the value set.
147 * @param valueset Pointer to the value set
148 * @returns Number of values contained in the value set
150 size_t ir_valueset_size(const ir_valueset_t *valueset);
153 * Initializes a value set iterator. Sets the iterator before the first element in
156 * @param iterator Pointer to already allocated iterator memory
157 * @param valueset Pointer to the value set
159 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
160 const ir_valueset_t *valueset);
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.
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
172 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
175 * Removes the element the iterator currently points to.
177 * @param valueset Pointer to the value set
178 * @param iterator Pointer to the value set iterator.
180 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
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))