summaryrefslogtreecommitdiffhomepage
path: root/ir/ir/valueset.h
blob: e82976b5c444b3fc27ebad6694a7117e1e28a7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @author    Michael Beck
 * @brief     A value set, containing expression for values.
 */
#ifndef _FIRM_VALUESET_H_
#define _FIRM_VALUESET_H_

#include "firm_types.h"
#include "xmalloc.h"
#include "list.h"

typedef struct ir_valueset_entry_t {
	ir_node     *value;  /**< the represented value */
	ir_node     *expr;   /**< the leader expression for the value in the current set */
	list_head   list;    /**< link field for the list iterator */
} ir_valueset_entry_t;

#define HashSet          ir_valueset_t
#define ValueType        ir_valueset_entry_t
#define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
#undef DO_REHASH

#include "hashset.h"

#undef ADDITIONAL_DATA
#undef ValueType
#undef HashSet

typedef struct ir_valueset_t ir_valueset_t;
typedef struct ir_valueset_iterator_t {
	list_head           *iter;       /**< points to the list head of the last element */
	const ir_valueset_t *valueset;   /**< the value set of this iterator. */
} ir_valueset_iterator_t;

/**
 * Initializes a value set with default size.
 *
 * @param valueset      Pointer to allocated space for the value set
 */
void ir_valueset_init(ir_valueset_t *valueset);

/**
 * Initializes a value set.
 *
 * @param valueset            Pointer to allocated space for the value set
 * @param expected_elements   Number of elements expected in the value set (roughly)
 */
void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);

/**
 * Destroys a value set and frees the memory allocated for hashtable. The memory of
 * the value set itself is not freed.
 *
 * @param valueset   Pointer to the value set
 */
void ir_valueset_destroy(ir_valueset_t *valueset);

/**
 * Allocates memory for a value set and initializes it.
 *
 * @param expected_elements   Number of elements expected in the value set (roughly)
 * @return The initialized value set
 */
static inline ir_valueset_t *ir_valueset_new(size_t expected_elements) {
	ir_valueset_t *res = XMALLOC(ir_valueset_t);
	ir_valueset_init_size(res, expected_elements);
	return res;
}

/**
 * Destroys a value set and frees the memory of the set itself.
 */
static inline void ir_valueset_del(ir_valueset_t *valueset) {
	ir_valueset_destroy(valueset);
	free(valueset);
}

/**
 * Inserts a (value, expression) pair into a valueset if the value is not already
 * known, else does nothing.
 *
 * @param valueset  Pointer to the value set
 * @param value     the value to insert into the value set
 * @param expr      the expression to associate with the value
 * @returns         1 if the value has been inserted,
 *                  0 if it was already there
 */
int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);

/**
 * Inserts a (value, expression) pair into a valueset if the value is not already
 * known, else replace the expression for the given value.
 *
 * @param valueset  Pointer to the value set
 * @param value     the value to insert into the value set
 * @param expr      the expression to associate with the value
 * @returns         1 if the value has been inserted,
 *                  0 if it was already there
 */
int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);

/**
 * Get the leader expression of a specific value from the value set.
 *
 * @param valueset  Pointer to the value set
 * @param value     The value to find
 * @returns         the associated expression of the value or NULL
 */
void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);

/**
 * Removes a value from a value set. Does nothing if the value set doesn't contain
 * the value.
 *
 * @param valueset  Pointer to the value set
 * @param value     value to remove from the values set
 */
void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);

/**
 * Returns the number of values contained in the value set.
 *
 * @param valueset  Pointer to the value set
 * @returns         Number of values contained in the value set
 */
size_t ir_valueset_size(const ir_valueset_t *valueset);

/**
 * Initializes a value set iterator. Sets the iterator before the first element in
 * the value set.
 *
 * @param iterator   Pointer to already allocated iterator memory
 * @param valueset   Pointer to the value set
 */
void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
                               const ir_valueset_t *valueset);

/**
 * Advances the iterator and returns the current element or NULL if all elements
 * in the value set have been processed.
 * @note It is not allowed to use ir_valueset_insert() or ir_valueset_remove() while
 *            iterating over a nodemap.
 *
 * @param iterator  Pointer to the value set iterator.
 * @param expr      After return contains the associated expression for the value or NULL
 * @returns         Next element in the value set or NULL
 */
ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);

/**
 * Removes the element the iterator currently points to.
 *
 * @param valueset  Pointer to the value set
 * @param iterator  Pointer to the value set iterator.
 */
void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);

#define foreach_valueset(valueset, value, expr, iter) \
	for (ir_valueset_iterator_init(&iter, valueset); (value = ir_valueset_iterator_next(&iter, &expr));)

#endif