2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
9 * @brief A value set, containing expression for values.
11 #ifndef _FIRM_VALUESET_H_
12 #define _FIRM_VALUESET_H_
14 #include "firm_types.h"
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;
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;
31 #undef ADDITIONAL_DATA
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;
42 * Initializes a value set with default size.
44 * @param valueset Pointer to allocated space for the value set
46 void ir_valueset_init(ir_valueset_t *valueset);
49 * Initializes a value set.
51 * @param valueset Pointer to allocated space for the value set
52 * @param expected_elements Number of elements expected in the value set (roughly)
54 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
57 * Destroys a value set and frees the memory allocated for hashtable. The memory of
58 * the value set itself is not freed.
60 * @param valueset Pointer to the value set
62 void ir_valueset_destroy(ir_valueset_t *valueset);
65 * Allocates memory for a value set and initializes it.
67 * @param expected_elements Number of elements expected in the value set (roughly)
68 * @return The initialized value set
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);
77 * Destroys a value set and frees the memory of the set itself.
79 static inline void ir_valueset_del(ir_valueset_t *valueset) {
80 ir_valueset_destroy(valueset);
85 * Inserts a (value, expression) pair into a valueset if the value is not already
86 * known, else does nothing.
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
94 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
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.
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
106 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
109 * Get the leader expression of a specific value from the value set.
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
115 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
118 * Removes a value from a value set. Does nothing if the value set doesn't contain
121 * @param valueset Pointer to the value set
122 * @param value value to remove from the values set
124 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
127 * Returns the number of values contained in the value set.
129 * @param valueset Pointer to the value set
130 * @returns Number of values contained in the value set
132 size_t ir_valueset_size(const ir_valueset_t *valueset);
135 * Initializes a value set iterator. Sets the iterator before the first element in
138 * @param iterator Pointer to already allocated iterator memory
139 * @param valueset Pointer to the value set
141 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
142 const ir_valueset_t *valueset);
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.
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
154 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
157 * Removes the element the iterator currently points to.
159 * @param valueset Pointer to the value set
160 * @param iterator Pointer to the value set iterator.
162 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
164 #define foreach_valueset(valueset, value, expr, iter) \
165 for (ir_valueset_iterator_init(&iter, valueset); (value = ir_valueset_iterator_next(&iter, &expr));)