2 * Author: Christian Wuerdig
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
8 * Performs lowering of perm nodes and spill/reload optimization.
21 #include "bechordal_t.h"
22 #include "besched_t.h"
25 #include "iredges_t.h"
29 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
32 #define is_Call(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_call)
34 /* lowering walker environment */
35 typedef struct _lower_env_t {
36 be_chordal_env_t *chord_env;
38 firm_dbg_module_t *dbg_module;
41 /* holds a perm register pair */
42 typedef struct _reg_pair_t {
43 const arch_register_t *in_reg; /**< a perm IN register */
44 ir_node *in_node; /**< the in node to which the register belongs */
46 const arch_register_t *out_reg; /**< a perm OUT register */
47 ir_node *out_node; /**< the out node to which the register belongs */
49 int checked; /**< indicates whether the pair was check for cycle or not */
52 typedef enum _perm_type_t {
59 /* structure to represent cycles or chains in a perm */
60 typedef struct _perm_cycle_t {
61 const arch_register_t **elems; /**< the registers in the cycle */
62 int n_elems; /**< number of elements in the cycle */
63 perm_type_t type; /**< type (CHAIN or CYCLE) */
66 /* Compare the in registers of two register pairs */
67 static int compare_reg_pair(const void *a, const void *b) {
68 const reg_pair_t *pair_a = a;
69 const reg_pair_t *pair_b = b;
71 if (pair_a->in_reg->index > pair_b->in_reg->index)
77 /* returns the number register pairs marked as checked */
78 static int get_n_checked_pairs(reg_pair_t *pairs, int n) {
81 for (i = 0; i < n; i++) {
90 * Gets the node corresponding to a register from an array of register pairs.
91 * NOTE: The given registers pairs and the register to look for must belong
92 * to the same register class.
94 * @param pairs The array of register pairs
95 * @param n The number of pairs
96 * @param reg The register to look for
97 * @param in_out 0 == look for IN register, 1 == look for OUT register
98 * @return The corresponding node or NULL if not found
100 static ir_node *get_node_for_register(reg_pair_t *pairs, int n, const arch_register_t *reg, int in_out) {
104 for (i = 0; i < n; i++) {
105 /* out register matches */
106 if (pairs[i].out_reg->index == reg->index)
107 return pairs[i].out_node;
111 for (i = 0; i < n; i++) {
112 /* in register matches */
113 if (pairs[i].in_reg->index == reg->index)
114 return pairs[i].in_node;
122 * Gets the index in the register pair array where the in/out register
123 * corresponds to reg_idx.
125 * @param pairs The array of register pairs
126 * @param n The number of pairs
127 * @param reg The register index to look for
128 * @param in_out 0 == look for IN register, 1 == look for OUT register
129 * @return The corresponding index in pairs or -1 if not found
131 static int get_pairidx_for_regidx(reg_pair_t *pairs, int n, int reg_idx, int in_out) {
135 for (i = 0; i < n; i++) {
136 /* out register matches */
137 if (pairs[i].out_reg->index == reg_idx)
142 for (i = 0; i < n; i++) {
143 /* in register matches */
144 if (pairs[i].in_reg->index == reg_idx)
153 * Gets an array of register pairs and tries to identify a cycle or chain starting
156 * @param cycle Variable to hold the cycle
157 * @param pairs Array of register pairs
158 * @param start Index to start
159 * @return The cycle or chain
161 static perm_cycle_t *get_perm_cycle(perm_cycle_t *cycle, reg_pair_t *pairs, int n, int start) {
162 int head = pairs[start].in_reg->index;
163 int cur_idx = pairs[start].out_reg->index;
164 int cur_pair_idx = start;
165 int n_pairs_done = get_n_checked_pairs(pairs, n);
168 /* assume worst case: all remaining pairs build a cycle or chain */
169 cycle->elems = calloc(n - n_pairs_done, sizeof(cycle->elems[0]));
170 cycle->n_elems = 2; /* initial number of elements is 2 */
171 cycle->elems[0] = pairs[start].in_reg;
172 cycle->elems[1] = pairs[start].out_reg;
173 cycle->type = PERM_CHAIN; /* default is CHAIN, only changed when we found a cycle */
175 /* mark the first pair as checked */
176 pairs[start].checked = 1;
180 /* check for cycle or end of a chain */
181 while (cur_idx != head && n_pairs_done < n) {
182 /* goto next register in cycle or chain */
183 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cur_idx, 0);
185 if (cur_pair_idx < 0)
188 cur_idx = pairs[cur_pair_idx].out_reg->index;
190 /* it's not the first element: insert it */
191 if (cur_idx != head) {
192 cycle->elems[idx++] = pairs[cur_pair_idx].out_reg;
198 /* we are there where we started -> CYCLE */
199 cycle->type = PERM_CYCLE;
202 /* mark the pair as checked */
203 pairs[cur_pair_idx].checked = 1;
210 * Lowers a perm node. Resolves cycles and creates a bunch of
211 * copy and swap operations to permute registers.
212 * Note: The caller of this function has to make sure, that irn
215 * @param irn The perm node
216 * @param block The block the perm node belongs to
217 * @param walk_env The environment
219 static void lower_perm_node(ir_node *irn, void *walk_env) {
220 const arch_register_class_t *reg_class;
221 const arch_env_t *arch_env;
222 firm_dbg_module_t *mod;
223 lower_env_t *env = walk_env;
225 const ir_edge_t *edge;
227 int n, i, pn, do_copy;
228 ir_node *sched_point, *block, *in[2];
229 ir_node *arg1, *arg2, *res1, *res2;
230 ir_node *cpyxchg = NULL;
232 arch_env = env->chord_env->main_env->arch_env;
233 do_copy = env->do_copy;
234 mod = env->dbg_module;
235 block = get_nodes_block(irn);
238 Get the schedule predecessor node to the perm
239 NOTE: This works with auto-magic. If we insert the
240 new copy/exchange nodes after this node, everything
243 sched_point = sched_prev(irn);
244 DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
245 assert(sched_point && "Perm is not scheduled or has no predecessor");
247 n = get_irn_arity(irn);
248 assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
250 reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
251 pairs = calloc(n, sizeof(pairs[0]));
253 /* build the list of register pairs (in, out) */
255 foreach_out_edge(irn, edge) {
256 pairs[i].out_node = get_edge_src_irn(edge);
257 pn = get_Proj_proj(pairs[i].out_node);
258 pairs[i].in_node = get_irn_n(irn, pn);
260 pairs[i].in_reg = arch_get_irn_register(arch_env, pairs[i].in_node);
261 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
263 pairs[i].checked = 0;
267 /* sort the register pairs by the indices of the in registers */
268 qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
270 /* Mark all equal pairs as checked, and exchange the OUT proj with
272 for (i = 0; i < n; i++) {
273 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
274 DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
275 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
277 /* We have to check for a special case:
278 The in-node could be a Proj from a Perm. In this case,
279 we need to correct the projnum */
280 if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
281 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
284 /* remove the proj from the schedule */
285 sched_remove(pairs[i].out_node);
287 /* reroute the edges from the proj to the argument */
288 edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
290 pairs[i].checked = 1;
294 /* Set do_copy to 0 if it's on but we have no free register */
299 /* check for cycles and chains */
300 while (get_n_checked_pairs(pairs, n) < n) {
303 /* go to the first not-checked pair */
304 while (pairs[i].checked) i++;
305 cycle = calloc(1, sizeof(*cycle));
306 cycle = get_perm_cycle(cycle, pairs, n, i);
308 /* We don't need to do anything if we have a Perm with two
309 elements which represents a cycle, because those nodes
310 already represent exchange nodes */
311 if (n == 2 && cycle->type == PERM_CYCLE) {
316 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
317 // the copy cascade (this reduces the cycle into a chain)
319 /* build copy/swap nodes */
320 for (i = 0; i < cycle->n_elems - 1; i++) {
321 arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
322 arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
324 res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
325 res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
328 If we have a cycle and don't copy: we need to create exchange nodes
329 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
330 IN_1 = in node with register i
331 IN_2 = in node with register i + 1
332 OUT_1 = out node with register i + 1
333 OUT_2 = out node with register i
335 if (cycle->type == PERM_CYCLE && !do_copy) {
339 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
340 irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
341 DBG((mod, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
342 irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
344 cpyxchg = be_new_Perm(reg_class, env->chord_env->irg, block, 2, in);
349 set_Proj_pred(res2, cpyxchg);
350 set_Proj_proj(res2, 0);
351 set_Proj_pred(res1, cpyxchg);
352 set_Proj_proj(res1, 1);
354 sched_add_after(sched_point, res1);
355 sched_add_after(sched_point, res2);
357 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
358 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
361 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
362 irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
364 cpyxchg = be_new_Copy(reg_class, env->chord_env->irg, block, arg1);
365 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
367 /* remove the proj from the schedule */
370 /* exchange copy node and proj */
371 exchange(res2, cpyxchg);
374 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
375 sched_add_after(sched_point, cpyxchg);
377 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
380 // free(cycle->elems);
384 /* remove the perm from schedule */
391 * Adds Projs to keep nodes for each register class, which eats the
392 * caller saved registers.
393 * Note: The caller has to make sure, that call is a Call
395 * @param call The Call node
396 * @param walk_env The walker environment
398 static void lower_call_node(ir_node *call, void *walk_env) {
399 lower_env_t *env = walk_env;
400 const arch_env_t *arch_env = env->chord_env->main_env->arch_env;
401 firm_dbg_module_t *mod = env->dbg_module;
402 const arch_register_class_t *reg_class;
403 int i, j, set_size = 0, pn, keep_arity;
404 arch_isa_t *isa = arch_env_get_isa(arch_env);
405 const ir_node *proj_T = NULL;
406 ir_node **in_keep, *block = get_nodes_block(call);
407 ir_node *last_proj = NULL;
409 const ir_edge_t *edge;
410 const arch_register_t *reg;
412 /* Prepare the bitset where we store the projnums which are already in use*/
413 for (i = 0; i < arch_isa_get_n_reg_class(isa); i++) {
414 reg_class = arch_isa_get_reg_class(isa, i);
415 set_size += arch_register_class_n_regs(reg_class);
418 in_keep = malloc(set_size * sizeof(ir_node *));
420 proj_set = bitset_malloc(set_size);
421 bitset_clear_all(proj_set);
423 /* check if there is a ProjT node and which arguments are used */
424 foreach_out_edge(call, edge) {
425 if (get_irn_mode(get_edge_src_irn(edge)) == mode_T)
426 proj_T = get_edge_src_irn(edge);
429 /* set all used arguments */
433 foreach_out_edge(proj_T, edge) {
434 ir_node *proj = get_edge_src_irn(edge);
436 assert(is_Proj(proj));
437 bitset_set(proj_set, get_Proj_proj(get_edge_src_irn(edge)));
440 * Filter out the last proj in the schedule.
441 * After that one, we have to insert the Keep node.
443 if(!last_proj || sched_comes_after(last_proj, proj))
449 proj_T = new_r_Proj(current_ir_graph, block, call, mode_T, pn_Call_T_result);
452 /* Create for each caller save register a proj (keep node arguement) */
453 /* if this proj is not already present */
454 for (i = 0; i < arch_isa_get_n_reg_class(isa); i++) {
456 /* reset the keep input, as we need one keep for each register class */
457 memset(in_keep, 0, set_size * sizeof(ir_node *));
459 reg_class = arch_isa_get_reg_class(isa, i);
461 for (j = 0; j < arch_register_class_n_regs(reg_class); j++) {
462 reg = arch_register_for_index(reg_class, j);
464 /* only check caller save registers */
465 if (arch_register_type_is(reg, caller_saved)) {
466 pn = isa->impl->get_projnum_for_register(isa, reg);
467 if (!bitset_is_set(proj_set, pn)) {
468 ir_node *proj = new_r_Proj(current_ir_graph, block, (ir_node *)proj_T, mode_Is, pn);
470 in_keep[keep_arity++] = proj;
471 sched_add_after(last_proj, proj);
477 /* ok, we found some caller save register which are not in use but must be saved */
481 keep = be_new_Keep(reg_class, current_ir_graph, block, keep_arity, in_keep);
482 sched_add_after(last_proj, keep);
486 bitset_free(proj_set);
493 * Calls the backend code generator functions to lower Spill and
494 * Reload nodes into Store and Load. The backend is fully responsible
495 * for creating the new nodes and setting their input correct.
496 * Note: The caller of this has to make sure that irn is a Spill
499 * @param irn The Spill/Reload node
500 * @param walk_env The walker environment
502 static void lower_spill_reload(ir_node *irn, void *walk_env) {
503 lower_env_t *env = walk_env;
504 arch_code_generator_t *cg = env->chord_env->main_env->cg;
505 const arch_env_t *aenv = env->chord_env->main_env->arch_env;
507 ir_node *sched_point;
509 if (be_is_Spill(irn) && cg->impl->lower_spill) {
510 res = cg->impl->lower_spill(cg, irn);
512 else if (be_is_Reload(irn) && cg->impl->lower_reload) {
513 res = cg->impl->lower_reload(cg, irn);
514 if (res && res != irn) {
515 /* copy the result register from the reload to the load */
516 arch_set_irn_register(aenv, res, arch_get_irn_register(aenv, irn));
520 if (res && res != irn) {
521 sched_point = sched_prev(irn);
524 sched_add_after(sched_point, res);
527 DBG((env->dbg_module, LEVEL_1, "node %+F not lowered\n", irn));
535 * Calls the corresponding lowering function for the node.
537 * @param irn The node to be checked for lowering
538 * @param walk_env The walker environment
540 static void lower_nodes_before_ra_walker(ir_node *irn, void *walk_env) {
541 lower_env_t *env = walk_env;
542 const arch_env_t *arch_env = env->chord_env->main_env->arch_env;
544 if (!is_Block(irn) && !is_Proj(irn)) {
545 if (is_Call(arch_env, irn)) {
546 lower_call_node(irn, walk_env);
555 * Calls the corresponding lowering function for the node.
557 * @param irn The node to be checked for lowering
558 * @param walk_env The walker environment
560 static void lower_nodes_after_ra_walker(ir_node *irn, void *walk_env) {
561 lower_env_t *env = walk_env;
562 const arch_env_t *arch_env = env->chord_env->main_env->arch_env;
564 if (!is_Block(irn) && !is_Proj(irn)) {
565 if (is_Perm(arch_env, irn)) {
566 lower_perm_node(irn, walk_env);
568 else if (be_is_Spill(irn) || be_is_Reload(irn)) {
569 lower_spill_reload(irn, walk_env);
577 * Walks over all blocks in an irg and performs lowering need
578 * to be done before register allocation (e.g. call lowering).
580 * @param chord_env The chordal environment containing the irg
581 * @param do_copy 1 == resolve cycles with a free reg if available
583 void lower_nodes_before_ra(be_chordal_env_t *chord_env, int do_copy) {
586 env.chord_env = chord_env;
587 env.do_copy = do_copy;
588 env.dbg_module = firm_dbg_register("ir.be.lower");
590 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_before_ra_walker, &env);
594 * Walks over all blocks in an irg and performs lowering need to be
595 * done after register allocation (e.g. perm and spill/reload lowering).
597 * @param chord_env The chordal environment containing the irg
598 * @param do_copy 1 == resolve cycles with a free reg if available
600 void lower_nodes_after_ra(be_chordal_env_t *chord_env, int do_copy) {
603 env.chord_env = chord_env;
604 env.do_copy = do_copy;
605 env.dbg_module = firm_dbg_register("ir.be.lower");
607 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_after_ra_walker, &env);