Remove superfluous asserts
[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         int i;
48         int len;
49
50         if (tgt_index < src_index) {
51                 unsigned tmp = src_index;
52                 src_index    = tgt_index;
53                 tgt_index    = tmp;
54         }
55
56         pbqp_node_t *src_node = get_node(pbqp, src_index);
57         pbqp_node_t *tgt_node = get_node(pbqp, tgt_index);
58         assert(tgt_node);
59
60         len = ARR_LEN(src_node->edges);
61
62         for (i = 0; i < len; ++i) {
63                 pbqp_edge_t *cur_edge = src_node->edges[i];
64                 if (cur_edge->tgt == tgt_node) {
65                         return cur_edge;
66                 }
67         }
68
69         return NULL;
70 }
71
72 pbqp_t *alloc_pbqp(unsigned number_nodes)
73 {
74         pbqp_t *pbqp = XMALLOC(pbqp_t);
75
76         obstack_init(&pbqp->obstack);
77
78         pbqp->solution = 0;
79         pbqp->num_nodes = number_nodes;
80 #if     KAPS_DUMP
81         pbqp->dump_file = NULL;
82 #endif
83         pbqp->nodes = OALLOCNZ(&pbqp->obstack, pbqp_node_t*, number_nodes);
84 #if KAPS_STATISTIC
85         pbqp->num_bf = 0;
86         pbqp->num_edges = 0;
87         pbqp->num_r0 = 0;
88         pbqp->num_r1 = 0;
89         pbqp->num_r2 = 0;
90         pbqp->num_rm = 0;
91         pbqp->num_rn = 0;
92 #endif
93
94         return pbqp;
95 }
96
97 void free_pbqp(pbqp_t *pbqp)
98 {
99         obstack_free(&pbqp->obstack, NULL);
100         xfree(pbqp);
101 }
102
103 void add_node_costs(pbqp_t *pbqp, unsigned node_index, vector_t *costs)
104 {
105         pbqp_node_t *node = get_node(pbqp, node_index);
106
107         if (node == NULL) {
108                 node = alloc_node(pbqp, node_index, costs);
109                 pbqp->nodes[node_index] = node;
110         } else {
111                 vector_add(node->costs, costs);
112         }
113 }
114
115 void add_edge_costs(pbqp_t *pbqp, unsigned src_index, unsigned tgt_index,
116                     pbqp_matrix_t *costs)
117 {
118         pbqp_edge_t *edge = get_edge(pbqp, src_index, tgt_index);
119
120         if (tgt_index < src_index) {
121                 pbqp_matrix_transpose(pbqp, costs);
122                 add_edge_costs(pbqp, tgt_index, src_index, costs);
123                 return;
124         }
125
126         if (edge == NULL) {
127                 edge = alloc_edge(pbqp, src_index, tgt_index, costs);
128         } else {
129                 pbqp_matrix_add(edge->costs, costs);
130         }
131 }
132
133 num get_node_solution(pbqp_t *pbqp, unsigned node_index)
134 {
135         pbqp_node_t *node = get_node(pbqp, node_index);
136
137         return node->solution;
138 }
139
140 num get_solution(pbqp_t *pbqp)
141 {
142         return pbqp->solution;
143 }
144
145 #if     KAPS_DUMP
146 void set_dumpfile(pbqp *pbqp, FILE *f)
147 {
148         pbqp->dump_file = f;
149 }
150 #endif