irgraph: Use get_irg_obstack() instead of accessing irg->obst directly.
[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         void        *link;   /**< additional attached information */
36         list_head   list;    /**< link field for the list iterator */
37 } ir_valueset_entry_t;
38
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;
42 #undef DO_REHASH
43
44 #include "hashset.h"
45
46 #undef ADDITIONAL_DATA
47 #undef ValueType
48 #undef HashSet
49
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;
55
56 /**
57  * Initializes a value set with default size.
58  *
59  * @param valueset      Pointer to allocated space for the value set
60  */
61 void ir_valueset_init(ir_valueset_t *valueset);
62
63 /**
64  * Initializes a value set.
65  *
66  * @param valueset            Pointer to allocated space for the value set
67  * @param expected_elements   Number of elements expected in the value set (roughly)
68  */
69 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
70
71 /**
72  * Destroys a value set and frees the memory allocated for hashtable. The memory of
73  * the value set itself is not freed.
74  *
75  * @param valueset   Pointer to the value set
76  */
77 void ir_valueset_destroy(ir_valueset_t *valueset);
78
79 /**
80  * Allocates memory for a value set and initializes it.
81  *
82  * @param expected_elements   Number of elements expected in the value set (roughly)
83  * @return The initialized value set
84  */
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);
88         return res;
89 }
90
91 /**
92  * Destroys a value set and frees the memory of the set itself.
93  */
94 static inline void ir_valueset_del(ir_valueset_t *valueset) {
95         ir_valueset_destroy(valueset);
96         xfree(valueset);
97 }
98
99 /**
100  * Inserts a (value, expression) pair into a valueset if the value is not already
101  * known, else does nothing.
102  *
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
108  */
109 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
110
111 /**
112  * Sets link of the element associated with specified value.
113  *
114  * @param valueset  Pointer to the value set.
115  * @param value     Value of the element.
116  * @param link      Link to be set in element.
117  */
118 void ir_valueset_set_link(const ir_valueset_t *valueset, const ir_node *value, void *link);
119
120 /**
121  * Get the link of the element associated with specified value.
122  *
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.
126  */
127 void *ir_valueset_get_link(const ir_valueset_t *valueset, const ir_node *value);
128
129 /**
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.
132  *
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
138  */
139 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
140
141 /**
142  * Get the leader expression of a specific value from the value set.
143  *
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
147  */
148 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
149
150 /**
151  * Removes a value from a value set. Does nothing if the value set doesn't contain
152  * the value.
153  *
154  * @param valueset  Pointer to the value set
155  * @param value     value to remove from the values set
156  */
157 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
158
159 /**
160  * Returns the number of values contained in the value set.
161  *
162  * @param valueset  Pointer to the value set
163  * @returns         Number of values contained in the value set
164  */
165 size_t ir_valueset_size(const ir_valueset_t *valueset);
166
167 /**
168  * Initializes a value set iterator. Sets the iterator before the first element in
169  * the value set.
170  *
171  * @param iterator   Pointer to already allocated iterator memory
172  * @param valueset   Pointer to the value set
173  */
174 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
175                                const ir_valueset_t *valueset);
176
177 /**
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.
182  *
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
186  */
187 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
188
189 /**
190  * Removes the element the iterator currently points to.
191  *
192  * @param valueset  Pointer to the value set
193  * @param iterator  Pointer to the value set iterator.
194  */
195 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
196
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))
201
202 #endif