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.
22 #include "bechordal_t.h"
23 #include "besched_t.h"
26 #include "iredges_t.h"
36 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
39 #define is_Call(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_call)
41 /* lowering walker environment */
42 typedef struct _lower_env_t {
43 be_chordal_env_t *chord_env;
45 firm_dbg_module_t *dbg_module;
48 /* holds a perm register pair */
49 typedef struct _reg_pair_t {
50 const arch_register_t *in_reg; /**< a perm IN register */
51 ir_node *in_node; /**< the in node to which the register belongs */
53 const arch_register_t *out_reg; /**< a perm OUT register */
54 ir_node *out_node; /**< the out node to which the register belongs */
56 int checked; /**< indicates whether the pair was check for cycle or not */
59 typedef enum _perm_type_t {
66 /* structure to represent cycles or chains in a perm */
67 typedef struct _perm_cycle_t {
68 const arch_register_t **elems; /**< the registers in the cycle */
69 int n_elems; /**< number of elements in the cycle */
70 perm_type_t type; /**< type (CHAIN or CYCLE) */
73 /* Compare the in registers of two register pairs */
74 static int compare_reg_pair(const void *a, const void *b) {
75 const reg_pair_t *pair_a = a;
76 const reg_pair_t *pair_b = b;
78 if (pair_a->in_reg->index > pair_b->in_reg->index)
84 /* returns the number register pairs marked as checked */
85 static int get_n_checked_pairs(reg_pair_t *pairs, int n) {
88 for (i = 0; i < n; i++) {
97 * Gets the node corresponding to a register from an array of register pairs.
98 * NOTE: The given registers pairs and the register to look for must belong
99 * to the same register class.
101 * @param pairs The array of register pairs
102 * @param n The number of pairs
103 * @param reg The register to look for
104 * @param in_out 0 == look for IN register, 1 == look for OUT register
105 * @return The corresponding node or NULL if not found
107 static ir_node *get_node_for_register(reg_pair_t *pairs, int n, const arch_register_t *reg, int in_out) {
111 for (i = 0; i < n; i++) {
112 /* out register matches */
113 if (pairs[i].out_reg->index == reg->index)
114 return pairs[i].out_node;
118 for (i = 0; i < n; i++) {
119 /* in register matches */
120 if (pairs[i].in_reg->index == reg->index)
121 return pairs[i].in_node;
129 * Gets the index in the register pair array where the in/out register
130 * corresponds to reg_idx.
132 * @param pairs The array of register pairs
133 * @param n The number of pairs
134 * @param reg The register index to look for
135 * @param in_out 0 == look for IN register, 1 == look for OUT register
136 * @return The corresponding index in pairs or -1 if not found
138 static int get_pairidx_for_regidx(reg_pair_t *pairs, int n, int reg_idx, int in_out) {
142 for (i = 0; i < n; i++) {
143 /* out register matches */
144 if (pairs[i].out_reg->index == reg_idx)
149 for (i = 0; i < n; i++) {
150 /* in register matches */
151 if (pairs[i].in_reg->index == reg_idx)
160 * Gets an array of register pairs and tries to identify a cycle or chain starting
163 * @param cycle Variable to hold the cycle
164 * @param pairs Array of register pairs
165 * @param start Index to start
166 * @return The cycle or chain
168 static perm_cycle_t *get_perm_cycle(perm_cycle_t *cycle, reg_pair_t *pairs, int n, int start) {
169 int head = pairs[start].in_reg->index;
170 int cur_idx = pairs[start].out_reg->index;
171 int cur_pair_idx = start;
172 int n_pairs_done = get_n_checked_pairs(pairs, n);
174 perm_type_t cycle_tp = PERM_CYCLE;
176 /* We could be right in the middle of a chain, so we need to find the start */
177 while (head != cur_idx) {
178 /* goto previous register in cycle or chain */
179 cur_pair_idx = get_pairidx_for_regidx(pairs, n, head, 1);
181 if (cur_pair_idx < 0) {
182 cycle_tp = PERM_CHAIN;
186 head = pairs[cur_pair_idx].in_reg->index;
187 start = cur_pair_idx;
191 /* assume worst case: all remaining pairs build a cycle or chain */
192 cycle->elems = calloc((n - n_pairs_done) * 2, sizeof(cycle->elems[0]));
193 cycle->n_elems = 2; /* initial number of elements is 2 */
194 cycle->elems[0] = pairs[start].in_reg;
195 cycle->elems[1] = pairs[start].out_reg;
196 cycle->type = cycle_tp;
197 cur_idx = pairs[start].out_reg->index;
200 /* check for cycle or end of a chain */
201 while (cur_idx != head) {
202 /* goto next register in cycle or chain */
203 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cur_idx, 0);
205 if (cur_pair_idx < 0)
208 cur_idx = pairs[cur_pair_idx].out_reg->index;
210 /* it's not the first element: insert it */
211 if (cur_idx != head) {
212 cycle->elems[idx++] = pairs[cur_pair_idx].out_reg;
216 /* we are there where we started -> CYCLE */
217 cycle->type = PERM_CYCLE;
221 /* mark all pairs having one in/out register with cycle in common as checked */
222 for (idx = 0; idx < cycle->n_elems; idx++) {
223 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 0);
225 if (cur_pair_idx >= 0)
226 pairs[cur_pair_idx].checked = 1;
228 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 1);
230 if (cur_pair_idx >= 0)
231 pairs[cur_pair_idx].checked = 1;
238 * Lowers a perm node. Resolves cycles and creates a bunch of
239 * copy and swap operations to permute registers.
240 * Note: The caller of this function has to make sure, that irn
243 * @param irn The perm node
244 * @param block The block the perm node belongs to
245 * @param walk_env The environment
247 static void lower_perm_node(ir_node *irn, void *walk_env) {
248 const arch_register_class_t *reg_class;
249 const arch_env_t *arch_env;
250 firm_dbg_module_t *mod;
251 lower_env_t *env = walk_env;
253 const ir_edge_t *edge;
255 int n, i, pn, do_copy, j;
256 ir_node *sched_point, *block, *in[2];
257 ir_node *arg1, *arg2, *res1, *res2;
258 ir_node *cpyxchg = NULL;
260 arch_env = env->chord_env->main_env->arch_env;
261 do_copy = env->do_copy;
262 mod = env->dbg_module;
263 block = get_nodes_block(irn);
266 Get the schedule predecessor node to the perm
267 NOTE: This works with auto-magic. If we insert the
268 new copy/exchange nodes after this node, everything
271 sched_point = sched_prev(irn);
272 DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
273 assert(sched_point && "Perm is not scheduled or has no predecessor");
275 n = get_irn_arity(irn);
276 assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
278 reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
279 pairs = alloca(n * sizeof(pairs[0]));
281 /* build the list of register pairs (in, out) */
283 foreach_out_edge(irn, edge) {
284 pairs[i].out_node = get_edge_src_irn(edge);
285 pn = get_Proj_proj(pairs[i].out_node);
286 pairs[i].in_node = get_irn_n(irn, pn);
288 pairs[i].in_reg = arch_get_irn_register(arch_env, pairs[i].in_node);
289 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
291 pairs[i].checked = 0;
295 /* sort the register pairs by the indices of the in registers */
296 qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
298 /* Mark all equal pairs as checked, and exchange the OUT proj with
300 for (i = 0; i < n; i++) {
301 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
302 DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
303 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
305 /* We have to check for a special case:
306 The in-node could be a Proj from a Perm. In this case,
307 we need to correct the projnum */
308 if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
309 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
312 /* remove the proj from the schedule */
313 sched_remove(pairs[i].out_node);
315 /* reroute the edges from the proj to the argument */
316 edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
318 pairs[i].checked = 1;
322 /* Set do_copy to 0 if it's on but we have no free register */
327 /* check for cycles and chains */
328 while (get_n_checked_pairs(pairs, n) < n) {
331 /* go to the first not-checked pair */
332 while (pairs[i].checked) i++;
333 cycle = calloc(1, sizeof(*cycle));
334 cycle = get_perm_cycle(cycle, pairs, n, i);
336 DB((mod, LEVEL_1, "%+F: following %s created:\n ", irn, cycle->type == PERM_CHAIN ? "chain" : "cycle"));
337 for (j = 0; j < cycle->n_elems; j++) {
338 DB((mod, LEVEL_1, " %s", cycle->elems[j]->name));
340 DB((mod, LEVEL_1, "\n"));
342 /* We don't need to do anything if we have a Perm with two
343 elements which represents a cycle, because those nodes
344 already represent exchange nodes */
345 if (n == 2 && cycle->type == PERM_CYCLE) {
350 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
351 // the copy cascade (this reduces the cycle into a chain)
353 /* build copy/swap nodes */
354 for (i = 0; i < cycle->n_elems - 1; i++) {
355 arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
356 arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
358 res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
359 res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
362 If we have a cycle and don't copy: we need to create exchange nodes
363 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
364 IN_1 = in node with register i
365 IN_2 = in node with register i + 1
366 OUT_1 = out node with register i + 1
367 OUT_2 = out node with register i
369 if (cycle->type == PERM_CYCLE && !do_copy) {
373 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
374 irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
375 DBG((mod, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
376 irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
378 cpyxchg = be_new_Perm(reg_class, env->chord_env->irg, block, 2, in);
383 set_Proj_pred(res2, cpyxchg);
384 set_Proj_proj(res2, 0);
385 set_Proj_pred(res1, cpyxchg);
386 set_Proj_proj(res1, 1);
388 sched_add_after(sched_point, res1);
389 sched_add_after(sched_point, res2);
391 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
392 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
395 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
396 irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
398 cpyxchg = be_new_Copy(reg_class, env->chord_env->irg, block, arg1);
399 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
401 /* remove the proj from the schedule */
404 /* exchange copy node and proj */
405 exchange(res2, cpyxchg);
408 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
409 sched_add_after(sched_point, cpyxchg);
411 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
418 /* remove the perm from schedule */
425 * Adds Projs to keep nodes for each register class, which eats the
426 * caller saved registers.
427 * Note: The caller has to make sure, that call is a Call
429 * @param call The Call node
430 * @param walk_env The walker environment
432 static void lower_call_node(ir_node *call, const void *walk_env) {
433 const arch_env_t *arch_env = walk_env;
436 arch_isa_t *isa = arch_env_get_isa(arch_env);
437 const ir_node *proj_T = NULL;
438 ir_node *block = get_nodes_block(call);
439 const arch_register_class_t *reg_class;
440 int i, j, pn, keep_arity;
443 const ir_edge_t *edge;
444 const arch_register_t *reg;
446 /* Prepare the bitset where we store the projnums which are already in use*/
447 for (i = 0; i < arch_isa_get_n_reg_class(isa); i++) {
448 reg_class = arch_isa_get_reg_class(isa, i);
449 set_size += arch_register_class_n_regs(reg_class);
452 in_keep = malloc(set_size * sizeof(ir_node *));
454 proj_set = bitset_malloc(set_size);
455 bitset_clear_all(proj_set);
457 /* check if there is a ProjT node and which arguments are used */
458 foreach_out_edge(call, edge) {
459 if (get_irn_mode(get_edge_src_irn(edge)) == mode_T)
460 proj_T = get_edge_src_irn(edge);
463 /* set all used arguments */
465 foreach_out_edge(proj_T, edge) {
466 ir_node *proj = get_edge_src_irn(edge);
468 assert(is_Proj(proj));
469 pn = isa->impl->handle_call_proj(isa, proj, 0);
470 bitset_set(proj_set, pn);
474 proj_T = new_r_Proj(current_ir_graph, block, call, mode_T, pn_Call_T_result);
477 /* Create for each caller save register a proj (keep node argument) */
478 /* if this proj is not already present */
479 for (i = 0; i < arch_isa_get_n_reg_class(isa); i++) {
481 /* reset the keep input, as we need one keep for each register class */
482 memset(in_keep, 0, set_size * sizeof(ir_node *));
484 reg_class = arch_isa_get_reg_class(isa, i);
486 for (j = 0; j < arch_register_class_n_regs(reg_class); j++) {
487 reg = arch_register_for_index(reg_class, j);
489 /* only check caller save registers */
490 if (arch_register_type_is(reg, caller_saved)) {
492 /* Only create new proj, iff not already present */
493 if (!bitset_is_set(proj_set, bitset_idx)) {
494 ir_node *proj = new_r_Proj(current_ir_graph, block, (ir_node *)proj_T, mode_Is, bitset_idx);
496 pn = isa->impl->handle_call_proj(isa, proj, 1);
497 in_keep[keep_arity++] = proj;
504 /* ok, we found some caller save register which are not in use but must be saved */
506 be_new_Keep(reg_class, current_ir_graph, block, keep_arity, in_keep);
510 bitset_free(proj_set);
517 * Calls the backend code generator functions to lower Spill and
518 * Reload nodes into Store and Load. The backend is fully responsible
519 * for creating the new nodes and setting their input correct.
520 * Note: The caller of this has to make sure that irn is a Spill
523 * @param irn The Spill/Reload node
524 * @param walk_env The walker environment
526 static void lower_spill_reload(ir_node *irn, void *walk_env) {
527 lower_env_t *env = walk_env;
528 arch_code_generator_t *cg = env->chord_env->main_env->cg;
529 const arch_env_t *aenv = env->chord_env->main_env->arch_env;
531 ir_node *sched_point;
533 if (be_is_Spill(irn) && cg->impl->lower_spill) {
534 res = cg->impl->lower_spill(cg, irn);
536 else if (be_is_Reload(irn) && cg->impl->lower_reload) {
537 res = cg->impl->lower_reload(cg, irn);
538 if (res && res != irn) {
539 /* copy the result register from the reload to the load */
540 arch_set_irn_register(aenv, res, arch_get_irn_register(aenv, irn));
544 if (res && res != irn) {
545 sched_point = sched_prev(irn);
548 sched_add_after(sched_point, res);
551 DBG((env->dbg_module, LEVEL_1, "node %+F not lowered\n", irn));
559 * Calls the corresponding lowering function for the node.
561 * @param irn The node to be checked for lowering
562 * @param walk_env The walker environment
564 static void lower_nodes_before_sched_walker(ir_node *irn, const void *walk_env) {
565 const arch_env_t *arch_env = walk_env;
567 if (!is_Block(irn) && !is_Proj(irn)) {
568 if (is_Call(arch_env, irn)) {
569 lower_call_node(irn, walk_env);
578 * Calls the corresponding lowering function for the node.
580 * @param irn The node to be checked for lowering
581 * @param walk_env The walker environment
583 static void lower_nodes_after_ra_walker(ir_node *irn, void *walk_env) {
584 lower_env_t *env = walk_env;
585 const arch_env_t *arch_env = env->chord_env->main_env->arch_env;
587 if (!is_Block(irn) && !is_Proj(irn)) {
588 if (is_Perm(arch_env, irn)) {
589 lower_perm_node(irn, walk_env);
591 else if (be_is_Spill(irn) || be_is_Reload(irn)) {
592 lower_spill_reload(irn, walk_env);
602 * Walks over all blocks in an irg and performs lowering need
603 * to be done before scheduling (e.g. call lowering).
605 * @param chord_env The chordal environment containing the irg
606 * @param do_copy 1 == resolve cycles with a free reg if available
608 void lower_nodes_before_sched(ir_graph *irg, const void *env) {
609 irg_walk_blkwise_graph(irg, NULL, lower_nodes_before_sched_walker, (void *)env);
615 * Walks over all blocks in an irg and performs lowering need to be
616 * done after register allocation (e.g. perm and spill/reload lowering).
618 * @param chord_env The chordal environment containing the irg
619 * @param do_copy 1 == resolve cycles with a free reg if available
621 void lower_nodes_after_ra(be_chordal_env_t *chord_env, int do_copy) {
624 env.chord_env = chord_env;
625 env.do_copy = do_copy;
626 env.dbg_module = firm_dbg_register("firm.be.lower");
628 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_after_ra_walker, &env);