lowering: fix i_mapper for new exception attributes
[libfirm] / ir / kaps / kaps.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   Partitioned Boolean Quadratic Problem (PBQP) solver.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "adt/array.h"
30 #include "adt/xmalloc.h"
31
32 #include "kaps.h"
33 #include "matrix.h"
34 #include "pbqp_edge.h"
35 #include "pbqp_edge_t.h"
36 #include "pbqp_node.h"
37 #include "pbqp_node_t.h"
38 #include "vector.h"
39
40 pbqp_node_t *get_node(pbqp_t *pbqp, unsigned index)
41 {
42         return pbqp->nodes[index];
43 }
44
45 pbqp_edge_t *get_edge(pbqp_t *pbqp, unsigned src_index, unsigned tgt_index)
46 {
47         size_t i;
48         size_t len;
49         pbqp_node_t *src_node;
50         pbqp_node_t *tgt_node;
51
52         if (tgt_index < src_index) {
53                 unsigned tmp = src_index;
54                 src_index    = tgt_index;
55                 tgt_index    = tmp;
56         }
57
58         src_node = get_node(pbqp, src_index);
59         tgt_node = get_node(pbqp, tgt_index);
60         assert(tgt_node);
61
62         len = ARR_LEN(src_node->edges);
63
64         for (i = 0; i < len; ++i) {
65                 pbqp_edge_t *cur_edge = src_node->edges[i];
66                 if (cur_edge->tgt == tgt_node) {
67                         return cur_edge;
68                 }
69         }
70
71         return NULL;
72 }
73
74 pbqp_t *alloc_pbqp(unsigned number_nodes)
75 {
76         pbqp_t *pbqp = XMALLOC(pbqp_t);
77
78         obstack_init(&pbqp->obstack);
79
80         pbqp->solution = 0;
81         pbqp->num_nodes = number_nodes;
82 #if KAPS_DUMP
83         pbqp->dump_file = NULL;
84 #endif
85         pbqp->nodes = OALLOCNZ(&pbqp->obstack, pbqp_node_t*, number_nodes);
86 #if KAPS_STATISTIC
87         pbqp->num_bf = 0;
88         pbqp->num_edges = 0;
89         pbqp->num_r0 = 0;
90         pbqp->num_r1 = 0;
91         pbqp->num_r2 = 0;
92         pbqp->num_rm = 0;
93         pbqp->num_rn = 0;
94 #endif
95
96         return pbqp;
97 }
98
99 void free_pbqp(pbqp_t *pbqp)
100 {
101         obstack_free(&pbqp->obstack, NULL);
102         xfree(pbqp);
103 }
104
105 void add_node_costs(pbqp_t *pbqp, unsigned node_index, vector_t *costs)
106 {
107         pbqp_node_t *node = get_node(pbqp, node_index);
108
109         if (node == NULL) {
110                 node = alloc_node(pbqp, node_index, costs);
111                 pbqp->nodes[node_index] = node;
112         } else {
113                 vector_add(node->costs, costs);
114         }
115 }
116
117 void add_edge_costs(pbqp_t *pbqp, unsigned src_index, unsigned tgt_index,
118                     pbqp_matrix_t *costs)
119 {
120         pbqp_edge_t *edge = get_edge(pbqp, src_index, tgt_index);
121
122         if (tgt_index < src_index) {
123                 pbqp_matrix_transpose(pbqp, costs);
124                 add_edge_costs(pbqp, tgt_index, src_index, costs);
125                 return;
126         }
127
128         if (edge == NULL) {
129                 edge = alloc_edge(pbqp, src_index, tgt_index, costs);
130         } else {
131                 pbqp_matrix_add(edge->costs, costs);
132         }
133 }
134
135 num get_node_solution(pbqp_t *pbqp, unsigned node_index)
136 {
137         pbqp_node_t *node = get_node(pbqp, node_index);
138
139         return node->solution;
140 }
141
142 num get_solution(pbqp_t *pbqp)
143 {
144         return pbqp->solution;
145 }
146
147 #if KAPS_DUMP
148 void set_dumpfile(pbqp *pbqp, FILE *f)
149 {
150         pbqp->dump_file = f;
151 }
152 #endif