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 void *link; /**< additional attached information */
36 list_head list; /**< link field for the list iterator */
37 } ir_valueset_entry_t;
39 #define HashSet ir_valueset_t
40 #define ValueType ir_valueset_entry_t
41 #define ADDITIONAL_DATA list_head elem_list; list_head all_iters;
46 #undef ADDITIONAL_DATA
50 typedef struct ir_valueset_t ir_valueset_t;
51 typedef struct ir_valueset_iterator_t {
52 list_head *iter; /**< points to the list head of the last element */
53 const ir_valueset_t *valueset; /**< the value set of this iterator. */
54 } ir_valueset_iterator_t;
57 * Initializes a value set with default size.
59 * @param valueset Pointer to allocated space for the value set
61 void ir_valueset_init(ir_valueset_t *valueset);
64 * Initializes a value set.
66 * @param valueset Pointer to allocated space for the value set
67 * @param expected_elements Number of elements expected in the value set (roughly)
69 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
72 * Destroys a value set and frees the memory allocated for hashtable. The memory of
73 * the value set itself is not freed.
75 * @param valueset Pointer to the value set
77 void ir_valueset_destroy(ir_valueset_t *valueset);
80 * Allocates memory for a value set and initializes it.
82 * @param expected_elements Number of elements expected in the value set (roughly)
83 * @return The initialized value set
85 static inline ir_valueset_t *ir_valueset_new(size_t expected_elements) {
86 ir_valueset_t *res = XMALLOC(ir_valueset_t);
87 ir_valueset_init_size(res, expected_elements);
92 * Destroys a value set and frees the memory of the set itself.
94 static inline void ir_valueset_del(ir_valueset_t *valueset) {
95 ir_valueset_destroy(valueset);
100 * Inserts a (value, expression) pair into a valueset if the value is not already
101 * known, else does nothing.
103 * @param valueset Pointer to the value set
104 * @param value the value to insert into the value set
105 * @param expr the expression to associate with the value
106 * @returns 1 if the value has been inserted,
107 * 0 if it was already there
109 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
112 * Sets link of the element associated with specified value.
114 * @param valueset Pointer to the value set.
115 * @param value Value of the element.
116 * @param link Link to be set in element.
118 void ir_valueset_set_link(const ir_valueset_t *valueset, const ir_node *value, void *link);
121 * Get the link of the element associated with specified value.
123 * @param valueset Pointer to the value set.
124 * @param value Value of the element.
125 * @returns Link of the element associated to the value.
127 void *ir_valueset_get_link(const ir_valueset_t *valueset, const ir_node *value);
130 * Inserts a (value, expression) pair into a valueset if the value is not already
131 * known, else replace the expression for the given value.
133 * @param valueset Pointer to the value set
134 * @param value the value to insert into the value set
135 * @param expr the expression to associate with the value
136 * @returns 1 if the value has been inserted,
137 * 0 if it was already there
139 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
142 * Get the leader expression of a specific value from the value set.
144 * @param valueset Pointer to the value set
145 * @param value The value to find
146 * @returns the associated expression of the value or NULL
148 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
151 * Removes a value from a value set. Does nothing if the value set doesn't contain
154 * @param valueset Pointer to the value set
155 * @param value value to remove from the values set
157 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
160 * Returns the number of values contained in the value set.
162 * @param valueset Pointer to the value set
163 * @returns Number of values contained in the value set
165 size_t ir_valueset_size(const ir_valueset_t *valueset);
168 * Initializes a value set iterator. Sets the iterator before the first element in
171 * @param iterator Pointer to already allocated iterator memory
172 * @param valueset Pointer to the value set
174 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
175 const ir_valueset_t *valueset);
178 * Advances the iterator and returns the current element or NULL if all elements
179 * in the value set have been processed.
180 * @note It is not allowed to use ir_valueset_insert() or ir_valueset_remove() while
181 * iterating over a nodemap.
183 * @param iterator Pointer to the value set iterator.
184 * @param expr After return contains the associated expression for the value or NULL
185 * @returns Next element in the value set or NULL
187 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
190 * Removes the element the iterator currently points to.
192 * @param valueset Pointer to the value set
193 * @param iterator Pointer to the value set iterator.
195 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
197 #define foreach_valueset(valueset, value, expr, iter) \
198 for (ir_valueset_iterator_init(&iter, valueset), \
199 value = ir_valueset_iterator_next(&iter, &expr); \
200 value != NULL; value = ir_valueset_iterator_next(&iter, &expr))