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.
23 #include "bechordal_t.h"
24 #include "besched_t.h"
28 #include "iredges_t.h"
39 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
41 /* lowering walker environment */
42 typedef struct _lower_env_t {
43 be_chordal_env_t *chord_env;
45 DEBUG_ONLY(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 = xcalloc((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 lower_env_t *env = walk_env;
252 int n, i, pn, do_copy, j, n_ops;
254 const ir_edge_t *edge;
256 ir_node *sched_point, *block, *in[2];
257 ir_node *arg1, *arg2, *res1, *res2;
258 ir_node *cpyxchg = NULL;
259 DEBUG_ONLY(firm_dbg_module_t *mod;)
261 arch_env = env->chord_env->birg->main_env->arch_env;
262 do_copy = env->do_copy;
263 DEBUG_ONLY(mod = env->dbg_module;)
264 block = get_nodes_block(irn);
267 Get the schedule predecessor node to the perm
268 NOTE: This works with auto-magic. If we insert the
269 new copy/exchange nodes after this node, everything
272 sched_point = sched_prev(irn);
273 DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
274 assert(sched_point && "Perm is not scheduled or has no predecessor");
276 n = get_irn_arity(irn);
277 assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
279 reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
280 pairs = alloca(n * sizeof(pairs[0]));
282 /* build the list of register pairs (in, out) */
284 foreach_out_edge(irn, edge) {
285 pairs[i].out_node = get_edge_src_irn(edge);
286 pn = get_Proj_proj(pairs[i].out_node);
287 pairs[i].in_node = get_irn_n(irn, pn);
289 pairs[i].in_reg = arch_get_irn_register(arch_env, pairs[i].in_node);
290 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
292 pairs[i].checked = 0;
296 /* sort the register pairs by the indices of the in registers */
297 qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
299 /* Mark all equal pairs as checked, and exchange the OUT proj with
301 for (i = 0; i < n; i++) {
302 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
303 DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
304 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
306 /* We have to check for a special case:
307 The in-node could be a Proj from a Perm. In this case,
308 we need to correct the projnum */
309 if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
310 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
313 /* remove the proj from the schedule */
314 sched_remove(pairs[i].out_node);
316 /* reroute the edges from the proj to the argument */
317 edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
319 pairs[i].checked = 1;
323 /* Set do_copy to 0 if it's on but we have no free register */
328 real_size = n - get_n_checked_pairs(pairs, n);
330 be_do_stat_perm(reg_class->name, reg_class->n_regs, irn, block, n, real_size);
332 /* check for cycles and chains */
333 while (get_n_checked_pairs(pairs, n) < n) {
336 /* go to the first not-checked pair */
337 while (pairs[i].checked) i++;
338 cycle = xcalloc(1, sizeof(*cycle));
339 cycle = get_perm_cycle(cycle, pairs, n, i);
341 DB((mod, LEVEL_1, "%+F: following %s created:\n ", irn, cycle->type == PERM_CHAIN ? "chain" : "cycle"));
342 for (j = 0; j < cycle->n_elems; j++) {
343 DB((mod, LEVEL_1, " %s", cycle->elems[j]->name));
345 DB((mod, LEVEL_1, "\n"));
347 /* We don't need to do anything if we have a Perm with two
348 elements which represents a cycle, because those nodes
349 already represent exchange nodes */
350 if (n == 2 && cycle->type == PERM_CYCLE) {
355 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
356 // the copy cascade (this reduces the cycle into a chain)
358 /* build copy/swap nodes from back to front */
359 for (i = cycle->n_elems - 2; i >= 0; i--) {
360 arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
361 arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
363 res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
364 res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
366 If we have a cycle and don't copy: we need to create exchange nodes
367 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
368 IN_1 = in node with register i
369 IN_2 = in node with register i + 1
370 OUT_1 = out node with register i + 1
371 OUT_2 = out node with register i
373 if (cycle->type == PERM_CYCLE && !do_copy) {
377 /* At this point we have to handle the following problem: */
379 /* If we have a cycle with more than two elements, then */
380 /* this could correspond to the following Perm node: */
382 /* +----+ +----+ +----+ */
383 /* | r1 | | r2 | | r3 | */
384 /* +-+--+ +-+--+ +--+-+ */
387 /* +-+--------+---------+-+ */
389 /* +-+--------+---------+-+ */
392 /* +-+--+ +-+--+ +--+-+ */
393 /* |Proj| |Proj| |Proj| */
394 /* | r2 | | r3 | | r1 | */
395 /* +----+ +----+ +----+ */
397 /* This node is about to be split up into two 2x Perm's */
398 /* for which we need 4 Proj's and the one additional Proj */
399 /* of the first Perm has to be one IN of the second. So in */
400 /* general we need to create one additional Proj for each */
401 /* "middle" Perm and set this to one in node of the successor */
404 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
405 irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
406 DBG((mod, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
407 irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
409 cpyxchg = be_new_Perm(reg_class, env->chord_env->irg, block, 2, in);
413 /* cycle is not done yet */
414 int pidx = get_pairidx_for_regidx(pairs, n, cycle->elems[i]->index, 0);
416 /* create intermediate proj */
417 res1 = new_r_Proj(get_irn_irg(irn), block, cpyxchg, get_irn_mode(res1), 0);
419 /* set as in for next Perm */
420 pairs[pidx].in_node = res1;
428 set_Proj_pred(res2, cpyxchg);
429 set_Proj_proj(res2, 0);
430 set_Proj_pred(res1, cpyxchg);
431 set_Proj_proj(res1, 1);
433 sched_add_after(sched_point, res1);
434 sched_add_after(sched_point, res2);
436 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
437 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
439 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
440 sched_add_after(sched_point, cpyxchg);
442 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
444 /* set the new scheduling point */
448 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
449 irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
451 cpyxchg = be_new_Copy(reg_class, env->chord_env->irg, block, arg1);
452 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
455 /* remove the proj from the schedule */
458 /* exchange copy node and proj */
459 exchange(res2, cpyxchg);
461 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
462 sched_add_after(sched_point, cpyxchg);
464 /* set the new scheduling point */
465 sched_point = cpyxchg;
469 be_do_stat_permcycle(reg_class->name, irn, block, cycle->type == PERM_CHAIN, cycle->n_elems, n_ops);
471 free((void *) cycle->elems);
477 /* remove the perm from schedule */
483 static int get_n_out_edges(const ir_node *irn) {
484 const ir_edge_t *edge;
487 foreach_out_edge(irn, edge) {
494 static ir_node *belower_skip_proj(ir_node *irn) {
496 irn = get_Proj_pred(irn);
500 static void fix_in(ir_node *irn, ir_node *old, ir_node *nw) {
503 irn = belower_skip_proj(irn);
504 n = get_irn_arity(irn);
506 for (i = 0; i < n; i++) {
507 if (get_irn_n(irn, i) == old) {
508 set_irn_n(irn, i, nw);
514 static void gen_assure_different_pattern(ir_node *irn, be_irg_t *birg, ir_node *other_different) {
515 const arch_env_t *arch_env = birg->main_env->arch_env;
516 ir_node *in[2], *keep, *cpy, *temp;
517 ir_node *block = get_nodes_block(irn);
518 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, other_different, -1);
519 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, "firm.be.lower");
521 if (arch_irn_is(arch_env, other_different, ignore) || ! mode_is_datab(get_irn_mode(other_different))) {
522 DBG((mod, LEVEL_1, "ignore constraint for %+F because other_irn is ignore or not a datab node\n", irn));
526 /* Make a not spillable copy of the different node */
527 /* this is needed because the different irn could be */
528 /* in block far far away */
529 /* The copy is optimized later if not needed */
531 temp = new_rd_Unknown(birg->irg, get_irn_mode(other_different));
532 cpy = be_new_Copy(cls, birg->irg, block, temp);
533 be_node_set_flags(cpy, BE_OUT_POS(0), arch_irn_flags_dont_spill);
538 /* Let the irn use the copy instead of the old other_different */
539 fix_in(irn, other_different, cpy);
541 /* Add the Keep resp. CopyKeep and reroute the users */
542 /* of the other_different irn in case of CopyKeep. */
543 if (get_n_out_edges(other_different) == 0) {
544 keep = be_new_Keep(cls, birg->irg, block, 2, in);
547 keep = be_new_CopyKeep_single(cls, birg->irg, block, cpy, irn, get_irn_mode(other_different));
548 be_node_set_reg_class(keep, 1, cls);
549 edges_reroute(other_different, keep, birg->irg);
552 /* after rerouting: let the copy point to the other_different irn */
553 set_irn_n(cpy, 0, other_different);
555 DBG((mod, LEVEL_1, "created %+F for %+F to assure should_be_different\n", keep, irn));
559 * Checks if node has a should_be_different constraint in output
560 * and adds a Keep then to assure the constraint.
562 static void assure_different_constraints(ir_node *irn, be_irg_t *birg) {
563 const arch_env_t *arch_env = birg->main_env->arch_env;
564 const arch_register_req_t *req;
565 arch_register_req_t req_temp;
568 req = arch_get_register_req(arch_env, &req_temp, irn, -1);
571 if (arch_register_req_is(req, should_be_different)) {
572 gen_assure_different_pattern(irn, birg, req->other_different);
574 else if (arch_register_req_is(req, should_be_different_from_all)) {
575 n = get_irn_arity(belower_skip_proj(irn));
576 for (i = 0; i < n; i++) {
577 gen_assure_different_pattern(irn, birg, get_irn_n(belower_skip_proj(irn), i));
586 * Calls the functions to assure register constraints.
588 * @param irn The node to be checked for lowering
589 * @param walk_env The walker environment
591 static void assure_constraints_walker(ir_node *irn, void *walk_env) {
595 if (mode_is_datab(get_irn_mode(irn)))
596 assure_different_constraints(irn, walk_env);
604 * Walks over all nodes to assure register constraints.
606 * @param birg The birg structure containing the irg
608 void assure_constraints(be_irg_t *birg) {
609 irg_walk_blkwise_graph(birg->irg, NULL, assure_constraints_walker, birg);
615 * Calls the corresponding lowering function for the node.
617 * @param irn The node to be checked for lowering
618 * @param walk_env The walker environment
620 static void lower_nodes_after_ra_walker(ir_node *irn, void *walk_env) {
621 lower_env_t *env = walk_env;
622 const arch_env_t *arch_env = env->chord_env->birg->main_env->arch_env;
624 if (!is_Block(irn) && !is_Proj(irn)) {
625 if (is_Perm(arch_env, irn)) {
626 lower_perm_node(irn, walk_env);
634 * Walks over all blocks in an irg and performs lowering need to be
635 * done after register allocation (e.g. perm lowering).
637 * @param chord_env The chordal environment containing the irg
638 * @param do_copy 1 == resolve cycles with a free reg if available
640 void lower_nodes_after_ra(be_chordal_env_t *chord_env, int do_copy) {
643 env.chord_env = chord_env;
644 env.do_copy = do_copy;
645 FIRM_DBG_REGISTER(env.dbg_module, "firm.be.lower");
647 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_after_ra_walker, &env);