becopyilp.c 7.02 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
10
 * @file
 * @brief       Common stuff used by all ILP formulations.
 * @author      Daniel Grund
 * @date        28.02.2006
11
 */
12
13
#include <stdbool.h>

14
#include "be_t.h"
Matthias Braun's avatar
Matthias Braun committed
15
#include "belive_t.h"
16
#include "beirg.h"
Christian Würdig's avatar
Christian Würdig committed
17
#include "irtools.h"
18
#include "irprintf.h"
Christian Würdig's avatar
Christian Würdig committed
19

Matthias Braun's avatar
Matthias Braun committed
20
#include "statev_t.h"
Matthias Braun's avatar
fix    
Matthias Braun committed
21
#include "bemodule.h"
Matthias Braun's avatar
Matthias Braun committed
22
#include "panic.h"
23

Sebastian Hack's avatar
Sebastian Hack committed
24
25
#include "lpp.h"

Matthias Braun's avatar
Matthias Braun committed
26
27
#include "lc_opts.h"
#include "lc_opts_enum.h"
28

Sebastian Hack's avatar
Sebastian Hack committed
29
30
31
#define DUMP_ILP 1

static int time_limit = 60;
Sebastian Hack's avatar
Sebastian Hack committed
32
static int solve_log  = 0;
Matthias Braun's avatar
Matthias Braun committed
33
static unsigned dump_flags = 0;
Sebastian Hack's avatar
Sebastian Hack committed
34
35

static const lc_opt_enum_mask_items_t dump_items[] = {
36
37
	{ "ilp", DUMP_ILP },
	{ NULL, 0 }
Sebastian Hack's avatar
Sebastian Hack committed
38
39
40
41
42
43
44
};

static lc_opt_enum_mask_var_t dump_var = {
	&dump_flags, dump_items
};

static const lc_opt_table_entry_t options[] = {
45
	LC_OPT_ENT_INT      ("limit", "time limit for solving in seconds (0 for unlimited)", &time_limit),
Sebastian Hack's avatar
Sebastian Hack committed
46
	LC_OPT_ENT_BOOL     ("log",   "show ilp solving log",              &solve_log),
47
	LC_OPT_ENT_ENUM_MASK("dump",  "dump flags",             &dump_var),
48
	LC_OPT_LAST
Sebastian Hack's avatar
Sebastian Hack committed
49
50
};

51
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp)
52
void be_init_copyilp(void)
Sebastian Hack's avatar
Sebastian Hack committed
53
{
54
55
56
57
58
59
	lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
	lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
	lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
	lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
	lc_opt_entry_t *ilp_grp = lc_opt_get_grp(co_grp, "ilp");

Sebastian Hack's avatar
Sebastian Hack committed
60
61
	lc_opt_add_table(ilp_grp, options);
}
62

Daniel Grund's avatar
Daniel Grund committed
63
#include "becopyilp_t.h"
64
#include "beifg.h"
Daniel Grund's avatar
Daniel Grund committed
65
66
67
68
69
70
71
72
73
74
75

/******************************************************************************
    _____ _                        _            _   _
   / ____(_)                      | |          | | (_)
  | (___  _ _______   _ __ ___  __| |_   _  ___| |_ _  ___  _ __
   \___ \| |_  / _ \ | '__/ _ \/ _` | | | |/ __| __| |/ _ \| '_ \
   ____) | |/ /  __/ | | |  __/ (_| | |_| | (__| |_| | (_) | | | |
  |_____/|_/___\___| |_|  \___|\__,_|\__,_|\___|\__|_|\___/|_| |_|

 *****************************************************************************/

Daniel Grund's avatar
Daniel Grund committed
76

Daniel Grund's avatar
Daniel Grund committed
77
78
79
/**
 * Checks if a node is simplicial in the graph heeding the already removed nodes.
 */
80
static inline bool sr_is_simplicial(ilp_env_t *const ienv, ir_node const *const ifn)
81
{
82
83
	bool              res = true;
	ir_node         **all = NEW_ARR_F(ir_node*, 0);
84
	be_lv_t    *const lv  = be_get_irg_liveness(ienv->co->irg);
85
	neighbours_iter_t iter;
86
	be_ifg_foreach_neighbour(ienv->co->cenv->ifg, &iter, ifn, curr) {
87
		/* Only consider non-removed neighbours. */
88
		if (sr_is_removed(ienv, curr))
89
90
91
92
93
94
95
96
			continue;

		/* Check whether the current node forms a clique with all previous nodes. */
		for (size_t i = ARR_LEN(all); i-- != 0;) {
			if (!be_values_interfere(lv, curr, all[i])) {
				res = false;
				goto end;
			}
97
		}
98
99

		ARR_APP1(ir_node*, all, curr);
100
	}
Daniel Grund's avatar
Daniel Grund committed
101

102
103
104
end:
	DEL_ARR_F(all);
	return res;
Daniel Grund's avatar
Daniel Grund committed
105
106
}

107
108
109
110
/**
 * Virtually remove all nodes not related to the problem
 * (simplicial AND not adjacent to a equal-color-edge)
 */
111
static void sr_remove(ilp_env_t *const ienv)
112
{
113
	bool redo = true;
114
	const be_ifg_t *ifg = ienv->co->cenv->ifg;
Daniel Grund's avatar
Daniel Grund committed
115
116

	while (redo) {
117
		redo = false;
118
		be_ifg_foreach_node(ifg, irn) {
119
			const arch_register_req_t *req = arch_get_irn_register_req(irn);
120
			if (arch_register_req_is(req, limited) || sr_is_removed(ienv, irn))
121
				continue;
122
			if (co_gs_is_optimizable(ienv->co, irn))
123
				continue;
124
			if (!sr_is_simplicial(ienv, irn))
125
				continue;
Daniel Grund's avatar
Daniel Grund committed
126

127
128
			ARR_APP1(ir_node*, ienv->col_suff, irn);
			ir_nodeset_insert(&ienv->all_removed, irn);
Daniel Grund's avatar
Daniel Grund committed
129

130
			redo = true;
Daniel Grund's avatar
Daniel Grund committed
131
132
133
134
		}
	}
}

135
136
137
/**
 * Virtually reinsert the nodes removed before and color them
 */
138
static void sr_reinsert(ilp_env_t *const ienv)
139
{
Daniel Grund's avatar
Daniel Grund committed
140
	/* color the removed nodes in right order */
141
142
143
144
	unsigned        const n_regs           = arch_register_class_n_regs(ienv->co->cls);
	unsigned       *const possible_cols    = rbitset_alloca(n_regs);
	unsigned const *const allocatable_cols = ienv->co->cenv->allocatable_regs->data;
	be_ifg_t const *const ifg              = ienv->co->cenv->ifg;
145
146
	for (size_t i = ARR_LEN(ienv->col_suff); i-- != 0;) {
		ir_node *const irn = ienv->col_suff[i];
Daniel Grund's avatar
Daniel Grund committed
147

148
		rbitset_copy(possible_cols, allocatable_cols, n_regs);
Daniel Grund's avatar
Daniel Grund committed
149

150
		/* get free color by inspecting all neighbors */
151
		neighbours_iter_t iter;
152
		be_ifg_foreach_neighbour(ifg, &iter, irn, other) {
153
154
155
			const arch_register_req_t *cur_req;
			unsigned cur_col;

156
			/* only inspect nodes which are in graph right now */
157
			if (sr_is_removed(ienv, other))
158
				continue;
159
160
161
162
163
164
165
166
167

			cur_req = arch_get_irn_register_req(other);
			cur_col = get_irn_col(other);

			/* Invalidate all single size register when it is a large one */
			do  {
				rbitset_clear(possible_cols, cur_col);
				++cur_col;
			} while ((cur_col % cur_req->width) != 0);
Daniel Grund's avatar
Daniel Grund committed
168
169
170
		}

		/* now all bits not set are possible colors */
171
		/* take one that matches the alignment constraint */
172
		unsigned free_col = 0;
173
		assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
174
		for (;;) {
175
176
177
178
179
180
			free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
			if (free_col % arch_get_irn_register_req(irn)->width == 0)
				break;
			++free_col;
			assert(free_col < n_regs);
		}
181
182
		set_irn_col(ienv->co->cls, irn, free_col);
		ir_nodeset_remove(&ienv->all_removed, irn); /* irn is back in graph again */
Daniel Grund's avatar
Daniel Grund committed
183
184
185
186
187
188
189
190
191
192
193
194
195
	}
}

/******************************************************************************
    _____                      _        _____ _      _____
   / ____|                    (_)      |_   _| |    |  __ \
  | |  __  ___ _ __   ___ _ __ _  ___    | | | |    | |__) |
  | | |_ |/ _ \ '_ \ / _ \ '__| |/ __|   | | | |    |  ___/
  | |__| |  __/ | | |  __/ |  | | (__   _| |_| |____| |
   \_____|\___|_| |_|\___|_|  |_|\___| |_____|______|_|

 *****************************************************************************/

196
197
#include <stdio.h>

198
199
ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
{
200
	ilp_env_t *res = XMALLOC(ilp_env_t);
Daniel Grund's avatar
Daniel Grund committed
201

Sebastian Hack's avatar
Sebastian Hack committed
202
203
204
205
	res->co         = co;
	res->build      = build;
	res->apply      = apply;
	res->env        = env;
206
207
	res->col_suff   = NEW_ARR_F(ir_node*, 0);
	ir_nodeset_init(&res->all_removed);
Daniel Grund's avatar
Daniel Grund committed
208
209
210
211

	return res;
}

212
213
lpp_sol_state_t ilp_go(ilp_env_t *ienv)
{
214
	ir_graph *irg = ienv->co->irg;
215

216
	sr_remove(ienv);
Daniel Grund's avatar
Daniel Grund committed
217
218

	ienv->build(ienv);
Sebastian Hack's avatar
Sebastian Hack committed
219

220
	if (dump_flags & DUMP_ILP) {
221
222
223
		char buf[128];
		FILE *f;

224
		ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
225
226
		            ienv->co->cenv->cls->name);
		f = fopen(buf, "wt");
227
		if (f == NULL) {
228
229
			panic("Couldn't open '%s' for writing", buf);
		}
Sebastian Hack's avatar
Sebastian Hack committed
230
231
232
		lpp_dump_plain(ienv->lp, f);
		fclose(f);
	}
233

234
235
236
237
	lpp_set_time_limit(ienv->lp, time_limit);
	if (solve_log)
		lpp_set_log(ienv->lp, stdout);

238
	lpp_solve(ienv->lp, be_options.ilp_server, be_options.ilp_solver);
239

Matthias Braun's avatar
Matthias Braun committed
240
241
242
243
	//stat_ev_dbl("co_ilp_objval",     ienv->lp->objval);
	//stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
	stat_ev_int("co_ilp_iter",       lpp_get_iter_cnt(ienv->lp));
	stat_ev_dbl("co_ilp_sol_time",   lpp_get_sol_time(ienv->lp));
244

Daniel Grund's avatar
Daniel Grund committed
245
246
	ienv->apply(ienv);

247
	sr_reinsert(ienv);
Daniel Grund's avatar
Daniel Grund committed
248
249
250
251

	return lpp_get_sol_state(ienv->lp);
}

252
253
void free_ilp_env(ilp_env_t *ienv)
{
254
255
	ir_nodeset_destroy(&ienv->all_removed);
	DEL_ARR_F(ienv->col_suff);
256
	lpp_free(ienv->lp);
Daniel Grund's avatar
Daniel Grund committed
257
258
	free(ienv);
}