ia32: Remove the unnecessary special case to get the latency of a CopyB.
[libfirm] / ir / be / becopyilp.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Common stuff used by all ILP formulations.
9  * @author      Daniel Grund
10  * @date        28.02.2006
11  */
12 #include "config.h"
13
14 #include <stdbool.h>
15
16 #include "be_t.h"
17 #include "beintlive_t.h"
18 #include "beirg.h"
19 #include "irtools.h"
20 #include "irprintf.h"
21
22 #include "statev_t.h"
23 #include "bemodule.h"
24 #include "error.h"
25
26 #include "lpp.h"
27
28 #include "lc_opts.h"
29 #include "lc_opts_enum.h"
30
31 #define DUMP_ILP 1
32
33 static int time_limit = 60;
34 static int solve_log  = 0;
35 static unsigned dump_flags = 0;
36
37 static const lc_opt_enum_mask_items_t dump_items[] = {
38         { "ilp", DUMP_ILP },
39         { NULL, 0 }
40 };
41
42 static lc_opt_enum_mask_var_t dump_var = {
43         &dump_flags, dump_items
44 };
45
46 static const lc_opt_table_entry_t options[] = {
47         LC_OPT_ENT_INT      ("limit", "time limit for solving in seconds (0 for unlimited)", &time_limit),
48         LC_OPT_ENT_BOOL     ("log",   "show ilp solving log",              &solve_log),
49         LC_OPT_ENT_ENUM_MASK("dump",  "dump flags",             &dump_var),
50         LC_OPT_LAST
51 };
52
53 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp)
54 void be_init_copyilp(void)
55 {
56         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
57         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
58         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
59         lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
60         lc_opt_entry_t *ilp_grp = lc_opt_get_grp(co_grp, "ilp");
61
62         lc_opt_add_table(ilp_grp, options);
63 }
64
65 #include "becopyilp_t.h"
66 #include "beifg.h"
67
68 /******************************************************************************
69     _____ _                        _            _   _
70    / ____(_)                      | |          | | (_)
71   | (___  _ _______   _ __ ___  __| |_   _  ___| |_ _  ___  _ __
72    \___ \| |_  / _ \ | '__/ _ \/ _` | | | |/ __| __| |/ _ \| '_ \
73    ____) | |/ /  __/ | | |  __/ (_| | |_| | (__| |_| | (_) | | | |
74   |_____/|_/___\___| |_|  \___|\__,_|\__,_|\___|\__|_|\___/|_| |_|
75
76  *****************************************************************************/
77
78
79 /**
80  * Checks if a node is simplicial in the graph heeding the already removed nodes.
81  */
82 static inline bool sr_is_simplicial(ilp_env_t *const ienv, ir_node const *const ifn)
83 {
84         bool              res = true;
85         ir_node         **all = NEW_ARR_F(ir_node*, 0);
86         be_lv_t    *const lv  = be_get_irg_liveness(ienv->co->irg);
87         neighbours_iter_t iter;
88         be_ifg_foreach_neighbour(ienv->co->cenv->ifg, &iter, ifn, curr) {
89                 /* Only consider non-removed neighbours. */
90                 if (sr_is_removed(ienv, curr))
91                         continue;
92
93                 /* Check whether the current node forms a clique with all previous nodes. */
94                 for (size_t i = ARR_LEN(all); i-- != 0;) {
95                         if (!be_values_interfere(lv, curr, all[i])) {
96                                 res = false;
97                                 goto end;
98                         }
99                 }
100
101                 ARR_APP1(ir_node*, all, curr);
102         }
103
104 end:
105         DEL_ARR_F(all);
106         return res;
107 }
108
109 /**
110  * Virtually remove all nodes not related to the problem
111  * (simplicial AND not adjacent to a equal-color-edge)
112  */
113 static void sr_remove(ilp_env_t *const ienv)
114 {
115         bool redo = true;
116         const be_ifg_t *ifg = ienv->co->cenv->ifg;
117
118         while (redo) {
119                 redo = false;
120                 be_ifg_foreach_node(ifg, irn) {
121                         const arch_register_req_t *req = arch_get_irn_register_req(irn);
122                         if (arch_register_req_is(req, limited) || sr_is_removed(ienv, irn))
123                                 continue;
124                         if (co_gs_is_optimizable(ienv->co, irn))
125                                 continue;
126                         if (!sr_is_simplicial(ienv, irn))
127                                 continue;
128
129                         ARR_APP1(ir_node*, ienv->col_suff, irn);
130                         ir_nodeset_insert(&ienv->all_removed, irn);
131
132                         redo = true;
133                 }
134         }
135 }
136
137 /**
138  * Virtually reinsert the nodes removed before and color them
139  */
140 static void sr_reinsert(ilp_env_t *const ienv)
141 {
142         ir_graph *irg        = ienv->co->irg;
143         be_ifg_t *ifg        = ienv->co->cenv->ifg;
144         unsigned  n_regs     = arch_register_class_n_regs(ienv->co->cls);
145
146         unsigned *const allocatable_cols = rbitset_alloca(n_regs);
147         be_set_allocatable_regs(irg, ienv->co->cls, allocatable_cols);
148
149         unsigned *const possible_cols = rbitset_alloca(n_regs);
150         neighbours_iter_t iter;
151
152         /* color the removed nodes in right order */
153         for (size_t i = ARR_LEN(ienv->col_suff); i-- != 0;) {
154                 ir_node *const irn = ienv->col_suff[i];
155
156                 rbitset_copy(possible_cols, allocatable_cols, n_regs);
157
158                 /* get free color by inspecting all neighbors */
159                 be_ifg_foreach_neighbour(ifg, &iter, irn, other) {
160                         const arch_register_req_t *cur_req;
161                         unsigned cur_col;
162
163                         /* only inspect nodes which are in graph right now */
164                         if (sr_is_removed(ienv, other))
165                                 continue;
166
167                         cur_req = arch_get_irn_register_req(other);
168                         cur_col = get_irn_col(other);
169
170                         /* Invalidate all single size register when it is a large one */
171                         do  {
172                                 rbitset_clear(possible_cols, cur_col);
173                                 ++cur_col;
174                         } while ((cur_col % cur_req->width) != 0);
175                 }
176
177                 /* now all bits not set are possible colors */
178                 /* take one that matches the alignment constraint */
179                 unsigned free_col;
180                 assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
181                 for (;;) {
182                         free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
183                         if (free_col % arch_get_irn_register_req(irn)->width == 0)
184                                 break;
185                         ++free_col;
186                         assert(free_col < n_regs);
187                 }
188                 set_irn_col(ienv->co->cls, irn, free_col);
189                 ir_nodeset_remove(&ienv->all_removed, irn); /* irn is back in graph again */
190         }
191 }
192
193 /******************************************************************************
194     _____                      _        _____ _      _____
195    / ____|                    (_)      |_   _| |    |  __ \
196   | |  __  ___ _ __   ___ _ __ _  ___    | | | |    | |__) |
197   | | |_ |/ _ \ '_ \ / _ \ '__| |/ __|   | | | |    |  ___/
198   | |__| |  __/ | | |  __/ |  | | (__   _| |_| |____| |
199    \_____|\___|_| |_|\___|_|  |_|\___| |_____|______|_|
200
201  *****************************************************************************/
202
203 #include <stdio.h>
204
205 ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
206 {
207         ilp_env_t *res = XMALLOC(ilp_env_t);
208
209         res->co         = co;
210         res->build      = build;
211         res->apply      = apply;
212         res->env        = env;
213         res->col_suff   = NEW_ARR_F(ir_node*, 0);
214         ir_nodeset_init(&res->all_removed);
215
216         return res;
217 }
218
219 lpp_sol_state_t ilp_go(ilp_env_t *ienv)
220 {
221         ir_graph *irg = ienv->co->irg;
222
223         sr_remove(ienv);
224
225         ienv->build(ienv);
226
227         if (dump_flags & DUMP_ILP) {
228                 char buf[128];
229                 FILE *f;
230
231                 ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
232                             ienv->co->cenv->cls->name);
233                 f = fopen(buf, "wt");
234                 if (f == NULL) {
235                         panic("Couldn't open '%s' for writing", buf);
236                 }
237                 lpp_dump_plain(ienv->lp, f);
238                 fclose(f);
239         }
240
241         lpp_set_time_limit(ienv->lp, time_limit);
242         if (solve_log)
243                 lpp_set_log(ienv->lp, stdout);
244
245         lpp_solve(ienv->lp, be_options.ilp_server, be_options.ilp_solver);
246
247         //stat_ev_dbl("co_ilp_objval",     ienv->lp->objval);
248         //stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
249         stat_ev_int("co_ilp_iter",       lpp_get_iter_cnt(ienv->lp));
250         stat_ev_dbl("co_ilp_sol_time",   lpp_get_sol_time(ienv->lp));
251
252         ienv->apply(ienv);
253
254         sr_reinsert(ienv);
255
256         return lpp_get_sol_state(ienv->lp);
257 }
258
259 void free_ilp_env(ilp_env_t *ienv)
260 {
261         ir_nodeset_destroy(&ienv->all_removed);
262         DEL_ARR_F(ienv->col_suff);
263         lpp_free(ienv->lp);
264         free(ienv);
265 }