ia32: Remove (empty) emitters from nodes, which should never be emitted.
[libfirm] / ir / kaps / vector.c
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  * @brief   PBQP vector.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "adt/array.h"
31
32 #include "vector.h"
33
34 num pbqp_add(num x, num y)
35 {
36         num res;
37
38         if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
39
40         res = x + y;
41
42 #if !KAPS_USE_UNSIGNED
43         /* No positive overflow. */
44         assert(x < 0 || y < 0 || res >= x);
45         assert(x < 0 || y < 0 || res >= y);
46 #endif
47
48         /* No negative overflow. */
49         assert(x > 0 || y > 0 || res <= x);
50         assert(x > 0 || y > 0 || res <= y);
51
52         /* Result is not infinity.*/
53         assert(res < INF_COSTS);
54
55         return res;
56 }
57
58 vector_t *vector_alloc(pbqp_t *pbqp, unsigned length)
59 {
60         vector_t *vec = (vector_t*)obstack_alloc(&pbqp->obstack, sizeof(*vec) + sizeof(*vec->entries) * length);
61         assert(length > 0);
62
63         vec->len = length;
64         memset(vec->entries, 0, sizeof(*vec->entries) * length);
65
66         return vec;
67 }
68
69 vector_t *vector_copy(pbqp_t *pbqp, vector_t *v)
70 {
71         unsigned  len  = v->len;
72         vector_t *copy = (vector_t*)obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
73         assert(copy);
74
75         return copy;
76 }
77
78 void vector_add(vector_t *sum, vector_t *summand)
79 {
80         int i;
81         int len;
82
83         assert(sum->len == summand->len);
84
85         len = sum->len;
86
87         for (i = 0; i < len; ++i) {
88                 sum->entries[i].data = pbqp_add(sum->entries[i].data,
89                                 summand->entries[i].data);
90         }
91 }
92
93 void vector_set(vector_t *vec, unsigned index, num value)
94 {
95         assert(index < vec->len);
96         vec->entries[index].data = value;
97 }
98
99 #if KAPS_ENABLE_VECTOR_NAMES
100 void vector_set_description(vector_t *vec, unsigned index, const char *name)
101 {
102         assert(index < vec->len);
103         vec->entries[index].name = name;
104 }
105 #endif
106
107 void vector_add_value(vector_t *vec, num value)
108 {
109         unsigned index;
110         unsigned len;
111
112         len = vec->len;
113
114         for (index = 0; index < len; ++index) {
115                 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
116         }
117 }
118
119 void vector_add_matrix_col(vector_t *vec, pbqp_matrix_t *mat, unsigned col_index)
120 {
121         unsigned index;
122         unsigned len;
123
124         assert(vec->len == mat->rows);
125         assert(col_index < mat->cols);
126
127         len = vec->len;
128
129         for (index = 0; index < len; ++index) {
130                 vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]);
131         }
132 }
133
134 void vector_add_matrix_row(vector_t *vec, pbqp_matrix_t *mat, unsigned row_index)
135 {
136         unsigned index;
137         unsigned len;
138
139         assert(vec->len == mat->cols);
140         assert(row_index < mat->rows);
141
142         len = vec->len;
143
144         for (index = 0; index < len; ++index) {
145                 vec->entries[index].data = pbqp_add(vec->entries[index].data,
146                                 mat->entries[row_index * mat->cols + index]);
147         }
148 }
149
150 num vector_get_min(vector_t *vec)
151 {
152         unsigned index;
153         unsigned len;
154         num      min = INF_COSTS;
155
156         len = vec->len;
157         assert(len > 0);
158
159         for (index = 0; index < len; ++index) {
160                 num elem = vec->entries[index].data;
161
162                 if (elem < min) {
163                         min = elem;
164                 }
165         }
166
167         return min;
168 }
169
170 unsigned vector_get_min_index(vector_t *vec)
171 {
172         unsigned index;
173         unsigned len;
174         unsigned min_index = 0;
175         num      min       = INF_COSTS;
176
177         len = vec->len;
178         assert(len > 0);
179
180         for (index = 0; index < len; ++index) {
181                 num elem = vec->entries[index].data;
182
183                 if (elem < min) {
184                         min = elem;
185                         min_index = index;
186                 }
187         }
188
189         return min_index;
190 }