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