becopyilp.c 6.95 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.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
	neighbours_iter_t iter;
85
	be_ifg_foreach_neighbour(ienv->co->cenv->ifg, &iter, ifn, curr) {
86
		/* Only consider non-removed neighbours. */
87
		if (sr_is_removed(ienv, curr))
88
89
90
91
			continue;

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

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

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

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

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

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

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

134
135
136
/**
 * Virtually reinsert the nodes removed before and color them
 */
137
static void sr_reinsert(ilp_env_t *const ienv)
138
{
Daniel Grund's avatar
Daniel Grund committed
139
	/* color the removed nodes in right order */
140
141
142
143
	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;
144
145
	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
146

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

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

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

			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
167
168
169
		}

		/* now all bits not set are possible colors */
170
		/* take one that matches the alignment constraint */
171
		unsigned free_col = 0;
172
		assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
173
		for (;;) {
174
175
176
177
178
179
			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);
		}
180
181
		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
182
183
184
185
186
187
188
189
190
191
192
193
194
	}
}

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

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

195
196
#include <stdio.h>

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

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

	return res;
}

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

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

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

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
239
240
241
242
	//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));
243

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

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

	return lpp_get_sol_state(ienv->lp);
}

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