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