bearch: Disallow passing Projs to get_irn_ops().
[libfirm] / ir / ir / valueset.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @author    Michael Beck
9  * @brief     A value set, containing expression for values.
10  */
11 #ifndef _FIRM_VALUESET_H_
12 #define _FIRM_VALUESET_H_
13
14 #include "firm_types.h"
15 #include "xmalloc.h"
16 #include "list.h"
17
18 typedef struct ir_valueset_entry_t {
19         ir_node     *value;  /**< the represented value */
20         ir_node     *expr;   /**< the leader expression for the value in the current set */
21         list_head   list;    /**< link field for the list iterator */
22 } ir_valueset_entry_t;
23
24 #define HashSet          ir_valueset_t
25 #define ValueType        ir_valueset_entry_t
26 #define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
27 #undef DO_REHASH
28
29 #include "hashset.h"
30
31 #undef ADDITIONAL_DATA
32 #undef ValueType
33 #undef HashSet
34
35 typedef struct ir_valueset_t ir_valueset_t;
36 typedef struct ir_valueset_iterator_t {
37         list_head           *iter;       /**< points to the list head of the last element */
38         const ir_valueset_t *valueset;   /**< the value set of this iterator. */
39 } ir_valueset_iterator_t;
40
41 /**
42  * Initializes a value set with default size.
43  *
44  * @param valueset      Pointer to allocated space for the value set
45  */
46 void ir_valueset_init(ir_valueset_t *valueset);
47
48 /**
49  * Initializes a value set.
50  *
51  * @param valueset            Pointer to allocated space for the value set
52  * @param expected_elements   Number of elements expected in the value set (roughly)
53  */
54 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
55
56 /**
57  * Destroys a value set and frees the memory allocated for hashtable. The memory of
58  * the value set itself is not freed.
59  *
60  * @param valueset   Pointer to the value set
61  */
62 void ir_valueset_destroy(ir_valueset_t *valueset);
63
64 /**
65  * Allocates memory for a value set and initializes it.
66  *
67  * @param expected_elements   Number of elements expected in the value set (roughly)
68  * @return The initialized value set
69  */
70 static inline ir_valueset_t *ir_valueset_new(size_t expected_elements) {
71         ir_valueset_t *res = XMALLOC(ir_valueset_t);
72         ir_valueset_init_size(res, expected_elements);
73         return res;
74 }
75
76 /**
77  * Destroys a value set and frees the memory of the set itself.
78  */
79 static inline void ir_valueset_del(ir_valueset_t *valueset) {
80         ir_valueset_destroy(valueset);
81         xfree(valueset);
82 }
83
84 /**
85  * Inserts a (value, expression) pair into a valueset if the value is not already
86  * known, else does nothing.
87  *
88  * @param valueset  Pointer to the value set
89  * @param value     the value to insert into the value set
90  * @param expr      the expression to associate with the value
91  * @returns         1 if the value has been inserted,
92  *                  0 if it was already there
93  */
94 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
95
96 /**
97  * Inserts a (value, expression) pair into a valueset if the value is not already
98  * known, else replace the expression for the given value.
99  *
100  * @param valueset  Pointer to the value set
101  * @param value     the value to insert into the value set
102  * @param expr      the expression to associate with the value
103  * @returns         1 if the value has been inserted,
104  *                  0 if it was already there
105  */
106 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
107
108 /**
109  * Get the leader expression of a specific value from the value set.
110  *
111  * @param valueset  Pointer to the value set
112  * @param value     The value to find
113  * @returns         the associated expression of the value or NULL
114  */
115 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
116
117 /**
118  * Removes a value from a value set. Does nothing if the value set doesn't contain
119  * the value.
120  *
121  * @param valueset  Pointer to the value set
122  * @param value     value to remove from the values set
123  */
124 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
125
126 /**
127  * Returns the number of values contained in the value set.
128  *
129  * @param valueset  Pointer to the value set
130  * @returns         Number of values contained in the value set
131  */
132 size_t ir_valueset_size(const ir_valueset_t *valueset);
133
134 /**
135  * Initializes a value set iterator. Sets the iterator before the first element in
136  * the value set.
137  *
138  * @param iterator   Pointer to already allocated iterator memory
139  * @param valueset   Pointer to the value set
140  */
141 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
142                                const ir_valueset_t *valueset);
143
144 /**
145  * Advances the iterator and returns the current element or NULL if all elements
146  * in the value set have been processed.
147  * @note It is not allowed to use ir_valueset_insert() or ir_valueset_remove() while
148  *            iterating over a nodemap.
149  *
150  * @param iterator  Pointer to the value set iterator.
151  * @param expr      After return contains the associated expression for the value or NULL
152  * @returns         Next element in the value set or NULL
153  */
154 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
155
156 /**
157  * Removes the element the iterator currently points to.
158  *
159  * @param valueset  Pointer to the value set
160  * @param iterator  Pointer to the value set iterator.
161  */
162 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
163
164 #define foreach_valueset(valueset, value, expr, iter) \
165         for (ir_valueset_iterator_init(&iter, valueset); (value = ir_valueset_iterator_next(&iter, &expr));)
166
167 #endif